Skip to content

perf(rt): adopt PreparedCall in reduce's fold loops - #727

Open
mparrett wants to merge 3 commits into
mainfrom
perf/vm-prepared-call-reduce
Open

perf(rt): adopt PreparedCall in reduce's fold loops#727
mparrett wants to merge 3 commits into
mainfrom
perf/vm-prepared-call-reduce

Conversation

@mparrett

@mparrett mparrett commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Second adopter from #720, stacked on the PreparedCall PR. reduce's four fold loops — ArrayVector, Range, chunked seq, and linear seq — each invoked the reducing fn through ec.Invoke, paying per-element resolution, frame-pool mutex traffic, and frame init. Resolve the reducer once per fold with PrepareCall and reuse its frame; non-bytecode and variadic reducers keep the reused-fargs fallback in all four loops.

Adds PreparedCall.Call2 for binary callables, introduced with its first consumer rather than ahead of one. PrepareCall's arity guard widens to admit 2, and each CallN validates against the prepared arity — a mismatch returns an error instead of panicking or invoking with unpopulated argument slots.

Measured on (reduce (fn [a x] (+ a x)) 0 (range 1000000)), darwin/arm64, interleaved mins: 50.5 ms vs 67.3 on the base branch and ~58.7 on v1.12.2 — the second adopter to land below the release baseline. Same perf-repeat caveat as the base PR: the micro families don't route through rt reduce, so the lane is a regression guard; the e2e number is the claim.

New tests: Call2 repeated invocation, CallN/arity mismatch errors, unsupported-arity rejection. Suites green: pkg/vm, pkg/rt, test/.

@mparrett mparrett added the perf-repeat Run the repeat A/B (variance-reduced) perf check label Aug 12, 2026
@mparrett
mparrett requested a review from nnunley August 12, 2026 04:15
@mparrett
mparrett force-pushed the perf/vm-prepared-call-reduce branch from 08e1da3 to 239cfff Compare August 12, 2026 05:55
@mparrett
mparrett force-pushed the perf/vm-prepared-call-reduce branch from b372353 to 999f0bd Compare August 12, 2026 16:28
@mparrett
mparrett requested a review from nooga August 12, 2026 17:50
@mparrett
mparrett force-pushed the perf/vm-prepared-call-reduce branch from 999f0bd to 42f4115 Compare August 12, 2026 19:44
Base automatically changed from perf/vm-prepared-call to main August 12, 2026 20:56
@mparrett
mparrett force-pushed the perf/vm-prepared-call-reduce branch 3 times, most recently from a11721a to a8e6d4a Compare August 13, 2026 16:40
@mparrett mparrett removed the perf-repeat Run the repeat A/B (variance-reduced) perf check label Aug 13, 2026
@mparrett
mparrett force-pushed the perf/vm-prepared-call-reduce branch from a8e6d4a to 43e9ad3 Compare August 14, 2026 00:03
@mparrett mparrett added the review-priority/medium Review after bases land / once rebased label Aug 14, 2026
mparrett and others added 3 commits August 14, 2026 16:16
reduce's four fold loops (ArrayVector, Range, chunked, and linear seq) each
invoked the reducing fn through ec.Invoke, paying resolution, frame-pool
mutex traffic, and frame init once per element. Resolve the reducer once per
fold with PrepareCall and reuse its frame; non-bytecode and variadic
reducers keep the reused-fargs ec.Invoke fallback. Adds PreparedCall.Call2
for binary callables — deliberately introduced with its first consumer.

On (reduce (fn [a x] (+ a x)) 0 (range 1000000)), darwin/arm64, interleaved
mins: 50.5 ms vs 67.3 on the base branch and ~58.7 on v1.12.2 — the second
adopter to land below the release baseline.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The guard added on the base branch admits only arities a CallN entry
point can populate; Call2 lands here, so admit arity 2 and pin it with
a repeated-invocation test. Also refresh generated.sums for this
branch's own native_prims.go edits.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Nothing tied the invocation method to the preparation: Call2 on a
unary preparation panicked writing args[1], and Call1 on a binary one
invoked bytecode with a stale or Go-nil second slot. CallN now returns
an ExecutionError on mismatch; the check is one predictable compare
per call (interleaved reduce micro unchanged: 39.4ms base / 39.0ms
head mins).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@mparrett
mparrett force-pushed the perf/vm-prepared-call-reduce branch from 43e9ad3 to f688abc Compare August 14, 2026 23:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review-priority/medium Review after bases land / once rebased

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant