Skip to content

Commit 018941e

Browse files
lizhedblock
authored andcommitted
Avoid unnecessary object allocation.
1 parent 85ed0ff commit 018941e

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lib/grape/http/request.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
module Grape
22
class Request < Rack::Request
3+
ROUTING_ARGS = 'rack.routing_args'
4+
HTTP_PREFIX = 'HTTP_'
5+
UNDERSCORE = '_'
6+
MINUS = '-'
7+
38
def params
49
@params ||= begin
510
params = Hashie::Mash.new(super)
6-
if env['rack.routing_args']
7-
args = env['rack.routing_args'].dup
11+
if env[ROUTING_ARGS]
12+
args = env[ROUTING_ARGS].dup
813
# preserve version from query string parameters
914
args.delete(:version)
1015
args.delete(:route_info)
@@ -16,8 +21,11 @@ def params
1621

1722
def headers
1823
@headers ||= env.dup.inject({}) do |h, (k, v)|
19-
if k.to_s.start_with? 'HTTP_'
20-
k = k[5..-1].tr('_', '-').downcase.gsub(/^.|[-_\s]./) { |x| x.upcase }
24+
if k.to_s.start_with? HTTP_PREFIX
25+
k = k[5..-1]
26+
k.tr!(UNDERSCORE, MINUS)
27+
k.downcase!
28+
k.gsub!(/^.|[-_\s]./, &:upcase!)
2129
h[k] = v
2230
end
2331
h

lib/grape/util/stackable_values.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ def initialize(inherited_values = {})
1313

1414
def [](name)
1515
return @froozen_values[name] if @froozen_values.key? name
16-
[@inherited_values[name], @new_values[name]].compact.flatten(1)
16+
value = [@inherited_values[name], @new_values[name]]
17+
value.compact!
18+
value.flatten!(1)
19+
value
1720
end
1821

1922
def []=(name, value)

0 commit comments

Comments
 (0)