Repository navigation
Use Symbol#name for session key stringification - #59102
Merged
Merged
Conversation
The session accessors call key.to_s on every access. Virtually every caller passes a symbol (session[:user_id], the CSRF token, the flash), and Symbol#to_s allocates a new String on every call on every released Ruby from 3.3 to 4.0. Symbol#name returns the symbol's interned frozen string instead, which is also exactly what Hash#[]= would have made of the key. Factor the conversion into a private stringify_key and use it at all seven call sites. String keys are unaffected: String#to_s returns self. Measured on Ruby 3.4.11 for the key conversion plus the hash lookup it feeds: allocations per access 1 -> 0 5,000,000 accesses 0.46s -> 0.30s Co-Authored-By: Claude Fable 5.1 <[email protected]>
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.
Motivation / Background
The session accessors call
key.to_son every access. Virtually every caller passes a symbol (session[:user_id], the CSRF token, the flash), andSymbol#to_sallocates a newStringon every call on every released Ruby from 3.3 to 4.0.Symbol#namereturns the symbol's interned frozen string instead, which is also exactly whatHash#[]=would have made of the key.Detail
The conversion is factored into a private
stringify_keyand used at all seven call sites:[],[]=,dig,has_key?,deleteand both arms offetch. String keys are unaffected, sinceString#to_sreturns self.A test pins that symbol and string keys address the same entry through writes, reads,
key?anddelete, which onlyfetchanddigcovered before.Additional information
Measured on Ruby 3.4.11 for the key conversion plus the hash lookup it feeds:
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]🤖 Generated with Claude Code