Skip to content

Avoid allocation for anonymous positional splat with no arguments #12596

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

Conversation

jeremyevans
Copy link
Contributor

Anonymous positional splats cannot be directly accessed, they can only be passed as splats to other methods. So if an anonymous positional splat would be empty, you can use a shared frozen empty array to save an allocation.

def a(*) end
a()

This is similar to how anonymous empty keyword splats are optimized, except those use nil instead of a shared empty frozen hash.

This updates the allocation tests to check that the array allocations are avoided where possible.

It also makes a small change to test_iseq.rb to ensure an unfrozen hash is passed as the value of an anonymous splat parameter.

Anonymous positional splats cannot be directly accessed, they can
only be passed as splats to other methods.  So if an anonymous
positional splat would be empty, you can use a shared frozen
empty array to save an allocation.

```ruby
def a(*) end
a()
```

This is similar to how anonymous empty keyword splats are optimized,
except those use `nil` instead of a shared empty frozen hash.

This updates the allocation tests to check that the array allocations
are avoided where possible.

It also makes a small change to test_iseq.rb to ensure an unfrozen
hash is passed as the value of an anonymous splat parameter.
@jeremyevans jeremyevans force-pushed the avoid-array-allocation-empty-anonymous-positional-splat branch from 7afeffa to 42632f1 Compare March 27, 2025 18:18
@jeremyevans jeremyevans merged commit 43683e1 into ruby:master Mar 27, 2025
75 checks passed
casperisfine pushed a commit to Shopify/ruby that referenced this pull request Apr 2, 2025
@casperisfine
Copy link
Contributor

This made some assertion fail: #13043

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