Strip the versioner's prefixes with each instead of reduce - #2882
Open
ericproulx wants to merge 1 commit into
Open
Strip the versioner's prefixes with each instead of reduce#2882ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
`Versioner::Path#before` runs on every request of a versioned API and takes the
mount path and prefix off the front of PATH_INFO before looking for a version
segment:
path_info = @prefixes.reduce(path_info) do |pi, path|
pi.start_with?(path) ? pi.delete_prefix(path) : pi
end
`@prefixes` holds at most two entries — a mount path and a prefix — and for an
API with neither it is empty. `Array` does not override `Enumerable#reduce`, so
that list pays the generic accumulator path on every request, which costs more
than the work it is driving.
`each` over the same list, assigning as it goes, is the same operation without
it. Measured in one process:
one prefix reduce 205 ns -> each 89 ns 2.31x
no prefix reduce 102 ns -> each 25 ns 4.02x
It allocates less too, which the timing alone does not show — 3 objects against
1 for identical work, the extra two being the Enumerable machinery rather than
anything the stripping needs:
reduce 3.0 objects
each 1.0 objects
End to end that is 166,376 -> 170,079 req/s on a small path-versioned API
(+2.2%, median of 9 runs interleaved with master's, Ruby 4.0.5 with YJIT), and
two fewer objects allocated per request. An API that declares no version never
builds this middleware and is untouched.
The result is byte-identical: the same prefixes are removed, in the same order,
under the same `start_with?` guard.
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
ericproulx
force-pushed
the
perf/versioner-prefix-each
branch
from
September 1, 2026 20:53
3db482e to
e73118c
Compare
Danger ReportNo issues found. |
dblock
approved these changes
Sep 2, 2026
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.
Versioner::Path#beforeruns on every request of a versioned API and takes the mount path and prefix off the front of PATH_INFO before looking for a version segment:@prefixesholds at most two entries — a mount path and a prefix — and for an API with neither it is empty.Arraydoes not overrideEnumerable#reduce, so that list pays the generic accumulator path on every request, which costs more than the work it is driving. In astackprofrun it was 30% ofbefore.eachover the same list, assigning as it goes, is the same operation without it.Measurements
Same-process A/B on the operation:
@prefixesIt allocates less too, which the timing alone does not show — 3 objects against 1 for identical work, the extra two being Enumerable machinery rather than anything the stripping needs:
End to end, median of 9 runs interleaved with master's, Ruby 4.0.5 + YJIT:
An API that declares no version never builds this middleware and is untouched.
Behaviour
Identical: the same prefixes are removed, in the same order, under the same
start_with?guard.eachreturns the receiver rather than the accumulator, so the stripped value is assigned to the local as it goes.🤖 Generated with Claude Code