Skip to content

ZJIT: Rest parameter copies a caller-built splat array that the interpreter reuses #1095

Open

Description

@thomasklemm

When the caller builds the splat array for the call (splatarray true + pushtoarray, VM_CALL_ARGS_SPLAT_MUT), the interpreter turns that array into the rest parameter: setup_parameters_complex sets rest_dupped and either uses the array as-is or beheads it in place with rb_ary_behead. ZJIT expands the splat into ArrayArefs and packs the rest into a new Array, so it allocates one more Array per call.

def rest(s, *a) = a                 # like URI.for(scheme, *arguments, default:)
def rest_kw(s, *a, default: 1) = a
def exact(*a) = a

def call_rest(x) = rest(*x, 4)      # splatarray true + pushtoarray => ARGS_SPLAT_MUT
def call_rest_kw(x) = rest_kw(*x, 4)
def call_exact(x) = exact(*x, 4)

def allocations
  before = GC.stat(:total_allocated_objects)
  yield
  GC.stat(:total_allocated_objects) - before
end

x = [1, 2, 3]
30.times do
  allocations { call_rest(x) }; allocations { call_rest_kw(x) }; allocations { call_exact(x) }
end
p [allocations { call_rest(x) }, allocations { call_rest_kw(x) }, allocations { call_exact(x) }]

On master (2bc6f9a), objects per call:

call_rest call_rest_kw call_exact
interpreter 1 1 1
--yjit --yjit-call-threshold=1 3 1 2
--zjit --zjit-call-threshold=1 2 2 2

(YJIT's extra objects come from different causes and are addressed in ruby#19664 and ruby#19666.)

--zjit-dump-hir, call_rest inlined into the block:

v41:ArrayExact = ToNewArray v12
...
ArrayPush v41, v45
v89:CInt64 = ArrayLength v41
v90:CInt64[4] = GuardBitEquals v89, CInt64(4) recompile
...
v98:BasicObject = ArrayAref v41, v97
v100:BasicObject = ArrayAref v41, v99
v102:BasicObject = ArrayAref v41, v101
v103:ArrayExact = NewArray v98, v100, v102
PushInlineFrame :rest, v28 (0x...), num_args=2

call_exact gets the same ToNewArray, then NewArray v113, v115, v117, v119, even though the rest receives exactly the splat elements.

Copying the interpreter's in-place behead isn't straightforward. Since ruby#19538, the stack-overflow check in gen_send_iseq_direct/gen_push_inline_frame side-exits with the original send's stack (exit_state), and the interpreter replays the send. That stack holds the caller-built array, so an in-place behead before that exit would make the replay see wrong arguments. Possible directions: pass the array through unchanged when the rest takes exactly the splat elements (nothing is mutated, so replay is unaffected); behead only after the last exit that uses the original state; or don't materialize the ToNewArray copy except on exit (cf. #1046) and build the rest directly from the source elements.

Breadth: in a nine-app Rails main allocation census, this is the largest remaining ZJIT-over-interpreter difference. It comes from URI.for(*self.split(uri), self) in URI::RFC3986_Parser#parse (ruby/uri lib/uri/rfc3986_parser.rb:135, uri 1.1.2, the version bundled with Ruby master), which every URI(...)/URI.parse call goes through, and hits 5 of 9 apps, +18.5 objects per operation on average (Campfire: 182 → 362 Arrays per request).

Activity

  1. added a commit that references this issue on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions