-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Micro-optimizations for Decompressor and Context #6
Conversation
7cd6f20
to
e2c1b82
Compare
Wow, amazing!! I've rebased your PR on top of a working CI, which includes external tests from I'd love for you to add benchmarks to For benchmark timing, I recommend you avoid |
e2c1b82
to
3460b0f
Compare
lib/protocol/hpack/context.rb
Outdated
NEVER_INDEXED_TYPE = {prefix: 4, pattern: 0x10}.freeze | ||
CHANGE_TABLE_SIZE_TYPE = {prefix: 5, pattern: 0x20}.freeze | ||
INCREMENTAL_TYPE = {prefix: 6, pattern: 0x40}.freeze | ||
INDEXED_TYPE = {prefix: 7, pattern: 0x80}.freeze | ||
HEADER_REPRESENTATION = { | ||
indexed: {prefix: 7, pattern: 0x80}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to use the constants defined above, here?
e.g.
indexed: {prefix: 7, pattern: 0x80}, | |
indexed: INDEXED_TYPE, |
?
lib/protocol/hpack/context.rb
Outdated
@@ -296,6 +301,11 @@ def add_to_table(command) | |||
command.freeze | |||
|
|||
@table.unshift(command) | |||
@cursize += entry_size(command) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nit, if we are going to introduce this, can we rename it to @current_table_size
? I'm okay to do this in a separate PR if you want to keep this PR hygienic.
type = nil | ||
|
||
|
||
case (pattern & MASK_SHIFT_4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a brief comment explaining how the lookup here works, and where the values are coming from?
3460b0f
to
f0073f9
Compare
f0073f9
to
5ce3ef9
Compare
lib/protocol/hpack/decompressor.rb
Outdated
header[:type] = :indexed | ||
type = INDEXED_TYPE | ||
else | ||
raise CompressionError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are missing coverage for this line - can you add a test for the failure case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never mind, this failure case is impossible (see comment)
938fbd6
to
2db0328
Compare
2db0328
to
8cae78d
Compare
Addressed comments, I'll add a benchmarking script in a follow-up! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, fantastic work.
We found
current_table_size
,Decompressor#read_header
andContext#decode
to be expensive functions while profiling code that usedprotocol-http2
. Microbenchmarks show a ~15+% improvement in decoding performance.Types of Changes
Contribution
We unrolled the loop in
read_header
to acase-when
block. Specifically using constants for thewhen
clauses so Ruby uses a hash lookup. We also remove some unnecessary hash lookups. We also do bookkeeping for the current table size instead of recomputing.The changes passed all tests locally. For benchmarking we used this slightly modified version of the rfc7541 test case
cc @froydnj