Split HeaderValidator::IsValidHeaderValueChar into obs-text-specific helpers. Replace `HeaderValidator::IsValidHeaderValueChar(uint8_t c, ObsTextOption)` with dedicated `IsValidHeaderValueCharWithObsText(uint8_t c)` and `IsValidHeaderValueCharWithoutObsText(uint8_t c)` helpers so each per-character predicate is unconditionally branch-free without taking an `ObsTextOption` parameter. This makes the branch-free intent explicit and prevents future refactors (or direct callers of the per-character helper) from introducing a runtime branch on `ObsTextOption`. Also expose `IsValidHeaderValueWithObsText` and `IsValidHeaderValueWithoutObsText` for callers with a compile-time obs-text policy (avoiding the option check in `IsValidHeaderValue`), while keeping `IsValidHeaderValue(absl::string_view, ObsTextOption)` as a thin wrapper for backwards compatibility. Verified that all loops still auto-vectorize (width 16, interleaved count 4). PiperOrigin-RevId: 1032766100
diff --git a/quiche/http2/adapter/header_validator.cc b/quiche/http2/adapter/header_validator.cc index 2fd44ab..41cdff8 100644 --- a/quiche/http2/adapter/header_validator.cc +++ b/quiche/http2/adapter/header_validator.cc
@@ -241,18 +241,26 @@ bool HeaderValidator::IsValidHeaderValue(absl::string_view value, ObsTextOption option) { - // Using bitwise `&=` without early exit and keeping `valid` local to each - // branch lets compilers auto-vectorize both loops as SIMD reductions. - if (option == ObsTextOption::kAllow) { - bool valid = true; - for (const uint8_t c : value) { - valid &= IsValidHeaderValueChar(c, ObsTextOption::kAllow); - } - return valid; - } + return option == ObsTextOption::kAllow + ? IsValidHeaderValueWithObsText(value) + : IsValidHeaderValueWithoutObsText(value); +} + +bool HeaderValidator::IsValidHeaderValueWithObsText(absl::string_view value) { + // Using bitwise `&=` without early exit lets compilers auto-vectorize the + // loop as a SIMD reduction. bool valid = true; for (const uint8_t c : value) { - valid &= IsValidHeaderValueChar(c, ObsTextOption::kDisallow); + valid &= IsValidHeaderValueCharWithObsText(c); + } + return valid; +} + +bool HeaderValidator::IsValidHeaderValueWithoutObsText( + absl::string_view value) { + bool valid = true; + for (const uint8_t c : value) { + valid &= IsValidHeaderValueCharWithoutObsText(c); } return valid; }
diff --git a/quiche/http2/adapter/header_validator.h b/quiche/http2/adapter/header_validator.h index fdc7088..012096d 100644 --- a/quiche/http2/adapter/header_validator.h +++ b/quiche/http2/adapter/header_validator.h
@@ -36,6 +36,8 @@ // RFC 9113 Section 8.2.1. static bool IsValidHeaderValue(absl::string_view value, ObsTextOption ops_text_option); + static bool IsValidHeaderValueWithObsText(absl::string_view value); + static bool IsValidHeaderValueWithoutObsText(absl::string_view value); // Returns whether a single byte `c` is valid in a header value according to // RFC 9110 Section 5.5 and RFC 9113 Section 8.2.1: @@ -44,7 +46,7 @@ // 0x0a-0x1f : invalid (control chars) // 0x20-0x7e : valid (SP ' ' through '~') // 0x7f : invalid (DEL) - // 0x80-0xff : valid only when `obs_text_option == ObsTextOption::kAllow` + // 0x80-0xff : valid only with obs-text allowed // // Equivalent to checking membership in: // constexpr absl::string_view kHttp2HeaderValueAllowedChars = @@ -52,18 +54,19 @@ // "!\"#$%&'()*+,-./" // "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" // "abcdefghijklmnopqrstuvwxyz{|}~"; - // (plus bytes 0x80-0xff when ObsTextOption::kAllow is set). + // (plus bytes 0x80-0xff for `IsValidHeaderValueCharWithObsText`). // // HTAB (0x09) sits isolated inside the 0x00-0x1f control range. XORing with // 0x16 (0x09 ^ 0x1f) moves HTAB right next to SP (0x20) at 0x1f, keeps every // other control byte below 0x1f, and leaves the top 3 bits of >= 0x20 bytes // non-zero. That turns `c == '\t' || c >= 0x20` into `(c ^ 0x16) >= 0x1f`. - // Bitwise `&` preserves branch-free vectorization. - static constexpr bool IsValidHeaderValueChar(uint8_t c, - ObsTextOption obs_text_option) { - return obs_text_option == ObsTextOption::kAllow - ? static_cast<bool>(((c ^ 0x16) >= 0x1f) & (c != 0x7f)) - : static_cast<bool>(((c ^ 0x16) >= 0x1f) & (c < 0x7f)); + // Splitting into two functions and using bitwise `&` keeps both helpers + // unconditionally branch-free and auto-vectorizable. + static constexpr bool IsValidHeaderValueCharWithObsText(uint8_t c) { + return static_cast<bool>(((c ^ 0x16) >= 0x1f) & (c != 0x7f)); + } + static constexpr bool IsValidHeaderValueCharWithoutObsText(uint8_t c) { + return static_cast<bool>(((c ^ 0x16) >= 0x1f) & (c < 0x7f)); } // Returns whether `authority` is valid according to RFC 3986 Section 3.2.
diff --git a/quiche/http2/adapter/header_validator_test.cc b/quiche/http2/adapter/header_validator_test.cc index cb0ad56..304f2ee 100644 --- a/quiche/http2/adapter/header_validator_test.cc +++ b/quiche/http2/adapter/header_validator_test.cc
@@ -146,12 +146,12 @@ const bool expected_no_obstext = absl::StrContains(kHttp2HeaderValueAllowedChars, static_cast<char>(c)); const bool expected_with_obstext = expected_no_obstext || c >= 0x80; - EXPECT_EQ(HeaderValidator::IsValidHeaderValueChar(static_cast<uint8_t>(c), - ObsTextOption::kDisallow), + EXPECT_EQ(HeaderValidator::IsValidHeaderValueCharWithoutObsText( + static_cast<uint8_t>(c)), expected_no_obstext) << "byte " << c; - EXPECT_EQ(HeaderValidator::IsValidHeaderValueChar(static_cast<uint8_t>(c), - ObsTextOption::kAllow), + EXPECT_EQ(HeaderValidator::IsValidHeaderValueCharWithObsText( + static_cast<uint8_t>(c)), expected_with_obstext) << "byte " << c; // Exercise both vectorized chunks and scalar remainders across lengths. @@ -159,6 +159,12 @@ for (const size_t position : {size_t{0}, size / 2, size - 1}) { std::string value(size, 'v'); value[position] = static_cast<char>(c); + EXPECT_EQ(HeaderValidator::IsValidHeaderValueWithoutObsText(value), + expected_no_obstext) + << "byte " << c << " at " << position << " of " << size; + EXPECT_EQ(HeaderValidator::IsValidHeaderValueWithObsText(value), + expected_with_obstext) + << "byte " << c << " at " << position << " of " << size; EXPECT_EQ(HeaderValidator::IsValidHeaderValue(value, ObsTextOption::kDisallow), expected_no_obstext)