Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### Features

* [#2770](https://github.com/ruby-grape/grape/pull/2770): Avoid per-entry array allocation in `Request#build_headers` by replacing `each_header.with_object` with a plain block - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

#### Fixes
Expand Down
8 changes: 7 additions & 1 deletion lib/grape/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,19 @@ def make_params
raise Grape::Exceptions::RequestError
end

# Uses a plain `each_header` block instead of `each_header.with_object`:
# `with_object` can only pass the block one value plus the memo, so the
# `k, v` pair would be boxed into a throwaway Array on every header. A
# two-arg block receives `k`/`v` directly and allocates nothing extra.
def build_headers
each_header.with_object(Grape::Util::Header.new) do |(k, v), headers|
headers = Grape::Util::Header.new
each_header do |k, v|
next unless k.start_with? 'HTTP_'

transformed_header = KNOWN_HEADERS.fetch(k) { -k[5..].tr('_', '-').downcase }
headers[transformed_header] = v
end
headers
end
end
end
Loading