[ruby-core:127317] [Ruby Feature#22501] Optimize `Hash.new` and `String.new` in `opt_new`
Issue #22501 has been reported by byroot (Jean Boussier). ---------------------------------------- Feature #22501: Optimize `Hash.new` and `String.new` in `opt_new` https://bugs.ruby-lang.org/issues/22501 * Author: byroot (Jean Boussier) * Status: Open ---------------------------------------- ### Context Both `Hash` and `String` initializer have a `capacity:` argument, because in performance sensitive code, correctly pre-sizing containers can have a big performance impact when the final size is large, as it saves both a number of reallocations, but also a number of copying / rehashing. Unfortunately, until now, for smaller capacities, the cost of handling keyword arguments from a C function would tend to outweigh the gains from right-sizing, making it hard to use in cases where the capacity is entirely dynamic and potentially small. Another concern is from Variable Width Allocation. For performance it's preferable to allocate embedded objects when possible, which requires to know the size prior to call `#initialize`, as at that point the slot has already been allocated to the minimum size. Hence the `String.new(capacity:)` / `Hash.new` requires `new` to be a custom `new` method to extract the capacity argument and compute the ideal slot size, but doing so defeat the existing `opt_new` optimization, and cause `String#initialize` / `Hash#initialize` to not be called. ### Prior Art @tenderlovemaking opened [Feature #22130] with the idea of a dedicated instruction for `String.new` but it died off over various concerns, one of which was that adding too many dedicated instruction would risk producing worse code in the interpreter loop. ### Solution So instead with @jhawthorn we thought of a smaller change. - Define `Hash#initialize` in Ruby for more efficient keyword argument handling. - Add a path in the existing `opt_new` to peek the stack for the `capacity:` keyword argument and pass it to the allocator. - Let `opt_new` call `initialize` like it would for the default `new` method. ### Implementation The `Hash.new` optimization was merged as [7142a3debb](https://github.com/ruby/ruby/commit/7142a3debbda9c35bef9921fc59a27225969b979) with some decent gains, and `String.new` is being finalized in [GH-19674](https://github.com/ruby/ruby/pull/19674) ### Concerns However after the `Hash.new` optimization was merged a subtle behavioral difference was noticed. Until now, refining `Hash#initialize` would have no effect when calling `Hash.new`, but in 4.1 the refined method is invoked by the interpreter (but not by JITs): ```ruby module R refine Hash do def initialize(*) raise "Not called prior to 4.1 nor by JITs" end end end using R Hash.new ``` ### Question What is the correct behavior here? Should this refinement work? or not work? Or is it "implementation defined". -- https://bugs.ruby-lang.org/
Issue #22501 has been updated by shugo (Shugo Maeda). The intended behavior is that a refinement of `#initialize` does not affect `new`. That said, I'm fine with making it implementation-defined if that helps optimizations like `opt_new`. ---------------------------------------- Feature #22501: Optimize `Hash.new` and `String.new` in `opt_new` https://bugs.ruby-lang.org/issues/22501#change-119844 * Author: byroot (Jean Boussier) * Status: Open ---------------------------------------- ### Context Both `Hash` and `String` initializer have a `capacity:` argument, because in performance sensitive code, correctly pre-sizing containers can have a big performance impact when the final size is large, as it saves both a number of reallocations, but also a number of copying / rehashing. Unfortunately, until now, for smaller capacities, the cost of handling keyword arguments from a C function would tend to outweigh the gains from right-sizing, making it hard to use in cases where the capacity is entirely dynamic and potentially small. Another concern is from Variable Width Allocation. For performance it's preferable to allocate embedded objects when possible, which requires to know the size prior to call `#initialize`, as at that point the slot has already been allocated to the minimum size. Hence the `String.new(capacity:)` / `Hash.new` requires `new` to be a custom `new` method to extract the capacity argument and compute the ideal slot size, but doing so defeat the existing `opt_new` optimization, and cause `String#initialize` / `Hash#initialize` to not be called. ### Prior Art @tenderlovemaking opened [Feature #22130] with the idea of a dedicated instruction for `String.new` but it died off over various concerns, one of which was that adding too many dedicated instruction would risk producing worse code in the interpreter loop. ### Solution So instead with @jhawthorn we thought of a smaller change. - Define `Hash#initialize` in Ruby for more efficient keyword argument handling. - Add a path in the existing `opt_new` to peek the stack for the `capacity:` keyword argument and pass it to the allocator. - Let `opt_new` call `initialize` like it would for the default `new` method. ### Implementation The `Hash.new` optimization was merged as [7142a3debb](https://github.com/ruby/ruby/commit/7142a3debbda9c35bef9921fc59a27225969b979) with some decent gains, and `String.new` is being finalized in [GH-19674](https://github.com/ruby/ruby/pull/19674) ### Concerns However after the `Hash.new` optimization was merged a subtle behavioral difference was noticed. Until now, refining `Hash#initialize` would have no effect when calling `Hash.new`, but in 4.1 the refined method is invoked by the interpreter (but not by JITs): ```ruby module R refine Hash do def initialize(*) raise "Not called prior to 4.1 nor by JITs" end end end using R Hash.new ``` ### Question What is the correct behavior here? Should this refinement work? or not work? Or is it "implementation defined". -- https://bugs.ruby-lang.org/
Issue #22501 has been updated by Eregon (Benoit Daloze). To state the obvious: at least the interpreter and JIT should be consistent. I think the interesting part is whether normal `Class#new` calls `initialize` with refinements or not. From: ```ruby class Foo end module R refine Foo do def initialize(*) raise "refined" end end end using R p Foo.new ``` Ruby 4.0+ uses refinements, Ruby 3.4 and before do not use refinements. This is probably a side effect of `opt_new`, and I think we should fix it to behave like Ruby 3.4 and before, i.e. ignore refinements for `initialize` called by `Class#new`. Here is one reason to illustrate it's not good to use refinements for `initialize`, it only applies for `Foo.new` but not `Foo.send(:new)`: ``` ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [x86_64-linux] With: p Foo.new ref.rb:7:in 'initialize': refined (RuntimeError) With: p Foo.send(:new) #<Foo:0x00007f0110fbb658> ``` Another reason is the inlining of `new` should be transparent, and e.g. semantically it should be valid to define new in Ruby like ```ruby class Class def new(...) allocate.tap { it.initialize(...) } end end ``` Which clearly wouldn't apply refinements from the caller of `new`. So from what I can see refinements applying to `initialize` seems an accidental side-effect of `opt_new` inlining the call in the bytecode and we should fix it. I'd imagine it's rather easy to fix, while OTOH always using caller refinements is very hard or maybe even impossible (and likely has a performance overhead). ---------------------------------------- Feature #22501: Optimize `Hash.new` and `String.new` in `opt_new` https://bugs.ruby-lang.org/issues/22501#change-119864 * Author: byroot (Jean Boussier) * Status: Open ---------------------------------------- ### Context Both `Hash` and `String` initializer have a `capacity:` argument, because in performance sensitive code, correctly pre-sizing containers can have a big performance impact when the final size is large, as it saves both a number of reallocations, but also a number of copying / rehashing. Unfortunately, until now, for smaller capacities, the cost of handling keyword arguments from a C function would tend to outweigh the gains from right-sizing, making it hard to use in cases where the capacity is entirely dynamic and potentially small. Another concern is from Variable Width Allocation. For performance it's preferable to allocate embedded objects when possible, which requires to know the size prior to call `#initialize`, as at that point the slot has already been allocated to the minimum size. Hence the `String.new(capacity:)` / `Hash.new` requires `new` to be a custom `new` method to extract the capacity argument and compute the ideal slot size, but doing so defeat the existing `opt_new` optimization, and cause `String#initialize` / `Hash#initialize` to not be called. ### Prior Art @tenderlovemaking opened [Feature #22130] with the idea of a dedicated instruction for `String.new` but it died off over various concerns, one of which was that adding too many dedicated instruction would risk producing worse code in the interpreter loop. ### Solution So instead with @jhawthorn we thought of a smaller change. - Define `Hash#initialize` in Ruby for more efficient keyword argument handling. - Add a path in the existing `opt_new` to peek the stack for the `capacity:` keyword argument and pass it to the allocator. - Let `opt_new` call `initialize` like it would for the default `new` method. ### Implementation The `Hash.new` optimization was merged as [7142a3debb](https://github.com/ruby/ruby/commit/7142a3debbda9c35bef9921fc59a27225969b979) with some decent gains, and `String.new` is being finalized in [GH-19674](https://github.com/ruby/ruby/pull/19674) ### Concerns However after the `Hash.new` optimization was merged a subtle behavioral difference was noticed. Until now, refining `Hash#initialize` would have no effect when calling `Hash.new`, but in 4.1 the refined method is invoked by the interpreter (but not by JITs): ```ruby module R refine Hash do def initialize(*) raise "Not called prior to 4.1 nor by JITs" end end end using R Hash.new ``` ### Question What is the correct behavior here? Should this refinement work? or not work? Or is it "implementation defined". -- https://bugs.ruby-lang.org/
Issue #22501 has been updated by byroot (Jean Boussier).
refinements applying to initialize seems an accidental side-effect of opt_new inlining the call to initialize in the bytecode and we should fix it.
Indeed.
I'd imagine it's rather easy to fix,
I assume it means instead of generating a `send` instruction, we need to generate a new one that doesn't consider refinements (or introduced a CALL_ARG) that does so. Conceptually it's "rather easy", but with the interpreter and two JITs, that's quite a bit of work. ---------------------------------------- Feature #22501: Optimize `Hash.new` and `String.new` in `opt_new` https://bugs.ruby-lang.org/issues/22501#change-119880 * Author: byroot (Jean Boussier) * Status: Open ---------------------------------------- ### Context Both `Hash` and `String` initializer have a `capacity:` argument, because in performance sensitive code, correctly pre-sizing containers can have a big performance impact when the final size is large, as it saves both a number of reallocations, but also a number of copying / rehashing. Unfortunately, until now, for smaller capacities, the cost of handling keyword arguments from a C function would tend to outweigh the gains from right-sizing, making it hard to use in cases where the capacity is entirely dynamic and potentially small. Another concern is from Variable Width Allocation. For performance it's preferable to allocate embedded objects when possible, which requires to know the size prior to call `#initialize`, as at that point the slot has already been allocated to the minimum size. Hence the `String.new(capacity:)` / `Hash.new` requires `new` to be a custom `new` method to extract the capacity argument and compute the ideal slot size, but doing so defeat the existing `opt_new` optimization, and cause `String#initialize` / `Hash#initialize` to not be called. ### Prior Art @tenderlovemaking opened [Feature #22130] with the idea of a dedicated instruction for `String.new` but it died off over various concerns, one of which was that adding too many dedicated instruction would risk producing worse code in the interpreter loop. ### Solution So instead with @jhawthorn we thought of a smaller change. - Define `Hash#initialize` in Ruby for more efficient keyword argument handling. - Add a path in the existing `opt_new` to peek the stack for the `capacity:` keyword argument and pass it to the allocator. - Let `opt_new` call `initialize` like it would for the default `new` method. ### Implementation The `Hash.new` optimization was merged as [7142a3debb](https://github.com/ruby/ruby/commit/7142a3debbda9c35bef9921fc59a27225969b979) with some decent gains, and `String.new` is being finalized in [GH-19674](https://github.com/ruby/ruby/pull/19674) ### Concerns However after the `Hash.new` optimization was merged a subtle behavioral difference was noticed. Until now, refining `Hash#initialize` would have no effect when calling `Hash.new`, but in 4.1 the refined method is invoked by the interpreter (but not by JITs): ```ruby module R refine Hash do def initialize(*) raise "Not called prior to 4.1 nor by JITs" end end end using R Hash.new ``` ### Question What is the correct behavior here? Should this refinement work? or not work? Or is it "implementation defined". -- https://bugs.ruby-lang.org/
Issue #22501 has been updated by Eregon (Benoit Daloze). I made a PR to fix this: https://github.com/ruby/ruby/pull/19857 BTW, I think this should be `Bug`, not `Feature` since it's an unintentional incompatibility. I think it may make sense to backport to Ruby 4.0, up to the branch maintainer of course. ---------------------------------------- Feature #22501: Optimize `Hash.new` and `String.new` in `opt_new` https://bugs.ruby-lang.org/issues/22501#change-119887 * Author: byroot (Jean Boussier) * Status: Open ---------------------------------------- ### Context Both `Hash` and `String` initializer have a `capacity:` argument, because in performance sensitive code, correctly pre-sizing containers can have a big performance impact when the final size is large, as it saves both a number of reallocations, but also a number of copying / rehashing. Unfortunately, until now, for smaller capacities, the cost of handling keyword arguments from a C function would tend to outweigh the gains from right-sizing, making it hard to use in cases where the capacity is entirely dynamic and potentially small. Another concern is from Variable Width Allocation. For performance it's preferable to allocate embedded objects when possible, which requires to know the size prior to call `#initialize`, as at that point the slot has already been allocated to the minimum size. Hence the `String.new(capacity:)` / `Hash.new` requires `new` to be a custom `new` method to extract the capacity argument and compute the ideal slot size, but doing so defeat the existing `opt_new` optimization, and cause `String#initialize` / `Hash#initialize` to not be called. ### Prior Art @tenderlovemaking opened [Feature #22130] with the idea of a dedicated instruction for `String.new` but it died off over various concerns, one of which was that adding too many dedicated instruction would risk producing worse code in the interpreter loop. ### Solution So instead with @jhawthorn we thought of a smaller change. - Define `Hash#initialize` in Ruby for more efficient keyword argument handling. - Add a path in the existing `opt_new` to peek the stack for the `capacity:` keyword argument and pass it to the allocator. - Let `opt_new` call `initialize` like it would for the default `new` method. ### Implementation The `Hash.new` optimization was merged as [7142a3debb](https://github.com/ruby/ruby/commit/7142a3debbda9c35bef9921fc59a27225969b979) with some decent gains, and `String.new` is being finalized in [GH-19674](https://github.com/ruby/ruby/pull/19674) ### Concerns However after the `Hash.new` optimization was merged a subtle behavioral difference was noticed. Until now, refining `Hash#initialize` would have no effect when calling `Hash.new`, but in 4.1 the refined method is invoked by the interpreter (but not by JITs): ```ruby module R refine Hash do def initialize(*) raise "Not called prior to 4.1 nor by JITs" end end end using R Hash.new ``` ### Question What is the correct behavior here? Should this refinement work? or not work? Or is it "implementation defined". -- https://bugs.ruby-lang.org/
Issue #22501 has been updated by byroot (Jean Boussier).
BTW, I think this should be Bug, not Feature since it's an unintentional incompatibility.
The bug should be a different ticket. I'll open it. ---------------------------------------- Feature #22501: Optimize `Hash.new` and `String.new` in `opt_new` https://bugs.ruby-lang.org/issues/22501#change-119888 * Author: byroot (Jean Boussier) * Status: Open ---------------------------------------- ### Context Both `Hash` and `String` initializer have a `capacity:` argument, because in performance sensitive code, correctly pre-sizing containers can have a big performance impact when the final size is large, as it saves both a number of reallocations, but also a number of copying / rehashing. Unfortunately, until now, for smaller capacities, the cost of handling keyword arguments from a C function would tend to outweigh the gains from right-sizing, making it hard to use in cases where the capacity is entirely dynamic and potentially small. Another concern is from Variable Width Allocation. For performance it's preferable to allocate embedded objects when possible, which requires to know the size prior to call `#initialize`, as at that point the slot has already been allocated to the minimum size. Hence the `String.new(capacity:)` / `Hash.new` requires `new` to be a custom `new` method to extract the capacity argument and compute the ideal slot size, but doing so defeat the existing `opt_new` optimization, and cause `String#initialize` / `Hash#initialize` to not be called. ### Prior Art @tenderlovemaking opened [Feature #22130] with the idea of a dedicated instruction for `String.new` but it died off over various concerns, one of which was that adding too many dedicated instruction would risk producing worse code in the interpreter loop. ### Solution So instead with @jhawthorn we thought of a smaller change. - Define `Hash#initialize` in Ruby for more efficient keyword argument handling. - Add a path in the existing `opt_new` to peek the stack for the `capacity:` keyword argument and pass it to the allocator. - Let `opt_new` call `initialize` like it would for the default `new` method. ### Implementation The `Hash.new` optimization was merged as [7142a3debb](https://github.com/ruby/ruby/commit/7142a3debbda9c35bef9921fc59a27225969b979) with some decent gains, and `String.new` is being finalized in [GH-19674](https://github.com/ruby/ruby/pull/19674) ### Concerns However after the `Hash.new` optimization was merged a subtle behavioral difference was noticed. Until now, refining `Hash#initialize` would have no effect when calling `Hash.new`, but in 4.1 the refined method is invoked by the interpreter (but not by JITs): ```ruby module R refine Hash do def initialize(*) raise "Not called prior to 4.1 nor by JITs" end end end using R Hash.new ``` ### Question What is the correct behavior here? Should this refinement work? or not work? Or is it "implementation defined". -- https://bugs.ruby-lang.org/
Issue #22501 has been updated by byroot (Jean Boussier). Status changed from Open to Closed A bug report has been filed [Bug #22539] and backport requested for 4.0. Closing this, and I'll merge the `String.new` fastpath soon. ---------------------------------------- Feature #22501: Optimize `Hash.new` and `String.new` in `opt_new` https://bugs.ruby-lang.org/issues/22501#change-119893 * Author: byroot (Jean Boussier) * Status: Closed ---------------------------------------- ### Context Both `Hash` and `String` initializer have a `capacity:` argument, because in performance sensitive code, correctly pre-sizing containers can have a big performance impact when the final size is large, as it saves both a number of reallocations, but also a number of copying / rehashing. Unfortunately, until now, for smaller capacities, the cost of handling keyword arguments from a C function would tend to outweigh the gains from right-sizing, making it hard to use in cases where the capacity is entirely dynamic and potentially small. Another concern is from Variable Width Allocation. For performance it's preferable to allocate embedded objects when possible, which requires to know the size prior to call `#initialize`, as at that point the slot has already been allocated to the minimum size. Hence the `String.new(capacity:)` / `Hash.new` requires `new` to be a custom `new` method to extract the capacity argument and compute the ideal slot size, but doing so defeat the existing `opt_new` optimization, and cause `String#initialize` / `Hash#initialize` to not be called. ### Prior Art @tenderlovemaking opened [Feature #22130] with the idea of a dedicated instruction for `String.new` but it died off over various concerns, one of which was that adding too many dedicated instruction would risk producing worse code in the interpreter loop. ### Solution So instead with @jhawthorn we thought of a smaller change. - Define `Hash#initialize` in Ruby for more efficient keyword argument handling. - Add a path in the existing `opt_new` to peek the stack for the `capacity:` keyword argument and pass it to the allocator. - Let `opt_new` call `initialize` like it would for the default `new` method. ### Implementation The `Hash.new` optimization was merged as [7142a3debb](https://github.com/ruby/ruby/commit/7142a3debbda9c35bef9921fc59a27225969b979) with some decent gains, and `String.new` is being finalized in [GH-19674](https://github.com/ruby/ruby/pull/19674) ### Concerns However after the `Hash.new` optimization was merged a subtle behavioral difference was noticed. Until now, refining `Hash#initialize` would have no effect when calling `Hash.new`, but in 4.1 the refined method is invoked by the interpreter (but not by JITs): ```ruby module R refine Hash do def initialize(*) raise "Not called prior to 4.1 nor by JITs" end end end using R Hash.new ``` ### Question What is the correct behavior here? Should this refinement work? or not work? Or is it "implementation defined". -- https://bugs.ruby-lang.org/
participants (3)
-
byroot (Jean Boussier) -
Eregon (Benoit Daloze) -
shugo (Shugo Maeda)