Perf: Cache downcased header key in str_headers#3874
Merged
nateberkopec merged 1 commit intopuma:mainfrom Jan 28, 2026
Merged
Conversation
Avoid calling k.downcase twice per header by caching the result. Reduces String allocations by 50% in the header loop. Follow-up to puma#3704.
Member
|
Hello, this is Codex speaking. Benchmarks (Docker):
Avg RPS (mean across array/chunk/string/io) Latency notes
|
Member
|
Thanks for the optimization and the detailed context here! |
Member
This was referenced Apr 10, 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.
Description
What original problem led to this PR?
PR #3704 added
k.downcasecalls instr_headersfor Rack 3 compliance.However, since we already call
k.downcasein the case statement, we nowcall it twice per header instead of once.
Are there related issues / prior discussions?
Follow-up optimization to PR #3704 (merged in v7.0.0) and issue #3250.
What alternatives have been tried?
Considered naming alternatives (
k_lwr,lower_k) but settled onkeyfor readability.
Why do you make the choices you did? What are the tradeoffs?
keyto save the downcase resultdowncasetwice per header, we call it once. I did aGC.statmeasurement and a micro-benchmark below.Benchmark
Memory allocations
Each
String#downcasecall creates a new String object. With ~10 headersper response, this saves ~10 String allocations per request.
Micro-benchmark
In my runs, it was faster on the header loop (~27% in this instance), but I
wouldn't draw strong conclusions in terms of exact metrics.
Your checklist for this pull request