Replace per-request Proc in Router#transaction with a helper#2692
Open
ericproulx wants to merge 1 commit intomasterfrom
Open
Replace per-request Proc in Router#transaction with a helper#2692ericproulx wants to merge 1 commit intomasterfrom
ericproulx wants to merge 1 commit intomasterfrom
Conversation
The `cascade_or_return_response` proc inside `Router#transaction` was
allocated on every request because it relied on non-local `return` to
exit the enclosing method when a non-cascading response arrived. That
behavior was doing two things at once — closing the cascading body as
a side effect, and halting transaction processing.
Split them: a plain `halt?(response)` helper returns `true` when the
response is final (and should be returned as-is), `false` when it's
either absent or cascading; closing the body lives inside the helper
as a side effect. The `transaction` method then uses explicit
early-returns for the halt case and plain assignments for the cascade
flag.
## Benchmark (1k iterations, isolated proc shape)
current variant speedup alloc
cascading response 3.12 M/s 6.88 M/s 2.21x 1 → 0 objects
hit response (final) 3.07 M/s 9.82 M/s 3.20x 1 → 0 objects
nil response 5.07 M/s 15.7 M/s 3.10x 1 → 0 objects
Saves one proc allocation per request in the router's `transaction`
dispatch — plus 2–3x faster control flow in the cascade-decision step.
No behavior change; all 2,236 specs pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
548846f to
396ead4
Compare
Danger ReportNo issues found. |
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#transactionallocated acascade_or_return_responseproc on every request. The proc had to live inside the method because it relied on non-localreturnto exittransactionwhen a non-cascading response arrived — the comment on top of the method calls this out explicitly. That proc was doing two things at once: closing the cascading body as a side effect, and halting transaction processing.Split them. A plain
halt?(response)helper answers "should this response halt processing?" — returningtruefor a final (non-cascading) response andfalsewhen the response is absent or cascading; closing the cascading body happens inside the helper as a side effect.transactionthen uses explicit early-returns for the halt case and plain assignments for the cascade flag. No proc needed; behavior identical.Perf / Benchmarks
Isolated microbenchmark of the cascade-decision shape, 1,000 iterations:
One proc allocation saved per request on the router dispatch path.
Test plan
bundle exec rspec— 2,236 examples, 0 failures.bundle exec rubocop lib/grape/router.rb— clean.🤖 Generated with Claude Code