Skip to content

Skip redundant GRAPE_ROUTING_ARGS rewrite when route has no params to merge#2738

Closed
ericproulx wants to merge 1 commit into
masterfrom
perf/skip-redundant-routing-args-write
Closed

Skip redundant GRAPE_ROUTING_ARGS rewrite when route has no params to merge#2738
ericproulx wants to merge 1 commit into
masterfrom
perf/skip-redundant-routing-args-write

Conversation

@ericproulx

Copy link
Copy Markdown
Contributor

Summary

  • Router#process_route reads env[GRAPE_ROUTING_ARGS], computes route_params, then writes the merged Hash back. When env[GRAPE_ROUTING_ARGS] was already set and route_params was 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.
  • Use ||= 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 failures
  • bundle exec rubocop lib/grape/router.rb — clean
  • CI green

🤖 Generated with Claude Code

@ericproulx ericproulx force-pushed the perf/skip-redundant-routing-args-write branch from 85cd54e to ca59681 Compare May 23, 2026 16:49
@github-actions

github-actions Bot commented May 23, 2026

Copy link
Copy Markdown

Danger Report

No issues found.

View run

…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>
@ericproulx ericproulx force-pushed the perf/skip-redundant-routing-args-write branch from ca59681 to 7f88b2a Compare May 23, 2026 16:52
@ericproulx

ericproulx commented May 23, 2026

Copy link
Copy Markdown
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 args = env[...] ||= default chord or the duplicated-branch restructure). Leaving master's three-line ternary as-is.

@ericproulx ericproulx closed this May 23, 2026
@ericproulx ericproulx deleted the perf/skip-redundant-routing-args-write branch May 23, 2026 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant