Thanks to visit codestin.com
Credit goes to github.com

Skip to content

YJIT: call alloc func directly in new #13219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jhawthorn
Copy link
Member

@jhawthorn jhawthorn commented Apr 30, 2025

This removes an indirect branch by calling the allocator function from the class directly.

Benchmark 1: ./miniruby_master --yjit -e '20_000_000.times { Object.new }'
  Time (mean ± σ):      1.203 s ±  0.020 s    [User: 1.187 s, System: 0.011 s]
  Range (min … max):    1.168 s …  1.230 s    10 runs

Benchmark 2: ./miniruby_allocator --yjit -e '20_000_000.times { Object.new }'
  Time (mean ± σ):     967.5 ms ±  22.4 ms    [User: 951.6 ms, System: 11.5 ms]
  Range (min … max):   949.9 ms … 1026.0 ms    10 runs

Summary
  ./miniruby_allocator --yjit -e '20_000_000.times { Object.new }' ran
    1.24 ± 0.04 times faster than ./miniruby_master --yjit -e '20_000_000.times { Object.new }'

cc @tenderlove

This removes an indirect branch by calling the allocator function from
the class directly.
@matzbot matzbot requested a review from a team April 30, 2025 23:21

let allocator = unsafe { rb_get_alloc_func(comptime_recv) };
let allocator = allocator.unwrap_or(rb_obj_alloc);
let obj = asm.ccall(allocator as _, vec![comptime_recv.into()]);
Copy link
Member

@k0kubun k0kubun Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When an allocator function is found, it seems to skip the if (rb_obj_class(obj) != rb_class_real(klass)) { rb_raise(rb_eTypeError, "wrong instance allocation"); } check in class_call_alloc_func. Is the error unreachable?

Copy link
Member

@tenderlove tenderlove Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this only be a problem for T_DATA objects? I don't think they use rb_class_allocate_instance as their allocator.

Copy link
Member Author

@jhawthorn jhawthorn Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's only reachable from a custom allocator which is implemented incorrectly (there's no reason to ever return an object with the wrong class), so I'm not sure if it's necessary to keep.

If it is I suppose we could allowlist just the most common builtin allocators (or even just the T_OBJECT allocator).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly didn't know when it happens (it'd be nice to have a comment there) and thus asked a question. If the check is useless (on opt_new's rb_obj_alloc call) on the interpreter too, it's fine.

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants