compile.c: statically resolve swap instructions when possible - #18538
Merged
Conversation
byroot
commented
Aug 27, 2026
Comment on lines
+372
to
+378
| type == BIN(putobject) || | ||
| type == BIN(putspecialobject) || | ||
| type == BIN(putnil) || | ||
| type == BIN(putself) || | ||
| type == BIN(duphash) || | ||
| type == BIN(getinstancevariable) || | ||
| type == BIN(getlocal) |
Member
Author
There was a problem hiding this comment.
Would be worth analyzing some real world code to see if there's some more common cases.
If the two preceeding instructions are known not to be dependent
on the state of the stack, then we can eliminate the `swap` and inverse
the two previous instructions.
This mostly eliminate instruction in two cases:
```ruby
Object.new
```
```
0000 opt_getconstant_path <ic:0 Object> ( 1)[Li]
0002 putnil
0003 swap
0004 opt_new <calldata!mid:new, argc:0, ARGS_SIMPLE>, 11
```
And:
```ruby
{**@A, b: 2}
```
```
0000 getinstancevariable :@A, <is:0> ( 1)[Li]
0003 putspecialobject 1
0005 swap
0006 putobject :b
0008 putobject 2
0010 opt_send_without_block <calldata!mid:core#hash_merge_ptr, argc:3, ARGS_SIMPLE>
```
Co-Authored-By: John Hawthorn <john@hawthorn.email>
eregon
reviewed
Sep 8, 2026
| ruby/parser_test.rb | ||
| ruby/ripper_test.rb | ||
| ruby/ruby_parser_test.rb | ||
| ruby/parameters_signature_test.rb |
Member
There was a problem hiding this comment.
This actually found that this PR causes 2 :line events on the second line for
return unless x
Foo.newSee https://bugs.ruby-lang.org/issues/22299
I'm aiming to remove all skips in ruby/prism#4223 so it's clear new skips should not be added but are actual regressions for :line events.
Member
Author
There was a problem hiding this comment.
Sorry about that, I'll have to dig into tracepoint to see if that's possible to fix, or if I have to revert that optimization.
byroot
added a commit
to byroot/ruby
that referenced
this pull request
Sep 9, 2026
[Bug #22299] Followup: ruby#18538
byroot
added a commit
to byroot/ruby
that referenced
this pull request
Sep 9, 2026
[Bug #22299] Followup: ruby#18538
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.
If the two preceeding instructions are known not to be dependent on the state of the stack, then we can eliminate the
swapand inverse the two previous instructions.This mostly eliminate instruction in two cases:
And: