Skip to content

Commit e9d4973

Browse files
ericproulxbasjanssen
authored andcommitted
AttributeTranslator optimization (ruby-grape#1944)
Added request_method and requirements has methods instead using method_missing since it's defined by defaults. Added regexp and index in Any class instead of falling back on method_missing
1 parent cb04a5a commit e9d4973

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Features
44

55
* Your contribution here.
6+
* [#1944](https://github.com/ruby-grape/grape/pull/1944): Reduced attribute_translator string allocations - [@ericproulx](https://github.com/ericproulx).
67
* [#1942](https://github.com/ruby-grape/grape/pull/1942): Optimized retained memory methods - [@ericproulx](https://github.com/ericproulx).
78
* [#1941](https://github.com/ruby-grape/grape/pull/1941): Frozen string literal - [@ericproulx](https://github.com/ericproulx).
89
* [#1940](https://github.com/ruby-grape/grape/pull/1940): Get rid of a needless step in HashWithIndifferentAccess - [@dnesteryuk](https://github.com/dnesteryuk).

lib/grape/router.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ class Router
77
attr_reader :map, :compiled
88

99
class Any < AttributeTranslator
10-
def initialize(pattern, **attributes)
10+
attr_reader :pattern, :regexp, :index
11+
def initialize(pattern, regexp, index, **attributes)
1112
@pattern = pattern
13+
@regexp = regexp
14+
@index = index
1215
super(attributes)
1316
end
1417
end
@@ -51,7 +54,7 @@ def append(route)
5154

5255
def associate_routes(pattern, **options)
5356
regexp = /(?<_#{@neutral_map.length}>)#{pattern.to_regexp}/
54-
@neutral_map << Any.new(pattern, regexp: regexp, index: @neutral_map.length, **options)
57+
@neutral_map << Any.new(pattern, regexp, @neutral_map.length, **options)
5558
end
5659

5760
def call(env)

lib/grape/router/attribute_translator.rb

+16-8
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,39 @@ module Grape
44
class Router
55
# this could be an OpenStruct, but doesn't work in Ruby 2.3.0, see https://bugs.ruby-lang.org/issues/12251
66
class AttributeTranslator
7+
attr_reader :attributes, :request_method, :requirements
8+
79
def initialize(attributes = {})
810
@attributes = attributes
11+
@request_method = attributes[:request_method]
12+
@requirements = attributes[:requirements]
913
end
1014

1115
def to_h
12-
@attributes
16+
attributes
1317
end
1418

15-
def method_missing(m, *args)
16-
if m[-1] == '='
17-
@attributes[m[0..-1]] = *args
18-
elsif m[-1] != '='
19-
@attributes[m]
19+
def method_missing(method_name, *args) # rubocop:disable Style/MethodMissing
20+
if setter?(method_name[-1])
21+
attributes[method_name[0..-1]] = *args
2022
else
21-
super
23+
attributes[method_name]
2224
end
2325
end
2426

2527
def respond_to_missing?(method_name, _include_private = false)
26-
if method_name[-1] == '='
28+
if setter?(method_name[-1])
2729
true
2830
else
2931
@attributes.key?(method_name)
3032
end
3133
end
34+
35+
private
36+
37+
def setter?(method_name)
38+
method_name[-1] == '='
39+
end
3240
end
3341
end
3442
end

0 commit comments

Comments
 (0)