Skip to content

perf(transduce): native map, filter, remove and take transducer stages (O-111) - #133

Merged
BuddhiLW merged 5 commits into
stagingfrom
perf/transduce-fused
Dec 1, 2026
Merged

BuddhiLW merged 5 commits into
stagingfrom
perf/transduce-fused

Conversation

@BuddhiLW

@BuddhiLW BuddhiLW commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

What

Native reducing functions for the map, filter, remove and take transducer stages (O-111, card CLJW-PERF-REDUCE-PATH). (map f) and the others now return native Function closures that carry native_call. A stage calls user f/pred through invokeCallable. It hands the element straight to the downstream rf, with no vm.eval frame per stage per element. comp, user transducers, init/completion arities, reduced and the lazy collection arities keep their current behaviour.

Commits:

  • f8faf0e7 the implementation (preserved WIP from the ling session; its subject still says wip)
  • bfffb4f7 measurements, history rows, and an .dev/optimizations.md table fix

Verification (against origin/staging 6c91f00)

  • zig build -Dwasm -Doptimize=ReleaseSafe: builds.
  • bash test/run_all.sh --smoke e2e_phase16_gc_torture: green, 17/17 steps, including zig_build_test_vm, zig_build_test_tree_walk (the dual-backend diff oracle) and test_clj_suites (171 suites, 1000 tests, 5393 assertions).
  • test/clj/suites/native_transducer_test.clj (14 tests, 158 assertions) passes on the JVM oracle (Clojure 1.12), on the VM binary and on a tree_walk binary. It covers early termination through reduced, per-rf take state, nested and reentrant take, completion arity, comp ordering, mixed user transducers, multi-input map, lazy apply tails, exceptions and forced GC inside callbacks.
  • 17 extra probes give identical output on the baseline binary, this branch on VM, and this branch on tree_walk. They cover partition-all, dedupe, take-while, mapcat, eduction over (range), nested take, (take 0) and multi-coll sequence.
  • I also ran the full clj suite runner on a tree_walk binary (the gate does not). It reports 2 failures, both in suites.gc-heap-return-test (committed-heap bounds). The baseline 6c91f00 tree_walk binary fails the same 2 assertions, so they come from the conservative stack scan and predate this branch.
  • The WIP commit said phase16_gc_torture failed once with swap!: expected atom, got ref. That did not reproduce: 6 consecutive standalone runs plus the smoke gate's own run all passed.

Measurements

Pinned A/B: taskset -c 2, cpu_core/instructions/u, 5 interleaved samples per side, ReleaseSafe -Dwasm. Min instructions, with min in-process ms in brackets (the host was shared and loaded, so ms is noisy):

workload before (6c91f00) after (this branch) delta instr
transduce 28.820 G (10351 ms) 11.208 G (4345 ms) -61.1%
into_map_only 1.764 G 1.557 G -11.8%
into_transient_checks 0.144 G 0.130 G -9.6%
into_collections 15.164 G 14.778 G -2.5%
reduce_map 29.221 G 29.392 G +0.6%
lazy_chain 8.009 G 8.039 G +0.4%
arrays 4.601 G 4.632 G +0.7%
json_roundtrip 2.337 G 2.353 G +0.7%
protocol_dispatch 6.638 G 6.654 G +0.2%
gc_churn 6.843 G 6.847 G +0.1%
fn_call, fib_recursive, dot_loop_nth, numeric_doubles, vector_assoc, string_ops identical identical 0.0% (string_ops -0.2%)

No workload regresses by more than +0.7% in instructions. reduce_map uses the lazy multi-coll map, which these stages do not cover (see the card).

bench/vs_bb/run.sh -n 5 --perf --no-jvm, two interleaved rounds per side (load 38 to 67). The four rows are appended to bench/vs_bb/history.yaml: transduce in-process best 3016 ms -> 1359 ms, cljw/bb 6.16 -> 2.84. Unpinned instructions:u there mixes P-core and E-core counts and is not comparable across rows, which is why the pinned table above is the one to read.

Merged with staging (#130 and #132)

After the merges (final sha 6188344 against staging b8a57ec)

  • bash test/run_all.sh --smoke e2e_phase16_gc_torture: green, 17/17 steps.
  • native_transducer_test (14 tests, 158 assertions): passes on VM and tree_walk.
  • A reduce-path probe (native rf over a range and over a vector through perf(vm): flatten variadic calls at exact arity, resolve reduce callees once #132's resolved arms, comp with take, multi-input map) gives the same output on staging, on the branch VM build and on the branch tree_walk build. The 17 earlier probes are unchanged too.
  • Pinned re-measure (taskset -c 2, cpu_core/instructions/u, 5 interleaved samples per side, min):
workload staging b8a57ec (#130 + #132) this branch 6188344 delta
transduce 27.461 G (2692 ms) 11.086 G (993 ms) -59.6%
reduce_map 28.694 G (3592 ms) 28.900 G (3402 ms) +0.7%

The optimisations compose. #132 already took transduce from 28.82 G to 27.46 G, and the native stages take it to 11.09 G. reduce_map stays on #132's level, as expected, because its lazy multi-coll map has no transducer stage.

… comp

WORK IN PROGRESS, not for merge. Preserved from ling cljw-transduce-fused
(gpt-6.1) when its session ended before it committed.

Native reducing-function objects for (map f), (filter pred),
(remove pred), (take n) and their comp, with a native suite
test/clj/suites/native_transducer_test.clj.

Same-window interleaved measurements (in-process ms, instructions:u):
- transduce: 3224 -> 1120 ms (bb 361); 28.82 G -> 11.21 G instructions.
- legacy_transduce: 228 -> 138 ms; into_transient_checks 112 -> 87 ms.
- reduce_map: 4088 -> 4304 ms in window 2 (no gain; noise-level).
- lazy_chain, into_collections, vector_assoc, gc_large_heap within noise.

Open before a PR: the GC torture run failed once with
"swap!: expected atom, got ref" (torture97-tests exit 1; the retry
exited 0). Find whether a native stage leaks a ref where an atom is
expected under GC pressure, then rerun zig build test -Dwasm, all native
suites and the torture smoke.

Smell-audited: 1: WIP preserved with its open torture failure stated, not proposed for merge.
Verifies the native map, filter, remove and take transducer stages
(O-111) against origin/staging 6c91f00 and records the samples.

- bench/vs_bb/history.yaml: two interleaved best-of-5 rounds per side,
  ReleaseSafe -Dwasm, host load 38-67. Rows for 6c91f00 are the
  baseline binary; rows for f8faf0e are this branch.
- Pinned (taskset -c 2, cpu_core instructions:u, 5 samples per side,
  interleaved): transduce 28.820G -> 11.208G (-61.1%), 10351 -> 4345
  ms. No other workload moved by more than +0.7% in instructions.
- .dev/optimizations.md: O-111 sat after a blank line, outside the
  table; it now continues the table.

The GC torture flake the WIP commit recorded did not reproduce: six
consecutive phase16_gc_torture runs and the smoke gate's run passed.
Keeps both sides. CHANGELOG.md: the native transducer stage entry and
the GC allocation record entry both stay under [Unreleased] / Changed.
.dev/optimizations.md: O-111 and O-092 both continue the table.

Smell-audited: 0: merge only; no source line changed beyond the two sides.
Keeps both sides of invokeCallable: native transducer closures (O-111)
dispatch through native_call first, then a fixed-arity .fn_val takes
#132's resolved path (O-064).

resolveFixedArity now returns null for a native closure. #132's reduce
range and vector arms call it directly; a native map stage carries a
variadic arity stub with no body, which the resolved binder would
otherwise run instead of native_call.

Smell-audited: 1: two-line guard plus conflict resolution, both behaviours kept.
@BuddhiLW
BuddhiLW marked this pull request as ready for review December 1, 2026 13:39
bash scripts/gen_placement.sh after the O-111 stages. The only change is
the four new Zig builtins cljw.internal/__map-xform, __filter-xform,
__remove-xform and __take-xform (total 1472 -> 1476, zig 438 -> 442).
check_placement_status, check_core_surface, check_doc_coverage and
check_capability_claims pass locally.
@BuddhiLW
BuddhiLW merged commit 297fdf3 into staging Dec 1, 2026
3 checks passed
BuddhiLW added a commit that referenced this pull request Dec 1, 2026
…zig-0.17.1

#133 was written for Zig 0.16. Ported here: transducer.zig's
@intFromEnum / @enumFromInt become @backingInt / @fromBackingInt, and
the two `Kind` decodes of an i64 capture take @intcast, since
@fromBackingInt wants the exact backing integer. data/placement.yaml
takes staging's (gen_placement.sh shows no drift under 0.17.1).
native_transducer_test: 14 tests, 158 assertions, 0 failures on vm.

Smell-audited: 1: forward merge plus the mechanical 0.17 port of the merged code
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.

1 participant