Skip to content

Commit 600eee9

Browse files
authored
Merge pull request #2011 from ericproulx/retained_regexes
Drop retained regexes
2 parents 7eec4ed + 6d608ce commit 600eee9

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#### Features
44

5-
* Your contribution here.
5+
* [#2011](https://github.com/ruby-grape/grape/pull/2011): Reduce total retained regexes - [@ericproulx](https://github.com/ericproulx).
66

77
#### Fixes
88

lib/grape/router.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ class Router
88
attr_reader :map, :compiled
99

1010
class Any < AttributeTranslator
11-
attr_reader :pattern, :regexp, :index
12-
def initialize(pattern, regexp, index, **attributes)
11+
attr_reader :pattern, :index
12+
def initialize(pattern, index, **attributes)
1313
@pattern = pattern
14-
@regexp = regexp
1514
@index = index
1615
super(attributes)
1716
end
@@ -39,18 +38,20 @@ def self.supported_methods
3938

4039
def initialize
4140
@neutral_map = []
41+
@neutral_regexes = []
4242
@map = Hash.new { |hash, key| hash[key] = [] }
4343
@optimized_map = Hash.new { |hash, key| hash[key] = // }
4444
end
4545

4646
def compile!
4747
return if compiled
48-
@union = Regexp.union(@neutral_map.map(&:regexp))
48+
@union = Regexp.union(@neutral_regexes)
49+
@neutral_regexes = nil
4950
self.class.supported_methods.each do |method|
5051
routes = map[method]
5152
@optimized_map[method] = routes.map.with_index do |route, index|
5253
route.index = index
53-
route.regexp = Regexp.new("(?<_#{index}>#{route.pattern.to_regexp})")
54+
Regexp.new("(?<_#{index}>#{route.pattern.to_regexp})")
5455
end
5556
@optimized_map[method] = Regexp.union(@optimized_map[method])
5657
end
@@ -62,8 +63,8 @@ def append(route)
6263
end
6364

6465
def associate_routes(pattern, **options)
65-
regexp = Regexp.new("(?<_#{@neutral_map.length}>)#{pattern.to_regexp}")
66-
@neutral_map << Any.new(pattern, regexp, @neutral_map.length, **options)
66+
@neutral_regexes << Regexp.new("(?<_#{@neutral_map.length}>)#{pattern.to_regexp}")
67+
@neutral_map << Any.new(pattern, @neutral_map.length, **options)
6768
end
6869

6970
def call(env)

lib/grape/router/route.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Route
1212
SOURCE_LOCATION_REGEXP = /^(.*?):(\d+?)(?::in `.+?')?$/.freeze
1313
FIXED_NAMED_CAPTURES = %w[format version].freeze
1414

15-
attr_accessor :pattern, :translator, :app, :index, :regexp, :options
15+
attr_accessor :pattern, :translator, :app, :index, :options
1616

1717
alias attributes translator
1818

0 commit comments

Comments
 (0)