mirror of
https://github.com/ruby/ruby.git
synced 2025-08-27 06:56:13 +02:00
Avoid unnecessary array allocations for f(arg, *arg, **arg, **arg), f(*arg, a: lvar), and other calls
The `f(arg, *arg, **arg, **arg)` case was previously not optimized.
The optimizer didn't optimize this case because of the multiple
keyword splats, and the compiler didn't optimize it because the
`f(*arg, **arg, **arg)` optimization added in
0ee3960685
didn't apply.
I found it difficult to apply this optimization without changing
the `setup_args_core` API, since by the time you get to the ARGSCAT
case, you don't know whether you were called recursively or directly,
so I'm not sure if it was possible to know at that point whether the
array allocation could be avoided.
This changes the dup_rest argument in `setup_args_core` from an int
to a pointer to int. This allows us to track whether we have allocated
a caller side array for multiple splats or splat+post across
recursive calls. Check the pointed value (*dup_rest) to determine the
`splatarray` argument. If dup_rest is 1, then use `splatarray true`
(caller-side array allocation), then set *dup_rest back to 0, ensuring
only a single `splatarray true` per method call.
Before calling `setup_args_core`, check whether the array allocation
can be avoided safely using `splatarray false`. Optimizable cases are:
```
// f(*arg)
SPLAT
// f(1, *arg)
ARGSCAT
LIST
// f(*arg, **arg)
ARGSPUSH
SPLAT
HASH nd_brace=0
// f(1, *arg, **arg)
ARGSPUSH
ARGSCAT
LIST
HASH nd_brace=0
```
If so, dup_rest is set to 0 instead of 1 to avoid the allocation.
After calling `setup_args_core`, check the flag. If the flag
includes `VM_CALL_ARGS_SPLAT`, and the pointed value has changed,
indicating `splatarray true` was used, then also set
`VM_CALL_ARGS_SPLAT_MUT` in the flag.
My initial attempt at this broke the `f(*ary, &ary.pop)` test,
because we were not duplicating the ary in the splat even though
it was modified later (evaluation order issue). The initial attempt
would also break `f(*ary, **ary.pop)` or `f(*ary, kw: ary.pop)` cases
for the same reason. I added test cases for those evaluation
order issues.
Add setup_args_dup_rest_p static function that checks that a given
node is safe. Call that on the block pass node to determine if
the block pass node is safe. Also call it on each of the hash
key/value nodes to test that they are safe. If any are not safe,
then set dup_rest = 1 so that `splatarray true` will be used to
avoid the evaluation order issue.
This new approach has the affect of optimizing most cases of
literal keywords after positional splats. Previously, only
static keyword hashes after positional splats avoided array
allocation for the splat. Now, most dynamic keyword hashes
after positional splats also avoid array allocation.
Add allocation tests for dynamic keyword keyword hashes after
positional splats.
setup_args_dup_rest_p is currently fairly conservative. It
could definitely be expanded to handle additional node types
to reduce allocations in additional cases.
This commit is contained in:
parent
ca0dae25ed
commit
3de20efc30
Notes:
git
2024-07-19 05:17:42 +00:00
3 changed files with 169 additions and 41 deletions
|
@ -303,7 +303,7 @@ class TestCall < Test::Unit::TestCase
|
|||
assert_syntax_error(%q{h[*a, 2, b: 5, **kw] += 1}, message)
|
||||
end
|
||||
|
||||
def test_call_splat_order
|
||||
def test_call_splat_post_order
|
||||
bug12860 = '[ruby-core:77701] [Bug# 12860]'
|
||||
ary = [1, 2]
|
||||
assert_equal([1, 2, 1], aaa(*ary, ary.shift), bug12860)
|
||||
|
@ -311,7 +311,7 @@ class TestCall < Test::Unit::TestCase
|
|||
assert_equal([0, 1, 2, 1], aaa(0, *ary, ary.shift), bug12860)
|
||||
end
|
||||
|
||||
def test_call_block_order
|
||||
def test_call_splat_block_order
|
||||
bug16504 = '[ruby-core:96769] [Bug# 16504]'
|
||||
b = proc{}
|
||||
ary = [1, 2, b]
|
||||
|
@ -320,6 +320,22 @@ class TestCall < Test::Unit::TestCase
|
|||
assert_equal([0, 1, 2, b], aaa(0, *ary, &ary.pop), bug16504)
|
||||
end
|
||||
|
||||
def test_call_splat_kw_order
|
||||
b = {}
|
||||
ary = [1, 2, b]
|
||||
assert_equal([1, 2, b, {a: b}], aaa(*ary, a: ary.pop))
|
||||
ary = [1, 2, b]
|
||||
assert_equal([0, 1, 2, b, {a: b}], aaa(0, *ary, a: ary.pop))
|
||||
end
|
||||
|
||||
def test_call_splat_kw_splat_order
|
||||
b = {}
|
||||
ary = [1, 2, b]
|
||||
assert_equal([1, 2, b], aaa(*ary, **ary.pop))
|
||||
ary = [1, 2, b]
|
||||
assert_equal([0, 1, 2, b], aaa(0, *ary, **ary.pop))
|
||||
end
|
||||
|
||||
def test_call_args_splat_with_nonhash_keyword_splat
|
||||
o = Object.new
|
||||
def o.to_hash; {a: 1} end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue