Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop retained regexes #2011

Merged
merged 1 commit into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#### Features

* Your contribution here.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually keep those.

* [#2011](https://github.com/ruby-grape/grape/pull/2011): Reduce total retained regexes - [@ericproulx](https://github.com/ericproulx).

#### Fixes

Expand Down
15 changes: 8 additions & 7 deletions lib/grape/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ class Router
attr_reader :map, :compiled

class Any < AttributeTranslator
attr_reader :pattern, :regexp, :index
def initialize(pattern, regexp, index, **attributes)
attr_reader :pattern, :index
def initialize(pattern, index, **attributes)
@pattern = pattern
@regexp = regexp
@index = index
super(attributes)
end
Expand Down Expand Up @@ -39,18 +38,20 @@ def self.supported_methods

def initialize
@neutral_map = []
@neutral_regexes = []
@map = Hash.new { |hash, key| hash[key] = [] }
@optimized_map = Hash.new { |hash, key| hash[key] = // }
end

def compile!
return if compiled
@union = Regexp.union(@neutral_map.map(&:regexp))
@union = Regexp.union(@neutral_regexes)
@neutral_regexes = nil
self.class.supported_methods.each do |method|
routes = map[method]
@optimized_map[method] = routes.map.with_index do |route, index|
route.index = index
route.regexp = Regexp.new("(?<_#{index}>#{route.pattern.to_regexp})")
Regexp.new("(?<_#{index}>#{route.pattern.to_regexp})")
end
@optimized_map[method] = Regexp.union(@optimized_map[method])
end
Expand All @@ -62,8 +63,8 @@ def append(route)
end

def associate_routes(pattern, **options)
regexp = Regexp.new("(?<_#{@neutral_map.length}>)#{pattern.to_regexp}")
@neutral_map << Any.new(pattern, regexp, @neutral_map.length, **options)
@neutral_regexes << Regexp.new("(?<_#{@neutral_map.length}>)#{pattern.to_regexp}")
@neutral_map << Any.new(pattern, @neutral_map.length, **options)
end

def call(env)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Route
SOURCE_LOCATION_REGEXP = /^(.*?):(\d+?)(?::in `.+?')?$/.freeze
FIXED_NAMED_CAPTURES = %w[format version].freeze

attr_accessor :pattern, :translator, :app, :index, :regexp, :options
attr_accessor :pattern, :translator, :app, :index, :options

alias attributes translator

Expand Down