Vectorizes `quiche::header_properties::HasInvalidHeaderChars`. Previously, `HasInvalidHeaderChars` scanned each byte using an early-exit loop over a 256-entry `std::array<bool, 256>` lookup table (`IsInvalidHeaderChar`), which blocked LLVM auto-vectorization and consumed ~3,640 GCUs fleet-wide (`IsInvalidHeaderChar` consumed an additional ~192 GCUs). This change replaces the lookup table with a branch-free `constexpr` predicate (`IsValidHeaderCharImpl`) and accumulates validity over `uint8_t` bytes using bitwise `&=`. With `-c opt`, Clang vectorizes `HasInvalidHeaderChars` at `vectorization width: 16, interleaved count: 4` with zero register spills. Microbenchmark results on Broadwell (`//third_party/quiche/balsa:header_properties_benchmark`, `setarch x86_64 -R --benchmark_repetitions=5 --benchmark_min_time=0.5s`): - 16 B: 15.6 ns (984 MB/s) -> 11.7 ns (1.32 GB/s) - 64 B: 56.0 ns (1.07 GB/s) -> 12.0 ns (5.04 GB/s, 4.7x) - 256 B: 246 ns (1.01 GB/s) -> 27.2 ns (8.93 GB/s, 9.0x) - 4096 B: 3,448 ns (1.11 GB/s) -> 384 ns (9.95 GB/s, 9.0x) Also adds exhaustive 256-byte and vector-boundary unit tests in `HeaderPropertiesTest` (verified in default and `-c dbg --config=asan` modes) and a multi-size microbenchmark in `//third_party/quiche/balsa:header_properties_benchmark`. Developed with this skill: http://google3/experimental/users/birenroy/skills/vectorizing_cpp_strings/ Protected by optimization with no functional change; not protected. PiperOrigin-RevId: 1033890867
diff --git a/quiche/balsa/header_properties.cc b/quiche/balsa/header_properties.cc index 57ffbc1..42702b5 100644 --- a/quiche/balsa/header_properties.cc +++ b/quiche/balsa/header_properties.cc
@@ -90,12 +90,12 @@ return invalidCharTable; } -constexpr std::array<bool, 256> buildInvalidCharLookupTable() { - std::array<bool, 256> invalidCharTable{}; - for (uint8_t c : kInvalidHeaderCharList) { - invalidCharTable[c] = true; - } - return invalidCharTable; +// Returns true if `c` is not in `kInvalidHeaderCharList`: +// {0x09, 0x0a, 0x0d, 0x20-0x7e, 0x80-0xff}. +// Note that `(c | 0x04) == 0x0d` matches both `\t` (0x09) and `\r` (0x0d). +constexpr bool IsValidHeaderCharImpl(uint8_t c) { + return static_cast<bool>(((c >= 0x20) & (c != 0x7f)) | ((c | 0x04) == 0x0d) | + (c == '\n')); } constexpr std::array<bool, 256> kAllTrueArray = { @@ -188,12 +188,7 @@ return invalidHeaderKeyCharTable[c]; } -bool IsInvalidHeaderChar(uint8_t c) { - static constexpr std::array<bool, 256> invalidCharTable = - buildInvalidCharLookupTable(); - - return invalidCharTable[c]; -} +bool IsInvalidHeaderChar(uint8_t c) { return !IsValidHeaderCharImpl(c); } bool IsValidTokenChar(uint8_t c) { static constexpr std::array<bool, 256> validTokenCharTable = @@ -230,12 +225,13 @@ } bool HasInvalidHeaderChars(absl::string_view value) { - for (const char c : value) { - if (IsInvalidHeaderChar(c)) { - return true; - } + bool valid = true; + for (const uint8_t c : value) { + // Do not convert this to an early return, as that will prevent the compiler + // from vectorizing this loop. + valid &= IsValidHeaderCharImpl(c); } - return false; + return !valid; } bool HasInvalidPathChar(absl::string_view value) {
diff --git a/quiche/balsa/header_properties_test.cc b/quiche/balsa/header_properties_test.cc index f4e749a..7d2b5c2 100644 --- a/quiche/balsa/header_properties_test.cc +++ b/quiche/balsa/header_properties_test.cc
@@ -1,9 +1,12 @@ #include "quiche/balsa/header_properties.h" +#include <cstddef> +#include <cstdint> #include <string> #include "absl/container/flat_hash_set.h" #include "absl/strings/ascii.h" +#include "absl/strings/match.h" #include "absl/strings/string_view.h" #include "quiche/common/platform/api/quiche_test.h" @@ -92,6 +95,7 @@ } TEST(HeaderPropertiesTest, HasInvalidHeaderChars) { + EXPECT_FALSE(HasInvalidHeaderChars("")); const char with_null[] = "Here's l\x00king at you, kid"; EXPECT_TRUE(HasInvalidHeaderChars(std::string(with_null, sizeof(with_null)))); EXPECT_TRUE(HasInvalidHeaderChars("Why's \x06 afraid of \x07? \x07\x08\x09")); @@ -105,6 +109,26 @@ EXPECT_FALSE(HasInvalidHeaderChars("\x42 is a nice character")); } +TEST(HeaderPropertiesTest, InvalidHeaderCharExhaustive256AndVectorBoundaries) { + const absl::string_view invalid_chars(kInvalidHeaderCharList, + sizeof(kInvalidHeaderCharList)); + constexpr size_t kLengths[] = {1, 5, 12, 16, 32, 40, 67}; + for (int b = 0; b < 256; ++b) { + const uint8_t byte = static_cast<uint8_t>(b); + const char ch = static_cast<char>(byte); + const bool expected_invalid = absl::StrContains(invalid_chars, ch); + EXPECT_EQ(IsInvalidHeaderChar(byte), expected_invalid) << "byte=" << b; + for (const size_t len : kLengths) { + for (const size_t pos : {size_t{0}, len / 2, len - 1}) { + std::string value(len, 'a'); + value[pos] = ch; + EXPECT_EQ(HasInvalidHeaderChars(value), expected_invalid) + << "byte=" << b << " len=" << len << " pos=" << pos; + } + } + } +} + TEST(HeaderPropertiesTest, HasInvalidPathChar) { EXPECT_FALSE(HasInvalidPathChar("")); EXPECT_FALSE(HasInvalidPathChar("/"));