Skip to content

Reduce number of instructions when fetching constants#5709

Closed
kddnewton wants to merge 1 commit intoruby:masterfrom
kddnewton:getconstant-instruction
Closed

Reduce number of instructions when fetching constants#5709
kddnewton wants to merge 1 commit intoruby:masterfrom
kddnewton:getconstant-instruction

Conversation

@kddnewton
Copy link
Contributor

@kddnewton kddnewton commented Mar 24, 2022

Currently every time there is a getconstant instruction we push a boolean onto the stack to let it know if it can search the global scope. This boolean is always known at compile-time though, so there's no need for it to be pushed onto the stack. Instead we can pass it directly as an argument to the getconstant instruction, thereby reducing the number of instructions in total.

After this change, it appears to be quite a bit faster on the constant invalidation benchmark.

|                       |compare-ruby|built-ruby|
|:----------------------|-----------:|---------:|
|constant_invalidation  |       3.198|     5.337|
|                       |           -|     1.67x|

Copy link
Member

@tenderlove tenderlove left a comment

Choose a reason for hiding this comment

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

LGTM, but if we change the instruction shouldn't we also fix YJIT?

@kddnewton
Copy link
Contributor Author

I might be reading this wrong, but I don't think we generate code for getconstant in YJIT. I think it should just work.

@XrXr
Copy link
Member

XrXr commented Mar 24, 2022

It's deliberately a stack argument as opposed to an instruction argument for compatibility with tools that look at the byte code. See 661927a and the discussion on https://bugs.ruby-lang.org/issues/11718#note-15.

Maybe things have changed since 2019 though. CC @ko1

@kddnewton
Copy link
Contributor Author

kddnewton commented Mar 24, 2022 via email

@XrXr
Copy link
Member

XrXr commented Mar 24, 2022

https://github.com/ruby/typeprof/ is probably the most prominent tool that relies on the iseq being stable. In fact since it's a bundle gem it's giving CI failures on this PR: https://github.com/ruby/ruby/runs/5682482784?check_suite_focus=true

@kddnewton
Copy link
Contributor Author

kddnewton commented Mar 24, 2022 via email

@XrXr
Copy link
Member

XrXr commented Mar 24, 2022

I'm not familiar enough with typeprof to say. Maybe @mame knows.

@eregon
Copy link
Member

eregon commented Mar 25, 2022

IMHO not changing the bytecode because some tool(s) relies on it is the beginning of the end (in terms of optimization, flexibility and leaking implementation details).
I believe the CRuby bytecode should always remain an implementation detail.
Maybe this change can be detected on the typeprof side (can it find out the number of operands of an instruction?), or the bytecode could have a header with a version or so?

@maximecb
Copy link
Contributor

maximecb commented Mar 25, 2022

This change seems like a good idea 👍

I think it's also fairly minor, we'll be able to adapt YJIT easily if we need to change anything. Other tools also shouldn't have a difficult time.

Currently every time there is a getconstant instruction we push a boolean onto the stack to let it know if it can search the global scope. This boolean is always known at compile-time though, so there's no need for it to be pushed onto the stack. Instead we can pass it directly as an argument to the getconstant instruction, thereby reducing the number of instructions in total.
@kddnewton kddnewton force-pushed the getconstant-instruction branch from a623426 to cf82168 Compare May 12, 2022 13:32
@kddnewton
Copy link
Contributor Author

Okay, I've just rebased this branch. In terms of chicken-and-egg, is the only way to get this merged to fix typeprof first so that it can detect this kind of change? Trying to determine how to get this to move forward.

@XrXr
Copy link
Member

XrXr commented May 12, 2022

Yeah, we want to fix typeprof first since we want green CI on master.

@kddnewton kddnewton closed this Sep 27, 2022
@kddnewton kddnewton deleted the getconstant-instruction branch September 27, 2022 19:29
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.

5 participants