Project

General

Profile

Actions

Feature #22501

closed

Optimize `Hash.new` and `String.new` in `opt_new`

Feature #22501: Optimize `Hash.new` and `String.new` in `opt_new`

Added by byroot (Jean Boussier) 30 days ago. Updated 21 days ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:127317]

Description

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 (Aaron Patterson) 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 (John Hawthorn) 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 with some decent gains, and String.new is being finalized in GH-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):

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".


Related issues 1 (0 open — 1 closed)

Related to Ruby - Bug #22539: Class#new shouldn't call refined initialize methodsClosedActions

Updated by shugo (Shugo Maeda) 23 days ago Actions #1 [ruby-core:127399]

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.

Updated by Eregon (Benoit Daloze) 22 days ago ยท Edited Actions #2 [ruby-core:127418]

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:

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 optimization/inlining of new should be transparent, and e.g. semantically it should be valid to define new in Ruby like

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 to initialize 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).

Updated by byroot (Jean Boussier) 21 days ago Actions #3 [ruby-core:127434]

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.

Updated by Eregon (Benoit Daloze) 21 days ago Actions #4 [ruby-core:127440]

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.

Updated by byroot (Jean Boussier) 21 days ago Actions #5 [ruby-core:127441]

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.

Updated by byroot (Jean Boussier) 21 days ago Actions #6

  • Related to Bug #22539: Class#new shouldn't call refined initialize methods added

Updated by byroot (Jean Boussier) 21 days ago 1Actions #7 [ruby-core:127443]

  • 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.

Actions

Also available in: PDF Atom