Skip to content

perf: drop closure and constant-divisor checks that cannot fail - #1402

Merged
kacy merged 3 commits into
mainfrom
perf-closure-in-place
Nov 15, 2026
Merged

kacy merged 3 commits into
mainfrom
perf-closure-in-place

Conversation

@kacy

@kacy kacy commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Closure calls and constant division carried checks that could only pass:

  • A closure body reading its own capture with a constant slot now does one load. The closure is the body's first parameter, and every call through a closure value tests the handle before it jumps in, so the second handle test and the spill check could not fail. In make_adder's lambda the body went from 26 instructions to 10.
  • A closure's handle test is reused for the rest of a straight run, so f(f(x)) tests f once. The cache is keyed by the local that holds the closure, dropped when that local is stored to, and cleared at every label.
  • Storing a capture into a closure built on this function's stack, at a constant slot, is one store.
  • A constant divisor other than 0 and -1 no longer emits divide-by-zero and overflow checks. Their conditions folded to constants, but the branches stayed: two dead tests on every i % 7.

Measured against main (t2.sh, 5 interleaved rounds, median ms):

bench main this change
closure_calls 114 82 -28%
closure_error 288 273 -5%
http_head_read 646 614 -5%
the rest of the set within noise

Under callgrind, closure_calls goes from 218M to 140M instructions and closure_error from 892M to 773M. A call through a closure now costs the same as a direct call in closure_calls.

Building maps and sets in place, which was planned for this change, is left out: it cost event_ledger 8% in timing with no gain anywhere, even though it ran fewer instructions.

The local suite passes, including both leak gates and memcheck.

Refs #986

@kacy
kacy merged commit 9cfe80d into main Nov 15, 2026
4 checks passed
@kacy
kacy deleted the perf-closure-in-place branch November 15, 2026 01:44
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