From f4fed074f820797ae5ec2e0872c303b8a3769881 Mon Sep 17 00:00:00 2001 From: Masakazu Kitajo Date: Tue, 10 Jan 2023 15:21:38 -0700 Subject: [PATCH 1/3] Limit the search range of static table by the first letter of header name --- proxy/http2/HPACK.cc | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/proxy/http2/HPACK.cc b/proxy/http2/HPACK.cc index 5c5d3f6bee2..afe25d4eebf 100644 --- a/proxy/http2/HPACK.cc +++ b/proxy/http2/HPACK.cc @@ -99,6 +99,35 @@ using TS_HPACK_STATIC_TABLE_ENTRY = enum { TS_HPACK_STATIC_TABLE_ENTRY_NUM }; +constexpr int HPACK_STATIC_TABLE_OFFSET[26] = { + TS_HPACK_STATIC_TABLE_ACCEPT_CHARSET, + TS_HPACK_STATIC_TABLE_ENTRY_NUM, + TS_HPACK_STATIC_TABLE_CACHE_CONTROL, + TS_HPACK_STATIC_TABLE_DATE, + TS_HPACK_STATIC_TABLE_ETAG, + TS_HPACK_STATIC_TABLE_FROM, + TS_HPACK_STATIC_TABLE_ENTRY_NUM, + TS_HPACK_STATIC_TABLE_HOST, + TS_HPACK_STATIC_TABLE_IF_MATCH, + TS_HPACK_STATIC_TABLE_ENTRY_NUM, + TS_HPACK_STATIC_TABLE_ENTRY_NUM, + TS_HPACK_STATIC_TABLE_LAST_MODIFIED, + TS_HPACK_STATIC_TABLE_MAX_FORWARDS, + TS_HPACK_STATIC_TABLE_ENTRY_NUM, + TS_HPACK_STATIC_TABLE_ENTRY_NUM, + TS_HPACK_STATIC_TABLE_PROXY_AUTHENTICATE, + TS_HPACK_STATIC_TABLE_ENTRY_NUM, + TS_HPACK_STATIC_TABLE_RANGE, + TS_HPACK_STATIC_TABLE_SERVER, + TS_HPACK_STATIC_TABLE_TRANSFER_ENCODING, + TS_HPACK_STATIC_TABLE_USER_AGENT, + TS_HPACK_STATIC_TABLE_VARY, + TS_HPACK_STATIC_TABLE_WWW_AUTHENTICATE, + TS_HPACK_STATIC_TABLE_ENTRY_NUM, + TS_HPACK_STATIC_TABLE_ENTRY_NUM, + TS_HPACK_STATIC_TABLE_ENTRY_NUM, +}; + constexpr HpackHeaderField STATIC_TABLE[] = {{"", ""}, {":authority", ""}, {":method", "GET"}, @@ -266,7 +295,20 @@ namespace HpackStaticTable { HpackLookupResult result; - for (unsigned int index = 1; index < TS_HPACK_STATIC_TABLE_ENTRY_NUM; ++index) { + // Limit the search range of static table + unsigned int start = 1; + unsigned int end = TS_HPACK_STATIC_TABLE_ENTRY_NUM; + if ('a' <= header.name[0] && header.name[0] <= 'z') { + start = HPACK_STATIC_TABLE_OFFSET[header.name[0] - 'a']; + if ('z' != header.name[0]) { + // This does not always set the ideal end index but works for some cases + end = HPACK_STATIC_TABLE_OFFSET[header.name[0] - 'a' + 1]; + } + } else if (':' == header.name[0]) { + end = HPACK_STATIC_TABLE_OFFSET[0]; + } + + for (unsigned int index = start; index < end; ++index) { // Profiling showed that use of const reference here is more performant than copying string_views. const std::string_view &name = STATIC_TABLE[index].name; const std::string_view &value = STATIC_TABLE[index].value; From ff0a06cbfe92054f8efebd8ea785b7b69d52f5fa Mon Sep 17 00:00:00 2001 From: Masakazu Kitajo Date: Fri, 20 Jan 2023 12:34:13 -0700 Subject: [PATCH 2/3] Add a comment --- proxy/http2/HPACK.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy/http2/HPACK.cc b/proxy/http2/HPACK.cc index afe25d4eebf..582ebc7c85b 100644 --- a/proxy/http2/HPACK.cc +++ b/proxy/http2/HPACK.cc @@ -296,7 +296,7 @@ namespace HpackStaticTable HpackLookupResult result; // Limit the search range of static table - unsigned int start = 1; + unsigned int start = 1; // First effective index for TS_HPACK_STATIC_TABLE_ENTRY is 1 unsigned int end = TS_HPACK_STATIC_TABLE_ENTRY_NUM; if ('a' <= header.name[0] && header.name[0] <= 'z') { start = HPACK_STATIC_TABLE_OFFSET[header.name[0] - 'a']; From 80c7e60a8fd65246e6f69c6e02584e5a6ff1447c Mon Sep 17 00:00:00 2001 From: Masakazu Kitajo Date: Fri, 20 Jan 2023 12:34:20 -0700 Subject: [PATCH 3/3] Have a temporal variable --- proxy/http2/HPACK.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/proxy/http2/HPACK.cc b/proxy/http2/HPACK.cc index 582ebc7c85b..57c53fe8857 100644 --- a/proxy/http2/HPACK.cc +++ b/proxy/http2/HPACK.cc @@ -298,13 +298,13 @@ namespace HpackStaticTable // Limit the search range of static table unsigned int start = 1; // First effective index for TS_HPACK_STATIC_TABLE_ENTRY is 1 unsigned int end = TS_HPACK_STATIC_TABLE_ENTRY_NUM; - if ('a' <= header.name[0] && header.name[0] <= 'z') { - start = HPACK_STATIC_TABLE_OFFSET[header.name[0] - 'a']; - if ('z' != header.name[0]) { + if (const auto c = header.name[0]; 'a' <= c && c <= 'z') { + start = HPACK_STATIC_TABLE_OFFSET[c - 'a']; + if ('z' != c) { // This does not always set the ideal end index but works for some cases - end = HPACK_STATIC_TABLE_OFFSET[header.name[0] - 'a' + 1]; + end = HPACK_STATIC_TABLE_OFFSET[c - 'a' + 1]; } - } else if (':' == header.name[0]) { + } else if (':' == c) { end = HPACK_STATIC_TABLE_OFFSET[0]; }