Skip to content

Keep shared-node takes inline on the CPU - #2114

Draft
oxura wants to merge 2 commits into
bendlang:mainfrom
oxura:fix/2081-host-inline-fade-20261110
Draft

oxura wants to merge 2 commits into
bendlang:mainfrom
oxura:fix/2081-host-inline-fade-20261110

Conversation

@oxura

@oxura oxura commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2081.

CPU takes were paying a FAR call even for a last-held shared node. Use the existing DEVICE switch: keep that helper out of line on Metal/CUDA devices, but let the host inline it. No helper body, memory ordering or cap changes.

Using nuxyel’s BPE repro, three alternating before/after runs pinned to CPU0:

perf stat median main 335f239a This change Change
User instructions 193,352,682,977 172,577,214,225 −10.74%

All six runs print 256, 31633, 2633430213. Linux x86_64, i7-11700, clang22.1.8, -std=c11 -O3; PMU counters ran over99% of the interval. Cycles were noisy, so this only claims the instruction reduction.

Build the linked repro with each checkout’s bun bend2/main.ts bpe_encode.bend -o bpe, then run taskset -c 0 perf stat -e instructions:u,cycles:u ./bpe.

Repo gate passes56/56; comp.ts is65987/66000 tokens. Full unchanged gate bundle was also run: test/perf fail because this machine cannot resolve the cluster bastion; ping skips because the site checkout is unavailable. Keeping this draft for mini/platform qualification. No GPU, macOS or Windows execution claim.

@nuxyel also checked this head on the original Core Ultra 7 machine. The three output lines and instruction reduction held; full host inlining beat the proposed split. Their cycles are directional because the machine was busy, and they measured a larger binary. This is their independent CPU check, not locally rerun mini or GPU qualification.

Thanks @nuxyel for the repro and measurements. AI-assisted with omp / GPT-6.1 Sol; no subagents.

@nuxyel

nuxyel commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Checked on my side, on the machine from #2081: Core Ultra 7 155H, one thread pinned to a P-core, clang 23.1.3 with -std=c11 -O3, perf stat in user mode, three alternated rounds. The C was emitted with bun bend2/main.ts at 335f239 and at this PR's head (f4a64ca), and every build prints the same three lines.

instructions cycles
335f239 194.16 G 43.5 G
this PR 172.93 G (−10.9%) 38.2 G (−12.2%)
335f239 with 2.0.39's ctr_take/span_fade 173.62 G (−10.6%) 38.7 G (−11.1%)

The instructions repeat to within about a thousand between rounds. The machine wasn't idle, so the cycles only show the direction.

The PR inlines all of span_fade on the host, not only the last-holder check. The binary has no span_fade symbol left and grows from 68.8 KB to 76.9 KB, and it runs 0.4% fewer instructions than the split I suggested in #2081. So it's the better fix for this program. I haven't tried CUDA or Metal; both stay on FAR here.

@oxura

oxura commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Thanks — I’ve linked your original-machine run and the binary-size tradeoff in the description, with cycles marked directional. Keeping this draft until the required mini qualification; no CUDA or Metal result claimed.

nicolas-abril added a commit to nicolas-abril/bend that referenced this pull request Dec 1, 2026
span_fade and rfc_wrap are inline on the host (the take is bendlang#2114's change,
by Lorenzobattistela) and unchanged on the devices. A constructor with
static nodes that is not hot is read in place, like any uncounted node:
the inlined counted path made each of lexer's takes longer.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
(cherry picked from commit a41ad59)
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.

2.0.40: on the CPU, every take of a shared node is a call (#1978), +10% instructions on a list-heavy program

2 participants