Skip to content

Remove expandarray/splatarray sequence peephole optimization #9727

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

Merged

Conversation

jeremyevans
Copy link
Contributor

newarray, duparray, concatarray, and splatarray always leave an array at the top of the stack. expandarray does not, it takes an array from the top of the stack as input, and leaves individual elements on the stack. I assume no Ruby code generates the expandarray/splatarray sequence, or it could break. The only use of expandarray outside the peephole optimizer is in the masgn code, and it does not appear to generate splatarray directly after expandarray.

The splatarray/splatarray peephole optimization is probably also wrong in the following case:

putobject  [1,2]
splatarray false
splatarray true

This instruction sequence should result in a duplicate of [1,2] at the top of the stack, but the peephole optimizer would remove the splatarray true, resulting in change that made [1,2] on top of the stack. I'm not sure Ruby code can generate splatarray false followed by splatarray true (I could get it to generate chains of splatarray true), so maybe this has no effect.

newarray, duparray, and concatarray all result in newly allocated arrays at the top of the stack, so they shouldn't have an issue with removing either splatarray true or splatarray false.

newarray, duparray, concatarray, and splatarray always leave an
array at the top of the stack.  expandarray does not, it takes
an array from the top of the stack as input, and leaves individual
elements on the stack.  I assume no Ruby code generates the
expandarray/splatarray sequence, or it could break. The only
use of expandarray outside the peephole optimizer is in the
masgn code, and it does not appear to generate splatarray
directly after expandarray.

The splatarray/splatarray peephole optimization is probably
also wrong in the following case:

```
putobject  [1,2]
splatarray false
splatarray true
```

This instruction sequence should result in a duplicate of
[1,2] at the top of the stack, but the peephole optimizer would
remove the `splatarray true`, resulting in change that made
[1,2] on top of the stack.  I'm not sure Ruby code can generate
`splatarray false` followed by `splatarray true` (I could get it
to generate chains of `splatarray true`), so maybe this has no
effect.

newarray, duparray, and concatarray all result in newly allocated
arrays at the top of the stack, so they shouldn't have an issue
with removing either `splatarray true` or `splatarray false`.
@jeremyevans jeremyevans requested a review from nobu January 26, 2024 22:25
@jeremyevans jeremyevans merged commit 8a027d1 into ruby:master Jan 27, 2024
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.

2 participants