Skip to content

Commit 526d3d1

Browse files
committed
Remove each_with_object where it's slow.
Remember #1128? Unfortunately, calling `each_with_object` on `Hash` is not a good idea (`inject` has the same problem btw). It allocates an array for each `(k,v)` pair which is not good. More details here: rails/rails@960de47 @tenderlove excellently covered it all.
1 parent 6e2fc02 commit 526d3d1

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

lib/grape/formatter/serializable_hash.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ def serialize(object)
2121
elsif object.is_a?(Array) && !object.map { |o| o.respond_to? :serializable_hash }.include?(false)
2222
object.map(&:serializable_hash)
2323
elsif object.is_a?(Hash)
24-
object.each_with_object({}) do |(k, v), h|
24+
h = {}
25+
object.each_pair do |k, v|
2526
h[k] = serialize(v)
2627
end
28+
h
2729
else
2830
object
2931
end

lib/grape/middleware/base.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ def content_type
5555
end
5656

5757
def mime_types
58-
content_types.each_with_object({}) do |(k, v), types_without_params|
58+
types_without_params = {}
59+
content_types.each_pair do |k, v|
5960
types_without_params[v.split(';').first] = k
6061
end
62+
types_without_params
6163
end
6264
end
6365
end

lib/grape/request.rb

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,39 @@ module Grape
22
class Request < Rack::Request
33
HTTP_PREFIX = 'HTTP_'.freeze
44

5+
alias_method :rack_params, :params
6+
57
def params
6-
@params ||= begin
7-
params = Hashie::Mash.new(super)
8-
if env[Grape::Env::RACK_ROUTING_ARGS]
9-
args = env[Grape::Env::RACK_ROUTING_ARGS].dup
10-
# preserve version from query string parameters
11-
args.delete(:version)
12-
args.delete(:route_info)
13-
params.deep_merge!(args)
14-
end
15-
params
16-
end
8+
@params ||= build_params
179
end
1810

1911
def headers
20-
@headers ||= env.each_with_object({}) do |(k, v), h|
12+
@headers ||= build_headers
13+
end
14+
15+
private
16+
17+
def build_params
18+
params = Hashie::Mash.new(rack_params)
19+
if env[Grape::Env::RACK_ROUTING_ARGS]
20+
args = env[Grape::Env::RACK_ROUTING_ARGS].dup
21+
# preserve version from query string parameters
22+
args.delete(:version)
23+
args.delete(:route_info)
24+
params.deep_merge!(args)
25+
end
26+
params
27+
end
28+
29+
def build_headers
30+
headers = {}
31+
env.each_pair do |k, v|
2132
next unless k.to_s.start_with? HTTP_PREFIX
2233

2334
k = k[5..-1].split('_').each(&:capitalize!).join('-')
24-
h[k] = v
35+
headers[k] = v
2536
end
37+
headers
2638
end
2739
end
2840
end

lib/grape/util/strict_hash_configuration.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ def self.nested_settings_methods(setting_name, new_config_class)
6464
end
6565

6666
define_method 'to_hash' do
67-
merge_hash = setting_name.keys.each_with_object({}) { |k, hash| hash[k] = send("#{k}_context").to_hash }
67+
merge_hash = {}
68+
setting_name.each_key { |k| merge_hash[k] = send("#{k}_context").to_hash }
6869

6970
@settings.to_hash.merge(
7071
merge_hash

0 commit comments

Comments
 (0)