Make Result#fields return interned strings in Ruby 3+ #1181
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.
Context
These strings are very likely to be used for hash keys. And when using a mutable string as a hash key, ruby will duplicate it and freeze the copy:
This leads to performance patches like this one: rails/rails@a46dcb7
Additionally, on recent rubies, when using a frozen string as a key, Ruby will intern the string to save on memory: msgpack/msgpack-ruby@e8dedef
And finally, since Ruby 3.0,
rb_enc_interned_str
allow to directly retrieve an existing interned string without allocating any object.This patch
Based on the three statements above, I believe that it would be preferable to directly intern the field string if possible, and if not to simply freeze them.
Of course this could cause some minor compatibility issues, but they quite unlikely and trivial to fix.