Skip to content

fix(compiler): leave literal affine constructor fields raw - #1566

Closed
oxura wants to merge 3 commits into
bendlang:mainfrom
oxura:fix/1559-affine-constructor-fields-20261014
Closed

oxura wants to merge 3 commits into
bendlang:mainfrom
oxura:fix/1559-affine-constructor-fields-20261014

Conversation

@oxura

@oxura oxura commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1559.

A constructor built at a known literal Kind(&1) now keeps its fields raw, even when another instantiation makes that constructor hot. Generic quantities still use conservative sealing. Matches still go through the existing ctr_take; keep and drop behavior is unchanged.

The regression mixes literal-affine construction with a generic constructor used at both &1 and &2, then consumes the Data result twice. It prints 499500, 999000, 499500 in the interpreter, native C (1 and 4 threads), and emitted JS on Bun. Seven ownership programs pass those lanes; the repository cap gate passes.

I merged main and removed the unrelated layout and GUIDE changes in 7896a14e.

The issue author's server measurements are now in: neutral. Almost all of that program's data is shared, so this literal-affine rule does not change its hot request path. The earlier microbenchmarks are not evidence of a production speedup, and no real-app speedup is claimed here.

Still on hold: this changes compiled-C behavior for an @unsafe copy of Kind(&1), including the reported Base Array.fork case. That needs @VictorTaelin or @nicolas-abril to sign off.

Not tested: macOS, Metal, CUDA, or the mini-cluster corpus.

AI-assisted with GPT-6.1 Sol; no subagents on this PR.

@aldeni

aldeni commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Thanks for picking this up so quickly! I ran it here, on the same machine as the issue, with the issue's two files: this branch (24d6523) against main (dbb3157), --threads 1, medians of 9 interleaved runs and the peak RSS of one run.

cold.bend heat.bend
main 149.8 ms, 22.8 MB 297.7 ms, 32.9 MB
this PR 150.4 ms, 22.7 MB 164.9 ms, 22.7 MB

build no longer seals, and sum keeps its ctr_take, which is the ~10% that's left, as expected.

tests/rfc/affine_build_in_hot.bend prints 499500, 999000, 499500 interpreted, on JS, and in C at 1 and 4 threads, and its emitted C goes from 5 seals to 3: the generic build over the erased quantity still seals, as you intended. gates/repo.ts is 54/54 here too. And I like the test: the same generic build at &1 and at &2, with the &2 result really copied, is exactly the case I could only point at in the issue.

@oxura

oxura commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the independent same-machine qualification and for exercising the erased-quantity build at both &1 and &2 with a genuinely copied Data result. I will keep the conservative generic sealing and existing ctr_take behavior exactly as qualified; removing the remaining match cost is not part of this PR. Head24d6523e is unchanged. Your measurements and platform qualification remain attributed to your run.

@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

Thanks @oxura, and thanks @aldeni for the issue and the numbers.

We're holding this one for now. The fix looks sound for safe code: a value whose type is literally Kind(&1) can never be shared, so storing its fields raw is safe. All five gates pass when it's merged with main, and on an M4 heat.bend drops from 164 ms to 62 ms.

What we're not sure about is how often this slowdown actually matters. On our 17 perf benchmarks, the emitted C is byte-for-byte the same with and without this PR. The 2x comes from a micro-benchmark built to trigger the slowdown. Meanwhile the PR changes behavior: an @unsafe copy of a Kind(&1) value now stops at runtime in compiled C, where main ran it. That includes Base's Array.fork over an Array<List<U32>> when both halves get matched. A new compiler rule like this needs sign-off from @VictorTaelin or @nicolas-abril.

@aldeni, could you measure this PR on the server you mentioned (the one where counting and freeing took 28–33% of CPU)? A real program that gets noticeably faster would make the case. If it does, two changes before merging:

  1. Drop the token trims in comp.ts. Main has room under the cap now, and the fix alone fits.
  2. Leave out the GUIDE.md paragraph. It uses terms the guide doesn't define, and we'll handle the wording separately.

oxura added 2 commits October 15, 2026 17:38
…ain' into review/1566-maintainer-20261015-1112
Restore the existing layout helpers and constructor-local spelling; leave GUIDE wording to maintainers. Keep the literal-affine rule and generic conservative sealing unchanged. Repository cap gate 54/54; seven interpreted/C1/C4/JS ownership consumers passed. Production workload benefit and unsafe-rule sign-off remain pending. AI-assisted follow-up: GPT-6.1 Sol (openai-codex/gpt-6.1-sol).
@oxura

oxura commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 7896a14e: merged current main without rewriting history, removed the unrelated token trims from comp.ts, and removed the GUIDE change. The affine rule, conservative generic sealing and existing ctr_take behavior are unchanged.

Local check on the new head: repo cap gate 54/54; affine_build_in_hot plus the six existing ownership controls pass interpreted, C with 1/4 threads, and JS on Bun. The mixed regression prints 499500 / 999000 / 499500 in all four lanes. No mini-cluster, Metal, CUDA, M4 or new timing qualification is claimed.

Understood that this remains held: I have not measured the server workload, and the reported @unsafe compatibility change still needs the requested compiler-owner sign-off. I updated the PR body to make the hold explicit and label the old timings as historical, rather than a production speedup. This follow-up was AI-assisted with GPT-6.1 Sol (openai-codex/gpt-6.1-sol); not human-review attestation.

@aldeni

aldeni commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Measured, as asked. Short version: on this program the PR is neutral, and the emitted C says why.

Setup. Same machine as #1559 (Linux x86-64, 4 cores). Compilers: main b54763b2 (the main side of the PR's merge) against the PR head 7896a14e, so the only difference is the PR's six lines. The program is the server from the issue, built natively by each compiler, one process at 4 threads, the same load generator and A/B/B/A order as the profile: 3 s warm-up, three 8 s rounds, median round, the machine under 0.35 busy cores before every cell, round spread ≤ 5.9%. Two list-building routes, means of the two runs per variant:

route clients main req/s PR req/s Δ CPU µs/req main → PR RSS MB
A 1 4,668 4,660 −0.2% 166.7 → 166.4 10.3 → 10.2
A 16 8,225 8,208 −0.2% 168.7 → 168.9 10.9 → 10.9
B 1 4,017 3,949 −1.7%* 191.7 → 191.9 10.9 → 10.9
B 16 7,504 7,544 +0.5% 181.7 → 181.1 11.1 → 11.1

* the first cell of the run (main itself measured 4,070 there and 3,965 in its second pass). I read all four as a tie. An equality check over every route of the program printed the same output on both binaries, so nothing here hit the new stop on an @unsafe copy of a Kind(&1) value.

Why. The two emitted C files have the same line count; the PR removes 57 of 2,816 rfc_seal sites (2%), all in IO continuations (startup and bookkeeping), none on the request path, and the hot column of CID_T is identical. This codebase declares its data at &2 almost everywhere (thousands of List<&2, _>, no List<&1, _>; nearly every datatype is Data), so a rule about literal Kind(&1) builds has nothing to touch. The 28–33% in the profile is the cost of values that really are shared, a different question from the one this PR answers.

So the fix is sound and the micro gain is real, but this program isn't the "noticeably faster real app". If you have a workload in mind that builds affine lists in volume on its hot path, I'm happy to measure it the same way.

@oxura

oxura commented Oct 16, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, that answers the app question: this service is neutral. I won't count it as a production speedup, and I've updated the PR body to say so.

The remaining hold is the compiled-C @unsafe Kind(&1) / Array.fork behavior needing maintainer sign-off, not another run of your benchmark.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thanks @oxura for the fix, and @aldeni for the measurements. I'm closing this one.

The rule is sound for safe code: a node built at a literal Kind(&1) can't gain a count there. But @unsafe is meant to allow exactly that. It lets a + binder form at any kind, and what it gives up is the proof guarantees, not runtime behavior. Array.fork relies on it: Array is a Type, so forking one is an @unsafe copy of an affine value, and its comment promises that matching a handle copies its part for any T.

With this PR, that stops holding when the elements are affine. Forking an Array<List<U32>> and matching the leaf on both handles prints 999000 on main, but the PR's C build stops with bend: runtime fail-stop (ERR_RFCS from span_fade), while the interpreter and JS still print 999000. Matching only one handle still works, and Array<U32> forks are unaffected. A shared node's fields can't be counted after it's built, so the runtime can't recover from a raw field there.

Since the server measurements came out neutral, I'd rather keep @unsafe copies working on every backend than trade them for the micro-benchmark gain. If a real workload turns out to need this, it would have to come with a way to prove that no @unsafe copy can reach the raw node.

@oxura

oxura commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Agreed. I'll keep the existing @unsafe copy behavior. This optimization needs that reachability proof before it can be safe.

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.

One + on a List<&2, U32> seals every List cell a native build makes, List<&1, U32> ones included: a loop that shares nothing runs 2x slower

4 participants