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

Optimize HTTP.header_name #14503

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/http/common.cr
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ module HTTP
{name, value}
end

# Important! These have to be in lexicographic order.
private COMMON_HEADERS = %w(
Accept-Encoding
Accept-Language
Expand Down Expand Up @@ -241,17 +240,17 @@ module HTTP
referer
user-agent
)
.to_h { |header| {header.to_slice, header} }

# :nodoc:
def self.header_name(slice : Bytes) : String
# Check if the header name is a common one.
# If so we avoid having to allocate a string for it.
if slice.size < 20
name = COMMON_HEADERS.bsearch { |string| slice <= string.to_slice }
jgaskins marked this conversation as resolved.
Show resolved Hide resolved
return name if name && name.to_slice == slice
if slice.size < 20 && (name = COMMON_HEADERS[slice]?)
name
else
String.new(slice)
end

String.new(slice)
end

# :nodoc:
Expand Down