Skip to content

Commit

Permalink
Minor performance improvements to HTTP::Cookies (#10488)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Mar 10, 2021
1 parent 7d0f64b commit 9a3d0c4
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/http/cookie.cr
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ module HTTP
end

# Yields each `HTTP::Cookie` in the collection.
def each(&block : Cookie ->)
@cookies.values.each do |cookie|
def each(& : Cookie ->)
@cookies.each_value do |cookie|
yield cookie
end
end
Expand All @@ -367,8 +367,15 @@ module HTTP
# given `HTTP::Headers` instance and returns it. Removes any existing
# `Cookie` headers in it.
def add_request_headers(headers)
headers.delete("Cookie")
headers.add("Cookie", map(&.to_cookie_header).join("; ")) unless empty?
if empty?
headers.delete("Cookie")
else
capacity = sum { |cookie| cookie.name.bytesize + cookie.value.bytesize + 1 }
capacity += (size - 1) * 2
headers["Cookie"] = String.build(capacity) do |io|
join(io, "; ", &.to_cookie_header(io))
end
end

headers
end
Expand Down

0 comments on commit 9a3d0c4

Please sign in to comment.