Add optimized instructions for frozen literal Hash and Array#11406
Merged
byroot merged 2 commits intoruby:masterfrom Sep 5, 2024
Merged
Add optimized instructions for frozen literal Hash and Array#11406byroot merged 2 commits intoruby:masterfrom
byroot merged 2 commits intoruby:masterfrom
Conversation
|
Try on Playground: https://ruby.github.io/play-ruby?run=10718146951 |
If an Array which is empty or only using literals is frozen, we detect this as a peephole optimization and change the instructions to be `opt_ary_freeze`. [Feature #20684] Co-authored-by: Jean Boussier <byroot@ruby-lang.org>
If a Hash which is empty or only using literals is frozen, we detect this as a peephole optimization and change the instructions to be `opt_hash_freeze`. [Feature #20684] Co-authored-by: Jean Boussier <byroot@ruby-lang.org>
auto-merge was automatically disabled
September 5, 2024 09:58
Head branch was pushed to by a user without write access
af44d24 to
b348c96
Compare
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.
[Feature #20684]
Context
Methods that take empty arrays or empty hashes as default values allocate a new object each time the method is called without the argument. Often they don't mutate the parameter. To prevent an allocation, in performance critical sections, a constant is defined that holds a frozen hash or array, and the constant is defined as the default value for the parameter.
Here are some examples:
Rails: https://github.com/rails/rails/blob/607d61e884237c223c24c6f47efa0b561dd8b637/activerecord/lib/active_record/relation/query_methods.rb#L159-L160
Roda: https://github.com/jeremyevans/roda/blob/102926a02dcabc9a31674e3cf98f049139c31492/lib/roda/plugins.rb#L9-L10
dry-rb: https://github.com/dry-rb/dry-container/blob/1ee41bb109455d06bf22ebcbd94b050cc4773733/lib/dry/container/mixin.rb#L68C5-L68C15
and many other gems: https://gist.github.com/casperisfine/47f22243d4ad203855256ef5bfae7979
Additionally when defining a frozen literal constant, we're currently inefficient because we store the literal in the bytecode, we dup it just to freeze it again. It doesn't amount to much but would be nice to avoid.
Proposal
This PRs introduces 2 new optimized instructions
opt_ary_freezeandopt_hash_freezethat behave likeopt_str_freezefor their respective types. If the freeze method hasn't been redefined, they simply push the frozen literal value on the stack. Like foropt_str_freeze, these instructions are added by the peephole optimizer when applicable.In the specific case of empty array and empty hash, we use a pre-allocated global empty frozen object to avoid retaining a distinct empty object each time.
This will allow code like this: https://github.com/ruby/ruby/blob/566f2eb501d94d4047a9aad4af0d74c6a96f34a9/lib/rubygems/resolver/api_set/gem_parser.rb to be shortened and simplified like this:
Overall it's a minor optimization but also a very simple patch and makes code nicer.