Vectorize HeaderValidator::IsValidHeaderValue with branch-free loops.

`HeaderValidator::IsValidHeaderValue` checks each byte with a 256-byte lookup table and early exit, preventing auto-vectorization and limiting throughput to ~1.2 GB/s on every HTTP/2 header value (and on Envoy HTTP/1 header values via `HeaderUtility::headerValueIsValid`).

Replace the table lookup with branch-free bitwise checks for both `ObsTextOption::kAllow` and `ObsTextOption::kDisallow`. XORing each byte with 0x16 maps HTAB (0x09) to 0x1f and every other control character below 0x1f, while preserving the upper three bits for all bytes ≥ 0x20. Keeping the boolean accumulator local to each branch lets LLVM recognize both loops as vector reductions (width 16, interleave 4) instead of falling back to scalar unrolling. Validation semantics remain identical for all 256 byte values.

Tested:
  `header_validator_benchmark` (opt, static): `BM_ValidateSingleHeaderForRequests` 484ns → 241ns (2.01×), `BM_ValidateSingleHeaderForResponses` 658ns → 298ns (2.21×), `BM_IsValidHeaderValueAllowObsText` 16B to 4kB 3.0× to 22.0× (up to 27.9 GB/s), `BM_IsValidHeaderValueDisallowObsText` 16B to 4kB 3.0× to 19.9× (up to 24.9 GB/s). Exhaustive 256-byte tests and fuzz tests pass under default and dbg+asan builds.
PiperOrigin-RevId: 1029273412
diff --git a/quiche/http2/adapter/header_validator.cc b/quiche/http2/adapter/header_validator.cc
index d7b6873..2fd44ab 100644
--- a/quiche/http2/adapter/header_validator.cc
+++ b/quiche/http2/adapter/header_validator.cc
@@ -2,6 +2,7 @@
 
 #include <array>
 #include <bitset>
+#include <cstdint>
 #include <string>
 
 #include "absl/strings/ascii.h"
@@ -26,12 +27,6 @@
     "!#$%&'*+-.0123456789"
     "^_`abcdefghijklmnopqrstuvwxyz|~";
 
-constexpr absl::string_view kHttp2HeaderValueAllowedChars =
-    "\t "
-    "!\"#$%&'()*+,-./"
-    "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
-    "abcdefghijklmnopqrstuvwxyz{|}~";
-
 constexpr absl::string_view kHttp2StatusValueAllowedChars = "0123456789";
 
 constexpr absl::string_view kValidAuthorityChars =
@@ -56,14 +51,6 @@
   }
   return map;
 }
-constexpr CharMap AllowObsText(CharMap map) {
-  // Characters above 0x80 are allowed in header field values as `obs-text` in
-  // RFC 7230.
-  for (uint8_t c = 0xff; c >= 0x80; --c) {
-    map[c] = true;
-  }
-  return map;
-}
 
 bool AllCharsInMap(absl::string_view str, const CharMap& map) {
   for (char c : str) {
@@ -254,13 +241,20 @@
 
 bool HeaderValidator::IsValidHeaderValue(absl::string_view value,
                                          ObsTextOption option) {
-  static constexpr CharMap valid_chars =
-      BuildValidCharMap(kHttp2HeaderValueAllowedChars);
-  static constexpr CharMap valid_chars_with_obs_text =
-      AllowObsText(BuildValidCharMap(kHttp2HeaderValueAllowedChars));
-  return AllCharsInMap(value, option == ObsTextOption::kAllow
-                                  ? valid_chars_with_obs_text
-                                  : valid_chars);
+  // 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;
+  }
+  bool valid = true;
+  for (const uint8_t c : value) {
+    valid &= IsValidHeaderValueChar(c, ObsTextOption::kDisallow);
+  }
+  return valid;
 }
 
 bool HeaderValidator::IsValidAuthority(absl::string_view authority) {
diff --git a/quiche/http2/adapter/header_validator.h b/quiche/http2/adapter/header_validator.h
index d75e1e8..fdc7088 100644
--- a/quiche/http2/adapter/header_validator.h
+++ b/quiche/http2/adapter/header_validator.h
@@ -2,6 +2,7 @@
 #define QUICHE_HTTP2_ADAPTER_HEADER_VALIDATOR_H_
 
 #include <bitset>
+#include <cstdint>
 #include <optional>
 #include <string>
 #include <vector>
@@ -36,6 +37,35 @@
   static bool IsValidHeaderValue(absl::string_view value,
                                  ObsTextOption ops_text_option);
 
+  // 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:
+  //   0x00-0x08 : invalid (control chars)
+  //   0x09      : valid   (HTAB '\t')
+  //   0x0a-0x1f : invalid (control chars)
+  //   0x20-0x7e : valid   (SP ' ' through '~')
+  //   0x7f      : invalid (DEL)
+  //   0x80-0xff : valid only when `obs_text_option == ObsTextOption::kAllow`
+  //
+  // Equivalent to checking membership in:
+  //   constexpr absl::string_view kHttp2HeaderValueAllowedChars =
+  //       "\t "
+  //       "!\"#$%&'()*+,-./"
+  //       "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
+  //       "abcdefghijklmnopqrstuvwxyz{|}~";
+  // (plus bytes 0x80-0xff when ObsTextOption::kAllow is set).
+  //
+  // 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));
+  }
+
   // Returns whether `authority` is valid according to RFC 3986 Section 3.2.
   static bool IsValidAuthority(absl::string_view authority);
 
diff --git a/quiche/http2/adapter/header_validator_test.cc b/quiche/http2/adapter/header_validator_test.cc
index 26ea561..cb0ad56 100644
--- a/quiche/http2/adapter/header_validator_test.cc
+++ b/quiche/http2/adapter/header_validator_test.cc
@@ -1,11 +1,14 @@
 #include "quiche/http2/adapter/header_validator.h"
 
+#include <cstddef>
+#include <cstdint>
 #include <optional>
 #include <string>
 #include <utility>
 #include <vector>
 
 #include "absl/strings/escaping.h"
+#include "absl/strings/match.h"
 #include "absl/strings/str_cat.h"
 #include "quiche/common/platform/api/quiche_test.h"
 
@@ -133,6 +136,42 @@
   }
 }
 
+TEST(HeaderValidatorTest, IsValidHeaderValueAllBytes) {
+  constexpr absl::string_view kHttp2HeaderValueAllowedChars =
+      "\t "
+      "!\"#$%&'()*+,-./"
+      "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
+      "abcdefghijklmnopqrstuvwxyz{|}~";
+  for (int c = 0; c < 256; ++c) {
+    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),
+              expected_no_obstext)
+        << "byte " << c;
+    EXPECT_EQ(HeaderValidator::IsValidHeaderValueChar(static_cast<uint8_t>(c),
+                                                      ObsTextOption::kAllow),
+              expected_with_obstext)
+        << "byte " << c;
+    // Exercise both vectorized chunks and scalar remainders across lengths.
+    for (const size_t size : {1, 5, 12, 16, 32, 40, 67}) {
+      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::IsValidHeaderValue(value,
+                                                      ObsTextOption::kDisallow),
+                  expected_no_obstext)
+            << "byte " << c << " at " << position << " of " << size;
+        EXPECT_EQ(
+            HeaderValidator::IsValidHeaderValue(value, ObsTextOption::kAllow),
+            expected_with_obstext)
+            << "byte " << c << " at " << position << " of " << size;
+      }
+    }
+  }
+}
+
 TEST(HeaderValidatorTest, StatusHasInvalidChar) {
   HeaderValidator v;