Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: http/1 use table for lower case conversion #231

Merged
merged 1 commit into from
Nov 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions source/common/http/http1/codec_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,28 +244,36 @@ http_parser_settings ConnectionImpl::settings_{
nullptr // on_chunk_complete
};

ConnectionImpl::ConnectionImpl(Network::Connection& connection, http_parser_type type)
: connection_(connection) {
http_parser_init(&parser_, type);
parser_.data = this;
const ConnectionImpl::ToLowerTable ConnectionImpl::to_lower_table_;

ConnectionImpl::ToLowerTable::ToLowerTable() {
for (size_t c = 0; c < 256; c++) {
table_[c] = c;
if ((c >= 'A') && (c <= 'Z')) {
table_[c] |= 0x20;
}
}
}

static void toLowerCase(HeaderString& text) {
void ConnectionImpl::ToLowerTable::toLowerCase(HeaderString& text) const {
char* buffer = text.buffer();
uint32_t size = text.size();
for (size_t i = 0; i < size; i++) {
char c = buffer[i];
if ((c >= 'A') && (c <= 'Z')) {
buffer[i] |= 0x20;
}
buffer[i] = table_[static_cast<uint8_t>(buffer[i])];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Been a while since I optimized at this level, but would it help to use restrict on buffer to indicate that it can't overlap with table_ ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea :) I will look into it.

}
}

ConnectionImpl::ConnectionImpl(Network::Connection& connection, http_parser_type type)
: connection_(connection) {
http_parser_init(&parser_, type);
parser_.data = this;
}

void ConnectionImpl::completeLastHeader() {
conn_log_trace("completed header: key={} value={}", connection_, current_header_field_.c_str(),
current_header_value_.c_str());
if (!current_header_field_.empty()) {
toLowerCase(current_header_field_);
to_lower_table_.toLowerCase(current_header_field_);
current_header_map_->addViaMove(std::move(current_header_field_),
std::move(current_header_value_));
}
Expand Down
8 changes: 8 additions & 0 deletions source/common/http/http1/codec_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ class ConnectionImpl : public virtual Connection, protected Logger::Loggable<Log
private:
enum class HeaderParsingState { Field, Value, Done };

struct ToLowerTable {
ToLowerTable();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think default ctor is not necessary here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is. we setup the table in the constructor

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad :)

void toLowerCase(HeaderString& text) const;

std::array<uint8_t, 256> table_;
};

/**
* Called in order to complete an in progress header decode.
*/
Expand Down Expand Up @@ -219,6 +226,7 @@ class ConnectionImpl : public virtual Connection, protected Logger::Loggable<Log
virtual void sendProtocolError() PURE;

static http_parser_settings settings_;
static const ToLowerTable to_lower_table_;

HeaderMapImplPtr current_header_map_;
HeaderParsingState header_parsing_state_{HeaderParsingState::Field};
Expand Down