-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache header normalization to reduce object allocation (#789)
Co-authored-by: Alexey Zapparov <[email protected]>
- Loading branch information
Showing
9 changed files
with
161 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
RSpec/ExampleLength: | ||
CountAsOne: | ||
- array | ||
- hash | ||
- heredoc | ||
- method_call | ||
|
||
RSpec/MultipleExpectations: | ||
Max: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ group :test do | |
|
||
gem "rspec", "~> 3.10" | ||
gem "rspec-its" | ||
gem "rspec-memory" | ||
|
||
gem "yardstick" | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
module HTTP | ||
class Headers | ||
class Normalizer | ||
# Matches HTTP header names when in "Canonical-Http-Format" | ||
CANONICAL_NAME_RE = /\A[A-Z][a-z]*(?:-[A-Z][a-z]*)*\z/ | ||
|
||
# Matches valid header field name according to RFC. | ||
# @see http://tools.ietf.org/html/rfc7230#section-3.2 | ||
COMPLIANT_NAME_RE = /\A[A-Za-z0-9!#$%&'*+\-.^_`|~]+\z/ | ||
|
||
NAME_PARTS_SEPARATOR_RE = /[\-_]/ | ||
|
||
# @private | ||
# Normalized header names cache | ||
class Cache | ||
MAX_SIZE = 200 | ||
|
||
def initialize | ||
@store = {} | ||
end | ||
|
||
def get(key) | ||
@store[key] | ||
end | ||
alias [] get | ||
|
||
def set(key, value) | ||
# Maintain cache size | ||
@store.delete(@store.each_key.first) while MAX_SIZE <= @store.size | ||
|
||
@store[key] = value | ||
end | ||
alias []= set | ||
end | ||
|
||
def initialize | ||
@cache = Cache.new | ||
end | ||
|
||
# Transforms `name` to canonical HTTP header capitalization | ||
def call(name) | ||
name = -name.to_s | ||
value = (@cache[name] ||= -normalize_header(name)) | ||
|
||
value.dup | ||
end | ||
|
||
private | ||
|
||
# Transforms `name` to canonical HTTP header capitalization | ||
# | ||
# @param [String] name | ||
# @raise [HeaderError] if normalized name does not | ||
# match {COMPLIANT_NAME_RE} | ||
# @return [String] canonical HTTP header name | ||
def normalize_header(name) | ||
return name if CANONICAL_NAME_RE.match?(name) | ||
|
||
normalized = name.split(NAME_PARTS_SEPARATOR_RE).each(&:capitalize!).join("-") | ||
|
||
return normalized if COMPLIANT_NAME_RE.match?(normalized) | ||
|
||
raise HeaderError, "Invalid HTTP header field name: #{name.inspect}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe HTTP::Headers::Normalizer do | ||
subject(:normalizer) { described_class.new } | ||
|
||
include_context RSpec::Memory | ||
|
||
describe "#call" do | ||
it "normalizes the header" do | ||
expect(normalizer.call("content_type")).to eq "Content-Type" | ||
end | ||
|
||
it "returns a non-frozen string" do | ||
expect(normalizer.call("content_type")).not_to be_frozen | ||
end | ||
|
||
it "evicts the oldest item when cache is full" do | ||
max_headers = (1..described_class::Cache::MAX_SIZE).map { |i| "Header#{i}" } | ||
max_headers.each { |header| normalizer.call(header) } | ||
normalizer.call("New-Header") | ||
cache_store = normalizer.instance_variable_get(:@cache).instance_variable_get(:@store) | ||
expect(cache_store.keys).to eq(max_headers[1..] + ["New-Header"]) | ||
end | ||
|
||
it "retuns mutable strings" do | ||
normalized_headers = Array.new(3) { normalizer.call("content_type") } | ||
|
||
expect(normalized_headers) | ||
.to satisfy { |arr| arr.uniq.size == 1 } | ||
.and(satisfy { |arr| arr.map(&:object_id).uniq.size == normalized_headers.size }) | ||
.and(satisfy { |arr| arr.none?(&:frozen?) }) | ||
end | ||
|
||
it "allocates minimal memory for normalization of the same header" do | ||
normalizer.call("accept") # XXX: Ensure normalizer is pre-allocated | ||
|
||
# On first call it is expected to allocate during normalization | ||
expect { normalizer.call("content_type") }.to limit_allocations( | ||
Array => 1, | ||
MatchData => 1, | ||
String => 6 | ||
) | ||
|
||
# On subsequent call it is expected to only allocate copy of a cached string | ||
expect { normalizer.call("content_type") }.to limit_allocations( | ||
Array => 0, | ||
MatchData => 0, | ||
String => 1 | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters