Skip redundant GRAPE_ROUTING_ARGS rewrite when route has no params to merge#2738
Closed
ericproulx wants to merge 1 commit into
Closed
Skip redundant GRAPE_ROUTING_ARGS rewrite when route has no params to merge#2738ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
85cd54e to
ca59681
Compare
Danger ReportNo issues found. |
…to merge
`Router#process_route` previously did:
args = env[Grape::Env::GRAPE_ROUTING_ARGS] || { route_info: route }
route_params = route.params(input)
env[Grape::Env::GRAPE_ROUTING_ARGS] = route_params.blank? ? args : args.merge(route_params)
When `env[GRAPE_ROUTING_ARGS]` was already set and `route_params` was
blank, the ternary returned `args` unchanged and we wrote the same Hash
back to env — a no-op assignment on every cascade match through a route
with no URL placeholders (the common case).
Switch to `||=` for the default, and only re-assign env when there are
new params to merge. Same observable behavior; one fewer env write per
no-param route match.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
ca59681 to
7f88b2a
Compare
Contributor
Author
|
Closing — the perf win (one Hash slot rewrite on cascade-matched no-param routes) is too small to justify the readability regression in either form (the |
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.
Summary
Router#process_routereadsenv[GRAPE_ROUTING_ARGS], computesroute_params, then writes the merged Hash back. Whenenv[GRAPE_ROUTING_ARGS]was already set androute_paramswas blank, the ternary returned the existing Hash unchanged and we wrote the same value back to env — a no-op assignment on every cascade match through a route with no URL placeholders.||=for the default seed and only re-assign env when there are new params to actually merge.Test plan
bundle exec rspec— 2313 examples, 0 failuresbundle exec rubocop lib/grape/router.rb— clean🤖 Generated with Claude Code