Skip to content

Commit 3a80591

Browse files
authored
perf: http/1 use table for lower case conversion (#231)
1 parent 67ca72c commit 3a80591

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

source/common/http/http1/codec_impl.cc

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,28 +244,36 @@ http_parser_settings ConnectionImpl::settings_{
244244
nullptr // on_chunk_complete
245245
};
246246

247-
ConnectionImpl::ConnectionImpl(Network::Connection& connection, http_parser_type type)
248-
: connection_(connection) {
249-
http_parser_init(&parser_, type);
250-
parser_.data = this;
247+
const ConnectionImpl::ToLowerTable ConnectionImpl::to_lower_table_;
248+
249+
ConnectionImpl::ToLowerTable::ToLowerTable() {
250+
for (size_t c = 0; c < 256; c++) {
251+
table_[c] = c;
252+
if ((c >= 'A') && (c <= 'Z')) {
253+
table_[c] |= 0x20;
254+
}
255+
}
251256
}
252257

253-
static void toLowerCase(HeaderString& text) {
258+
void ConnectionImpl::ToLowerTable::toLowerCase(HeaderString& text) const {
254259
char* buffer = text.buffer();
255260
uint32_t size = text.size();
256261
for (size_t i = 0; i < size; i++) {
257-
char c = buffer[i];
258-
if ((c >= 'A') && (c <= 'Z')) {
259-
buffer[i] |= 0x20;
260-
}
262+
buffer[i] = table_[static_cast<uint8_t>(buffer[i])];
261263
}
262264
}
263265

266+
ConnectionImpl::ConnectionImpl(Network::Connection& connection, http_parser_type type)
267+
: connection_(connection) {
268+
http_parser_init(&parser_, type);
269+
parser_.data = this;
270+
}
271+
264272
void ConnectionImpl::completeLastHeader() {
265273
conn_log_trace("completed header: key={} value={}", connection_, current_header_field_.c_str(),
266274
current_header_value_.c_str());
267275
if (!current_header_field_.empty()) {
268-
toLowerCase(current_header_field_);
276+
to_lower_table_.toLowerCase(current_header_field_);
269277
current_header_map_->addViaMove(std::move(current_header_field_),
270278
std::move(current_header_value_));
271279
}

source/common/http/http1/codec_impl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ class ConnectionImpl : public virtual Connection, protected Logger::Loggable<Log
148148
private:
149149
enum class HeaderParsingState { Field, Value, Done };
150150

151+
struct ToLowerTable {
152+
ToLowerTable();
153+
void toLowerCase(HeaderString& text) const;
154+
155+
std::array<uint8_t, 256> table_;
156+
};
157+
151158
/**
152159
* Called in order to complete an in progress header decode.
153160
*/
@@ -219,6 +226,7 @@ class ConnectionImpl : public virtual Connection, protected Logger::Loggable<Log
219226
virtual void sendProtocolError() PURE;
220227

221228
static http_parser_settings settings_;
229+
static const ToLowerTable to_lower_table_;
222230

223231
HeaderMapImplPtr current_header_map_;
224232
HeaderParsingState header_parsing_state_{HeaderParsingState::Field};

0 commit comments

Comments
 (0)