Skip to content

Commit d30b780

Browse files
ericproulxdblock
authored andcommitted
Regex string allocation (#1943)
Use Regexp instead of // for regex in router.rb for a more efficient string allocation.
1 parent afcca35 commit d30b780

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Your contribution here.
66
* [#1944](https://github.com/ruby-grape/grape/pull/1944): Reduced attribute_translator string allocations - [@ericproulx](https://github.com/ericproulx).
7+
* [#1943](https://github.com/ruby-grape/grape/pull/1943): Reduce number of regex string allocations - [@ericproulx](https://github.com/ericproulx).
78
* [#1942](https://github.com/ruby-grape/grape/pull/1942): Optimized retained memory methods - [@ericproulx](https://github.com/ericproulx).
89
* [#1941](https://github.com/ruby-grape/grape/pull/1941): Frozen string literal - [@ericproulx](https://github.com/ericproulx).
910
* [#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

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def compile!
4141
routes = map[method]
4242
@optimized_map[method] = routes.map.with_index do |route, index|
4343
route.index = index
44-
route.regexp = /(?<_#{index}>#{route.pattern.to_regexp})/
44+
route.regexp = Regexp.new("(?<_#{index}>#{route.pattern.to_regexp})")
4545
end
4646
@optimized_map[method] = Regexp.union(@optimized_map[method])
4747
end
@@ -53,7 +53,7 @@ def append(route)
5353
end
5454

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

0 commit comments

Comments
 (0)