Look content types up in a plain Hash instead of an indifferent one - #2883
Open
ericproulx wants to merge 1 commit into
Open
Look content types up in a plain Hash instead of an indifferent one#2883ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
`PrecomputedContentTypes#content_type_for` answers what media type a format
maps to, and backed that with a `HashWithIndifferentAccess`:
def content_type_for(format)
content_types_indifferent_access[format]
end
Every read therefore ran `convert_key` before the lookup, because the callers
disagree on spelling: the format negotiated from a path extension, a `?format=`
or an `Accept` header arrives as a String, while the API's declared format and
the value stashed in `api.format` are Symbols.
Both spellings can be present in the same Hash instead. It is built once per
middleware and never written again, so the conversion is work that only ever
had one answer.
HWIA[:json] 42.3 ns -> Hash[:json] 25.5 ns 1.66x
HWIA['json'] 38.8 ns -> Hash['json'] 33.7 ns 1.15x
`content_type_for` is called twice on a request — once to negotiate the format,
once to set the response content type — and three times when the path carries a
format extension. That is about 34 ns per request, roughly 0.6% of a minimal
one. End to end it sits below what the version_throughput harness can resolve,
so no end-to-end figure is claimed here. Allocation counts are unchanged:
`convert_key` used `Symbol#name`, which allocates nothing.
Keys arrive as Symbols — the `content_type` DSL calls `key.to_sym` and the
defaults are Symbols — but the key is also stored as it came, so a middleware
constructed directly with String keys still answers to either spelling, as the
indifferent hash did. Checked against `HashWithIndifferentAccess` over the
default content types plus unknown, nil and empty keys, and over a string-keyed
hash looked up both ways: same answer every time.
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
ericproulx
force-pushed
the
perf/content-type-lookup
branch
from
September 1, 2026 21:13
3739314 to
4b95a6b
Compare
Danger ReportNo issues found. |
Member
|
This feels like an over-optimization but ok. |
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.
PrecomputedContentTypes#content_type_foranswers what media type a format maps to, and backed that with aHashWithIndifferentAccess:Every read ran
convert_keybefore the lookup, because the callers genuinely disagree on spelling — a format negotiated from a path extension, a?format=or anAcceptheader arrives as a String, while the API's declared format and the value stashed inapi.formatare Symbols.Both spellings can live in the same Hash instead. It is built once per middleware and never written again, so that conversion is work with only one possible answer.
Measurements
Same-process A/B on the lookup:
[:json]['json']content_type_forruns twice per request — once to negotiate the format, once to set the response content type — and three times when the path carries a format extension (measured by instrumenting the method).That is about 34 ns per request, roughly 0.6% of a minimal one. End to end this is below what the harness can resolve — repeated runs straddle zero — so no end-to-end figure is claimed. Allocation counts are unchanged:
convert_keyusedSymbol#name, which allocates nothing.Equivalence
Keys arrive as Symbols —
DSL::RequestResponse#content_typecallskey.to_sym, andContentTypes::DEFAULTSare Symbols — but the key is stored as it came as well, so a middleware constructed directly with String keys still answers to either spelling, exactly as the indifferent hash did.Checked against
HashWithIndifferentAccessover the default content types plus unknown,niland empty keys (16 probes, identical answers), and over a string-keyed hash looked up both ways:Suite green (2652), RuboCop clean.
🤖 Generated with Claude Code