Skip to content

Commit

Permalink
Regex string allocation (#1943)
Browse files Browse the repository at this point in the history
Use Regexp instead of // for regex in router.rb for a more efficient string allocation.
  • Loading branch information
ericproulx authored and dblock committed Dec 16, 2019
1 parent afcca35 commit d30b780
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Your contribution here.
* [#1944](https://github.com/ruby-grape/grape/pull/1944): Reduced attribute_translator string allocations - [@ericproulx](https://github.com/ericproulx).
* [#1943](https://github.com/ruby-grape/grape/pull/1943): Reduce number of regex string allocations - [@ericproulx](https://github.com/ericproulx).
* [#1942](https://github.com/ruby-grape/grape/pull/1942): Optimized retained memory methods - [@ericproulx](https://github.com/ericproulx).
* [#1941](https://github.com/ruby-grape/grape/pull/1941): Frozen string literal - [@ericproulx](https://github.com/ericproulx).
* [#1940](https://github.com/ruby-grape/grape/pull/1940): Get rid of a needless step in HashWithIndifferentAccess - [@dnesteryuk](https://github.com/dnesteryuk).
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def compile!
routes = map[method]
@optimized_map[method] = routes.map.with_index do |route, index|
route.index = index
route.regexp = /(?<_#{index}>#{route.pattern.to_regexp})/
route.regexp = Regexp.new("(?<_#{index}>#{route.pattern.to_regexp})")
end
@optimized_map[method] = Regexp.union(@optimized_map[method])
end
Expand All @@ -53,7 +53,7 @@ def append(route)
end

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

Expand Down

0 comments on commit d30b780

Please sign in to comment.