From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 23FAF3858D39 for ; Tue, 28 Feb 2023 09:50:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 23FAF3858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677577846; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6zNwlzA6gmIr6DaGqElX1q4PgVfe0/j0f8Zd2by8xpQ=; b=UlApVUhgAmMNSUM8Zbrkf8zMkYnB7qhtgr6eqhkng+rbZ0k9zdjV+FDOrRIgfMtQhZcCJj sutPXX1gv6jyEUZqZcN+bGhab8Rv40v+7wgxlsHcHPjUoUJjJEbT7PQyDRnH1+iVSmETF1 glt6oclxey1cz7JnBw9+rf2p2BGPc4k= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-672-nCVeT30jNOiw39nlBiOlkg-1; Tue, 28 Feb 2023 04:50:43 -0500 X-MC-Unique: nCVeT30jNOiw39nlBiOlkg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7F5F780558D; Tue, 28 Feb 2023 09:50:43 +0000 (UTC) Received: from localhost (unknown [10.33.36.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 46678140EBF6; Tue, 28 Feb 2023 09:50:43 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add likely/unlikely attributes to implementation Date: Tue, 28 Feb 2023 09:50:42 +0000 Message-Id: <20230228095042.1192997-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested powerpc64le-linux. Pushed to trunk. -- >8 -- For the common case of converting valid text this improves performance significantly. libstdc++-v3/ChangeLog: * src/c++11/codecvt.cc: Add [[likely]] and [[unlikely]] attributes. --- libstdc++-v3/src/c++11/codecvt.cc | 92 +++++++++++++++---------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/libstdc++-v3/src/c++11/codecvt.cc b/libstdc++-v3/src/c++11/codecvt.cc index e333e795f48..02f05752de8 100644 --- a/libstdc++-v3/src/c++11/codecvt.cc +++ b/libstdc++-v3/src/c++11/codecvt.cc @@ -256,19 +256,19 @@ namespace return incomplete_mb_character; char32_t c1 = (unsigned char) from[0]; // https://en.wikipedia.org/wiki/UTF-8#Sample_code - if (c1 < 0x80) + if (c1 < 0x80) [[likely]] { ++from; return c1; } - else if (c1 < 0xC2) // continuation or overlong 2-byte sequence + else if (c1 < 0xC2) [[unlikely]] // continuation or overlong 2-byte sequence return invalid_mb_sequence; else if (c1 < 0xE0) // 2-byte sequence { - if (avail < 2) + if (avail < 2) [[unlikely]] return incomplete_mb_character; char32_t c2 = (unsigned char) from[1]; - if ((c2 & 0xC0) != 0x80) + if ((c2 & 0xC0) != 0x80) [[unlikely]] return invalid_mb_sequence; char32_t c = (c1 << 6) + c2 - 0x3080; if (c <= maxcode) @@ -277,17 +277,17 @@ namespace } else if (c1 < 0xF0) // 3-byte sequence { - if (avail < 2) + if (avail < 2) [[unlikely]] return incomplete_mb_character; char32_t c2 = (unsigned char) from[1]; - if ((c2 & 0xC0) != 0x80) + if ((c2 & 0xC0) != 0x80) [[unlikely]] return invalid_mb_sequence; - if (c1 == 0xE0 && c2 < 0xA0) // overlong + if (c1 == 0xE0 && c2 < 0xA0) [[unlikely]] // overlong return invalid_mb_sequence; - if (avail < 3) + if (avail < 3) [[unlikely]] return incomplete_mb_character; char32_t c3 = (unsigned char) from[2]; - if ((c3 & 0xC0) != 0x80) + if ((c3 & 0xC0) != 0x80) [[unlikely]] return invalid_mb_sequence; char32_t c = (c1 << 12) + (c2 << 6) + c3 - 0xE2080; if (c <= maxcode) @@ -296,31 +296,31 @@ namespace } else if (c1 < 0xF5 && maxcode > 0xFFFF) // 4-byte sequence { - if (avail < 2) + if (avail < 2) [[unlikely]] return incomplete_mb_character; char32_t c2 = (unsigned char) from[1]; - if ((c2 & 0xC0) != 0x80) + if ((c2 & 0xC0) != 0x80) [[unlikely]] return invalid_mb_sequence; - if (c1 == 0xF0 && c2 < 0x90) // overlong + if (c1 == 0xF0 && c2 < 0x90) [[unlikely]] // overlong return invalid_mb_sequence; - if (c1 == 0xF4 && c2 >= 0x90) // > U+10FFFF + if (c1 == 0xF4 && c2 >= 0x90) [[unlikely]] // > U+10FFFF return invalid_mb_sequence; - if (avail < 3) + if (avail < 3) [[unlikely]] return incomplete_mb_character; char32_t c3 = (unsigned char) from[2]; - if ((c3 & 0xC0) != 0x80) + if ((c3 & 0xC0) != 0x80) [[unlikely]] return invalid_mb_sequence; - if (avail < 4) + if (avail < 4) [[unlikely]] return incomplete_mb_character; char32_t c4 = (unsigned char) from[3]; - if ((c4 & 0xC0) != 0x80) + if ((c4 & 0xC0) != 0x80) [[unlikely]] return invalid_mb_sequence; char32_t c = (c1 << 18) + (c2 << 12) + (c3 << 6) + c4 - 0x3C82080; if (c <= maxcode) from += 4; return c; } - else // > U+10FFFF + else [[unlikely]] // > U+10FFFF return invalid_mb_sequence; } @@ -330,20 +330,20 @@ namespace { if (code_point < 0x80) { - if (to.size() < 1) + if (to.size() < 1) [[unlikely]] return false; to = code_point; } else if (code_point <= 0x7FF) { - if (to.size() < 2) + if (to.size() < 2) [[unlikely]] return false; to = (code_point >> 6) + 0xC0; to = (code_point & 0x3F) + 0x80; } else if (code_point <= 0xFFFF) { - if (to.size() < 3) + if (to.size() < 3) [[unlikely]] return false; to = (code_point >> 12) + 0xE0; to = ((code_point >> 6) & 0x3F) + 0x80; @@ -351,14 +351,14 @@ namespace } else if (code_point <= 0x10FFFF) { - if (to.size() < 4) + if (to.size() < 4) [[unlikely]] return false; to = (code_point >> 18) + 0xF0; to = ((code_point >> 12) & 0x3F) + 0x80; to = ((code_point >> 6) & 0x3F) + 0x80; to = (code_point & 0x3F) + 0x80; } - else + else [[unlikely]] return false; return true; } @@ -403,16 +403,16 @@ namespace unsigned long maxcode, codecvt_mode mode) { const size_t avail = from.size(); - if (avail == 0) + if (avail == 0) [[unlikely]] return incomplete_mb_character; int inc = 1; char32_t c = adjust_byte_order(from[0], mode); if (is_high_surrogate(c)) { - if (avail < 2) + if (avail < 2) [[unlikely]] return incomplete_mb_character; const char16_t c2 = adjust_byte_order(from[1], mode); - if (is_low_surrogate(c2)) + if (is_low_surrogate(c2)) [[likely]] { c = surrogate_pair_to_code_point(c, c2); inc = 2; @@ -420,7 +420,7 @@ namespace else return invalid_mb_sequence; } - else if (is_low_surrogate(c)) + else if (is_low_surrogate(c)) [[unlikely]] return invalid_mb_sequence; if (c <= maxcode) from += inc; @@ -464,9 +464,9 @@ namespace while (from.size() && to.size()) { const char32_t codepoint = read_utf8_code_point(from, maxcode); - if (codepoint == incomplete_mb_character) + if (codepoint == incomplete_mb_character) [[unlikely]] return codecvt_base::partial; - if (codepoint > maxcode) + if (codepoint > maxcode) [[unlikely]] return codecvt_base::error; to = codepoint; } @@ -479,14 +479,14 @@ namespace ucs4_out(range& from, range& to, unsigned long maxcode = max_code_point, codecvt_mode mode = {}) { - if (!write_utf8_bom(to, mode)) + if (!write_utf8_bom(to, mode)) [[unlikely]] return codecvt_base::partial; while (from.size()) { const char32_t c = from[0]; - if (c > maxcode) + if (c > maxcode) [[unlikely]] return codecvt_base::error; - if (!write_utf8_code_point(to, c)) + if (!write_utf8_code_point(to, c)) [[unlikely]] return codecvt_base::partial; ++from; } @@ -502,9 +502,9 @@ namespace while (from.size() && to.size()) { const char32_t codepoint = read_utf16_code_point(from, maxcode, mode); - if (codepoint == incomplete_mb_character) + if (codepoint == incomplete_mb_character) [[unlikely]] return codecvt_base::partial; - if (codepoint > maxcode) + if (codepoint > maxcode) [[unlikely]] return codecvt_base::error; to = codepoint; } @@ -516,14 +516,14 @@ namespace ucs4_out(range& from, range& to, unsigned long maxcode = max_code_point, codecvt_mode mode = {}) { - if (!write_utf16_bom(to, mode)) + if (!write_utf16_bom(to, mode)) [[unlikely]] return codecvt_base::partial; while (from.size()) { const char32_t c = from[0]; - if (c > maxcode) + if (c > maxcode) [[unlikely]] return codecvt_base::error; - if (!write_utf16_code_point(to, c, mode)) + if (!write_utf16_code_point(to, c, mode)) [[unlikely]] return codecvt_base::partial; ++from; } @@ -544,11 +544,11 @@ namespace { auto orig = from; const char32_t codepoint = read_utf8_code_point(from, maxcode); - if (codepoint == incomplete_mb_character) + if (codepoint == incomplete_mb_character) [[unlikely]] return codecvt_base::partial; if (codepoint > maxcode) return codecvt_base::error; - if (!write_utf16_code_point(to, codepoint, mode)) + if (!write_utf16_code_point(to, codepoint, mode)) [[unlikely]] { from = orig; // rewind to previous position return codecvt_base::partial; @@ -564,7 +564,7 @@ namespace unsigned long maxcode = max_code_point, codecvt_mode mode = {}, surrogates s = surrogates::allowed) { - if (!write_utf8_bom(to, mode)) + if (!write_utf8_bom(to, mode)) [[unlikely]] return codecvt_base::partial; while (from.size()) { @@ -572,14 +572,14 @@ namespace int inc = 1; if (is_high_surrogate(c)) { - if (s == surrogates::disallowed) + if (s == surrogates::disallowed) [[unlikely]] return codecvt_base::error; // No surrogates in UCS-2 - if (from.size() < 2) + if (from.size() < 2) [[unlikely]] return codecvt_base::partial; // stop converting at this point const char32_t c2 = from[1]; - if (is_low_surrogate(c2)) + if (is_low_surrogate(c2)) [[likely]] { c = surrogate_pair_to_code_point(c, c2); inc = 2; @@ -587,11 +587,11 @@ namespace else return codecvt_base::error; } - else if (is_low_surrogate(c)) + else if (is_low_surrogate(c)) [[unlikely]] return codecvt_base::error; - if (c > maxcode) + if (c > maxcode) [[unlikely]] return codecvt_base::error; - if (!write_utf8_code_point(to, c)) + if (!write_utf8_code_point(to, c)) [[unlikely]] return codecvt_base::partial; from += inc; } -- 2.39.2