Speed up static route matching - #2940
Merged
Merged
Conversation
Danger ReportNo issues found. |
ericproulx
force-pushed
the
perf/static-route-matching
branch
from
September 12, 2026 21:16
99470ec to
2821214
Compare
dblock
approved these changes
Sep 12, 2026
dblock
left a comment
Member
There was a problem hiding this comment.
I am not convinced these optimizations are worth the complexity of code, but since it's AI-generated I can live with it.
3 tasks
Contributor
Author
|
ericproulx
force-pushed
the
perf/static-route-matching
branch
from
September 13, 2026 08:45
2821214 to
5c1449e
Compare
ericproulx
force-pushed
the
perf/static-route-matching
branch
from
September 13, 2026 17:13
5c1449e to
28da1fa
Compare
6 tasks
ericproulx
added this pull request to stack #2944
September 13, 2026 19:11
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Every direct route request currently runs through the method’s compiled
Regexp.unionand then scans the registered routes to find the capture group that matched. For APIs with many routes, a request for a literal path therefore pays work proportional to route registration order even though its result is known once the router is compiled.What changes
Grape::Router#compile!now builds a frozen per-method lookup table for paths a route spells out in full. At request time, an exact lookup can skip both the union match and the route scan.Keeping other requests as fast as before
The table lookup is extra work for every request it cannot answer, so the matching path was restructured to pay for it:
#transactionand#match?read the pair through one lookup. They used to read@optimized_mapand@mapseparately, so a request that misses the table does no more Hash lookups than it did before this change.Benchmarks
Same process, Ruby 4.0.6, no JIT, 7 rounds with the variants' order rotated, median change against master:
GET /api/v1/hello/42/hello/:idGET /api/v1/hello/42/hello,/hello/:idGET /api/v1/hello/helloGET /api/v1/hello/hello,/hello/:idDifferences under ~1% are within this harness's noise: the literal-path rows run identical code in both versions and still measured −1.0% and +0.3%. On the last of 200 literal routes, the lookup takes a request from ~17,700 to ~157,800 i/s without a JIT.
Behaviour preserved
ANYroutes, auto-OPTIONS, greedy-neighbour matching, and all non-static paths retain the existing flow.Coverage
The router specs exercise the behavior-sensitive cases:
/:idroute still selects that earlier route and receives fresh:idvalues on successive requests.:versioncapture.Validation
bundle exec rubocop lib/grape/router.rb spec/grape/router_spec.rbbundle exec rspec— 2,864 examples, 0 failuresbundle exec rakewas attempted; its global RuboCop scan is blocked locally by pre-existing ignored benchmark artifacts undertmp/with unrelated offenses.