Repository navigation
Convert Mash symbol keys with Symbol#name - #613
Merged
Merged
Conversation
Mash#convert_key turns every symbol key into a string with Symbol#to_s, which allocates a new String on each read and write. Symbol#name, on Ruby 3.0+, returns the symbol's interned frozen string instead, which is also what Hash#[]= would have stored for the key. Older Rubies keep using Symbol#to_s.
Contributor
Danger ReportNo issues found. |
Member
|
Thanks, merging. Is it time to drop support for Ruby < 3? |
Contributor
Author
|
Thanks, yeah, it's probably about time to drop 2.7. |
Member
|
Following up on #613 (comment): opened #622 to drop Ruby 2.7 support and require Ruby 3.0. It removes obsolete version-gated method definitions and Symbol#name fallbacks, updates CI and docs, and runs the relevant specs unconditionally on supported Rubies. — Copilot, an AI posting on behalf of @dblock. |
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.
Mash#convert_keyturns every symbol key into a string withSymbol#to_s, andSymbol#to_sallocates a newStringon each call. So everymash[:key],mash.key?(:key),fetch,deleteand write with a symbol key allocates one.On Ruby 3.0+ this uses
Symbol#nameinstead, which returns the symbol's interned frozen string. That's also whatHash#[]=would have stored for the key, so the stored keys and lookups are the same as before. Ruby 2.7 doesn't haveSymbol#nameand keeps usingSymbol#to_s. String and other keys are unchanged.Rails made the same change for session keys in rails/rails#59102.
Measured on Ruby 3.3.5:
[]+key?+fetchwith symbol keysIt shows up in gems that read Mash options on every request. In OmniAuth, where strategy options are a
Mash, a request that isn't an auth request allocated 163 objects with three strategies before this and 115 after.The new specs check that symbol keys reach the same entries as their string names through writes, reads,
key?,fetch,values_atanddelete, and that reading with a symbol key doesn't allocate on CRuby 3.0+.