collections: win back the allocations of ArrayList over Buffer, and its cost table (follow-up of #526) #566

Closed
opened 2026-10-29 06:35:30 +00:00 by orion · 11 comments
Collaborator

Context

Follow-up of #526 (PR #529, step 3 of #55; it is step 3b of the epic's checklist). ArrayList over Buffer raises the allocation budget (.allocations) of 156 conformance programs by +8514 blocks in total: PR #529 records old and new counts per program in its body and raises the budgets as a deliberate cost of slice 1. The four largest rises are network-lookup +2106, parallel-ordered +2041, parallel-texts +1003 and parallel-sources +781 (together 5931, 70 % of the total); the other 152 programs rise by 1 to 229 blocks each, most of them by under 10. #526's acceptance asked that no budget rises and that the PR prints the tools/collection-cost.sh table (record section 6, "no slower than before"); neither is met by #529.

What is already known, from std/collections/src/list.trb and docs/PERFORMANCE.md section 1.1:

  • The ArrayList record is three words (items: Buffer, start, count, 24 bytes), so it is Inline (at most 32 bytes): the record itself is not a block. The extra blocks come from somewhere else, and nobody has measured where. Candidate sources, to be confirmed or dropped by measurement and not assumed: the adoption of runtime lists through ArrayList.over (what the natives of network-lookup and parallel-* hand over), a window made by slice and then rebased by own() on the next write (items.slice(...) is a copy), window() in replace/compact, the ListIterator of a loop that is not counted (it holds the whole record, 32 bytes, on the edge of the inline limit), and a list that stays shared with a live iterator or slice when it is written (Buffer.append and Buffer.set copy while the storage has a second owner).
  • A nested write a[i][j] = v and a[i].f = v on an ArrayList takes the read-modify-write round trip until #565 lands, which copies the inner value once per write; the rises of nested-write, list-update-nested, array-matrix, counted-for-write-to-list and collection-places may be that and not this issue's.

Goal

The extra blocks of the record over Buffer are attributed per source, every source the lowering or the std bodies can avoid is removed, every budget that drops is lowered, and the cost of slice 1 is measured and printed.

Design record point

docs/design/COLLECTION-STORAGE.md section 6 ("no slower than before", the allocation count of a program must not rise, the table of benchmarks), decisions 1 to 5 (the record, the shared slice and its rebase, literals and for), section 8 slice 1 (done by #526; this finishes its cost clause). Epic #55, step 3b; docs/ROADMAP.md names the epic's criterion "Benchmarks no slower than before", which rests on this issue.

Requirements

  • R1. The PR shall attribute the +8514 blocks to named sources (for example: adoption through ArrayList.over, rebase of a window, iterator record, shared storage copied on a write, round trip of an element write), with the blocks per occurrence and the programs affected, measured in the forge CI (tier B conformance counts, sh benchmarks/run.sh --allocations for the benchmarks) and not guessed; the table is in the PR body.
  • R2. The attribution shall cover at least the four largest programs (network-lookup, parallel-ordered, parallel-texts, parallel-sources) and one small program of each family among the rest (a literal, a slice, a nested write, an iterator), so the long tail of small rises is explained by the same sources.
  • R3. When a source can be avoided by a change in std/collections/src/list.trb or in the lowering (ir/lower/collection.trb, the counted loop ir/counted.trb, ir/instantiate.trb) without a new native, a new syntax form or a new name the compiler looks up by string, the PR shall remove it; the observable behaviour (record section 2, rule 1) shall not change.
  • R4. When a source cannot be avoided without breaking a decision of the record (for example a block the shared slice needs), the PR shall record it in COLLECTION-STORAGE.md section 6 as a decided by the loop paragraph with its blocks per occurrence and the budgets that keep it, and shall file a loop issue for any avoidable remainder that needs about 1 h or more.
  • R5. When a budget drops below the value #529 set, the PR shall lower the .allocations file to the count CI measures (sh tools/conformance.sh --update-budgets is the author's tool; the worker reads the counts from the tier B log, whose closing note names every program under its budget, and writes the numbers, never runs the suite locally).
  • R6. The PR body shall print the tools/collection-cost.sh table of tier B (fastest of nine runs, merge base against branch, the same job) for list-index, list-iterate, array-write, buffer-write, bytes-pass, list-slice, list-write-shared, list-literal, with the baselines of #468 as reference; a row worse than 1.00 plus the script's own run-to-run noise shall be fixed or decided with the numbers in COLLECTION-STORAGE.md, as record section 6 says.
  • R7. docs/design/COLLECTION-STORAGE.md shall be updated: the status line (its element steps and its allocations are open) and section 6 shall say what was measured and what remains.

Acceptance criteria

  • The PR body has the attribution table of R1 and R2: source, blocks per occurrence, programs and total blocks, and for each source one of "removed in this PR" or "kept, because ..." (R4).
  • Every source that adds 100 blocks or more to any single program (the 9 rows of #529's table with +100 or more, from network-lookup +2106 down to encoding-decode-problems +115) is removed or recorded under R4; the sum of the remaining rises against the pre-#529 budgets is stated in the PR, and it is smaller than +8514 by every block removed.
  • The .allocations files of every program whose count dropped are lowered to the counts of the tier B log; no budget is above the value #529 set; the conformance gate (.allocations exact or below) is green.
  • The cost table of R6 is in the PR; every row is at most 1.00 plus the noise the script measures, or the row is decided with its numbers in COLLECTION-STORAGE.md; list-slice for a type without a destructor still equals the shared form, and list-literal still allocates once per literal.
  • slice-close-generic and destructor-slice-concrete (the Close-item programs of #468) still pass; the conformance programs and doc tests run unchanged, and the torb run --vm results of list-index and list-iterate are reported and not wider than on main (#482 stays its own issue).
  • COLLECTION-STORAGE.md updated per R7; docs gates and, if a doc comment of std/collections changed, the regenerated docs/standard-library pages and skills/ (torb docs skill docs skills).
  • Gates green in both tiers (CI).

Seed

No expected: the work uses forms the seed already reads (bodies in std/collections, recognition by shape in compiler/src/ir). If a source can only be removed with a new native, a syntax form or a name the compiler looks up by string, that part is not in this PR: record it under R4 and file it as its own loop issue (two-commit rule, seed refresh stays Torben's).

Size

One PR, one or two Conventional Commits: perf(collections): ... or perf(ir): ... for the removals with the lowered budgets and the doc update, and the cost table in the PR body (no code). Attribution happens first, on the PR branch opened as a draft, read from the CI logs; the removals follow in the same PR. The worker never runs the bootstrap, the gates or a LTO build in its own exec (CI only); locally only torb format --check on touched files.

Dependencies

  • #526 / PR #529: merged (lands first, done). Epic #55.
  • PR #838 (closes #565, step 3a): open, not merged when this was refined. It edits compiler/src/ir/elements.trb, ir/explain-copies.trb, ir/counted.trb, compiler/tests/harness.trb, tests/command-line/copies and the status line of COLLECTION-STORAGE.md, and it removes the round trip of an element write on an ArrayList, which may by itself lower the budgets of the nested-write programs. Consequences for the work: (a) start from main as it is then; if #838 has merged, rebase on it and take the attribution on top of it, so its share is not counted as this issue's; (b) if #838 is still open when the work starts, do the attribution of R1 and R2 on a branch off main, do not edit the files #838 touches, and if a removal needs one of them, comment waiting (PR #838) instead of stacking on its branch or duplicating its change; (c) rebase over #838 before the budgets are written, because both PRs edit .allocations files and the COLLECTION-STORAGE.md status line.
  • #837 (harness prelude, tests only) is independent. #482 (VM list-iterate) is related, not a blocker.

Out of scope

TrieList and the other slices of #55 (slices 2 to 7), rewriting the five bulk Buffer rows, #482, the seed refresh, any change that moves observable behaviour or a documented cost.

Decided by the loop

  • Measure in CI first, remove what the lowering can avoid, record the rest with numbers: no owner decision, it is performance work with a measurable outcome (loop contract, "Definition of ready").
  • The "no budget rises" criterion of #526 is kept as the aim; where a block is the price of a decision of the record (shared slice, rebase), the numbers go into COLLECTION-STORAGE.md instead of reverting the decision.
  • One PR, because the sources are found by the same measurement and fixed in the same few files; if the attribution shows removals in unrelated areas (lowering and std bodies and runtime), the worker files the later ones as loop issues (R4).
## Context Follow-up of #526 (PR #529, step 3 of #55; it is step 3b of the epic's checklist). `ArrayList` over `Buffer` raises the allocation budget (`.allocations`) of 156 conformance programs by +8514 blocks in total: PR #529 records old and new counts per program in its body and raises the budgets as a deliberate cost of slice 1. The four largest rises are `network-lookup` +2106, `parallel-ordered` +2041, `parallel-texts` +1003 and `parallel-sources` +781 (together 5931, 70 % of the total); the other 152 programs rise by 1 to 229 blocks each, most of them by under 10. #526's acceptance asked that no budget rises and that the PR prints the `tools/collection-cost.sh` table (record section 6, "no slower than before"); neither is met by #529. What is already known, from `std/collections/src/list.trb` and `docs/PERFORMANCE.md` section 1.1: - The `ArrayList` record is three words (`items: Buffer`, `start`, `count`, 24 bytes), so it is `Inline` (at most 32 bytes): the record itself is not a block. The extra blocks come from somewhere else, and nobody has measured where. Candidate sources, to be confirmed or dropped by measurement and not assumed: the adoption of runtime lists through `ArrayList.over` (what the natives of `network-lookup` and `parallel-*` hand over), a window made by `slice` and then rebased by `own()` on the next write (`items.slice(...)` is a copy), `window()` in `replace`/`compact`, the `ListIterator` of a loop that is not counted (it holds the whole record, 32 bytes, on the edge of the inline limit), and a list that stays shared with a live iterator or slice when it is written (`Buffer.append` and `Buffer.set` copy while the storage has a second owner). - A nested write `a[i][j] = v` and `a[i].f = v` on an `ArrayList` takes the read-modify-write round trip until #565 lands, which copies the inner value once per write; the rises of `nested-write`, `list-update-nested`, `array-matrix`, `counted-for-write-to-list` and `collection-places` may be that and not this issue's. ## Goal The extra blocks of the record over `Buffer` are attributed per source, every source the lowering or the std bodies can avoid is removed, every budget that drops is lowered, and the cost of slice 1 is measured and printed. ## Design record point `docs/design/COLLECTION-STORAGE.md` section 6 ("no slower than before", the allocation count of a program must not rise, the table of benchmarks), decisions 1 to 5 (the record, the shared slice and its rebase, literals and `for`), section 8 slice 1 (done by #526; this finishes its cost clause). Epic #55, step 3b; `docs/ROADMAP.md` names the epic's criterion "Benchmarks no slower than before", which rests on this issue. ## Requirements - R1. The PR shall attribute the +8514 blocks to named sources (for example: adoption through `ArrayList.over`, rebase of a window, iterator record, shared storage copied on a write, round trip of an element write), with the blocks per occurrence and the programs affected, measured in the forge CI (tier B conformance counts, `sh benchmarks/run.sh --allocations` for the benchmarks) and not guessed; the table is in the PR body. - R2. The attribution shall cover at least the four largest programs (`network-lookup`, `parallel-ordered`, `parallel-texts`, `parallel-sources`) and one small program of each family among the rest (a literal, a slice, a nested write, an iterator), so the long tail of small rises is explained by the same sources. - R3. When a source can be avoided by a change in `std/collections/src/list.trb` or in the lowering (`ir/lower/collection.trb`, the counted loop `ir/counted.trb`, `ir/instantiate.trb`) without a new native, a new syntax form or a new name the compiler looks up by string, the PR shall remove it; the observable behaviour (record section 2, rule 1) shall not change. - R4. When a source cannot be avoided without breaking a decision of the record (for example a block the shared slice needs), the PR shall record it in COLLECTION-STORAGE.md section 6 as a `decided by the loop` paragraph with its blocks per occurrence and the budgets that keep it, and shall file a `loop` issue for any avoidable remainder that needs about 1 h or more. - R5. When a budget drops below the value #529 set, the PR shall lower the `.allocations` file to the count CI measures (`sh tools/conformance.sh --update-budgets` is the author's tool; the worker reads the counts from the tier B log, whose closing note names every program under its budget, and writes the numbers, never runs the suite locally). - R6. The PR body shall print the `tools/collection-cost.sh` table of tier B (fastest of nine runs, merge base against branch, the same job) for `list-index`, `list-iterate`, `array-write`, `buffer-write`, `bytes-pass`, `list-slice`, `list-write-shared`, `list-literal`, with the baselines of #468 as reference; a row worse than 1.00 plus the script's own run-to-run noise shall be fixed or decided with the numbers in COLLECTION-STORAGE.md, as record section 6 says. - R7. `docs/design/COLLECTION-STORAGE.md` shall be updated: the status line (its element steps and its allocations are open) and section 6 shall say what was measured and what remains. ## Acceptance criteria - [ ] The PR body has the attribution table of R1 and R2: source, blocks per occurrence, programs and total blocks, and for each source one of "removed in this PR" or "kept, because ..." (R4). - [ ] Every source that adds 100 blocks or more to any single program (the 9 rows of #529's table with +100 or more, from `network-lookup` +2106 down to `encoding-decode-problems` +115) is removed or recorded under R4; the sum of the remaining rises against the pre-#529 budgets is stated in the PR, and it is smaller than +8514 by every block removed. - [ ] The `.allocations` files of every program whose count dropped are lowered to the counts of the tier B log; no budget is above the value #529 set; the conformance gate (`.allocations` exact or below) is green. - [ ] The cost table of R6 is in the PR; every row is at most 1.00 plus the noise the script measures, or the row is decided with its numbers in COLLECTION-STORAGE.md; `list-slice` for a type without a destructor still equals the shared form, and `list-literal` still allocates once per literal. - [ ] `slice-close-generic` and `destructor-slice-concrete` (the `Close`-item programs of #468) still pass; the conformance programs and doc tests run unchanged, and the `torb run --vm` results of `list-index` and `list-iterate` are reported and not wider than on `main` (#482 stays its own issue). - [ ] COLLECTION-STORAGE.md updated per R7; docs gates and, if a doc comment of `std/collections` changed, the regenerated `docs/standard-library` pages and `skills/` (`torb docs skill docs skills`). - [ ] Gates green in both tiers (CI). ## Seed No expected: the work uses forms the seed already reads (bodies in `std/collections`, recognition by shape in `compiler/src/ir`). If a source can only be removed with a new native, a syntax form or a name the compiler looks up by string, that part is not in this PR: record it under R4 and file it as its own `loop` issue (two-commit rule, seed refresh stays Torben's). ## Size One PR, one or two Conventional Commits: `perf(collections): ...` or `perf(ir): ...` for the removals with the lowered budgets and the doc update, and the cost table in the PR body (no code). Attribution happens first, on the PR branch opened as a draft, read from the CI logs; the removals follow in the same PR. The worker never runs the bootstrap, the gates or a LTO build in its own exec (CI only); locally only `torb format --check` on touched files. ## Dependencies - #526 / PR #529: merged (lands first, done). Epic #55. - PR #838 (closes #565, step 3a): **open, not merged** when this was refined. It edits `compiler/src/ir/elements.trb`, `ir/explain-copies.trb`, `ir/counted.trb`, `compiler/tests/harness.trb`, `tests/command-line/copies` and the status line of COLLECTION-STORAGE.md, and it removes the round trip of an element write on an `ArrayList`, which may by itself lower the budgets of the nested-write programs. Consequences for the work: (a) start from `main` as it is then; if #838 has merged, rebase on it and take the attribution on top of it, so its share is not counted as this issue's; (b) if #838 is still open when the work starts, do the attribution of R1 and R2 on a branch off `main`, do not edit the files #838 touches, and if a removal needs one of them, comment `waiting (PR #838)` instead of stacking on its branch or duplicating its change; (c) rebase over #838 before the budgets are written, because both PRs edit `.allocations` files and the COLLECTION-STORAGE.md status line. - #837 (harness prelude, tests only) is independent. #482 (VM `list-iterate`) is related, not a blocker. ## Out of scope `TrieList` and the other slices of #55 (slices 2 to 7), rewriting the five bulk `Buffer` rows, #482, the seed refresh, any change that moves observable behaviour or a documented cost. ## Decided by the loop - Measure in CI first, remove what the lowering can avoid, record the rest with numbers: no owner decision, it is performance work with a measurable outcome (loop contract, "Definition of ready"). - The "no budget rises" criterion of #526 is kept as the aim; where a block is the price of a decision of the record (shared slice, rebase), the numbers go into COLLECTION-STORAGE.md instead of reverting the decision. - One PR, because the sources are found by the same measurement and fixed in the same few files; if the attribution shows removals in unrelated areas (lowering and std bodies and runtime), the worker files the later ones as `loop` issues (R4).
Author
Collaborator

[loop:orion] labelled loop: live-design follow-up of #526 / PR #529 (epic #55), filed by an interactive session without the label. Not superseded; no retirement.

[loop:orion] labelled `loop`: live-design follow-up of #526 / PR #529 (epic #55), filed by an interactive session without the label. Not superseded; no retirement.
Author
Collaborator

[loop:torbscript] intake: in scope (author orion, label loop), measurement and allocation follow-up of #526 (epic #55); not a duplicate (#565 is the element-step/--explain-copies half), no collision with a design record (COLLECTION-STORAGE.md is the spec). No seed impact expected. next: refine (queued, dependency #526 / PR #529 landed)

[loop:torbscript] intake: in scope (author orion, label loop), measurement and allocation follow-up of #526 (epic #55); not a duplicate (#565 is the element-step/--explain-copies half), no collision with a design record (COLLECTION-STORAGE.md is the spec). No seed impact expected. next: refine (queued, dependency #526 / PR #529 landed)
Author
Collaborator

[loop:torbscript] refine: next: waiting (the ready buffer is full, 12 ready of 4; refinement resumes when it drains)

[loop:torbscript] refine: next: waiting (the ready buffer is full, 12 ready of 4; refinement resumes when it drains)
Author
Collaborator

[loop:torbscript] claim refine by session:loop-torbscript-566-refine until 2026-11-16T17:00:00Z
Reason: the ready buffer fell to 3 of 4 (#836, #565, #365) and #566 was starved (deferred 3 times); one refine fits.

[loop:torbscript] claim refine by session:loop-torbscript-566-refine until 2026-11-16T17:00:00Z Reason: the ready buffer fell to 3 of 4 (#836, #565, #365) and #566 was starved (deferred 3 times); one refine fits.
Author
Collaborator

[loop:torbscript] decided by the loop: #566 is performance work with a measurable outcome, so no owner question. The body now has EARS requirements R1 to R7, acceptance criteria that CI can check, the seed answer (no) and one PR as its size. The attribution of the +8514 blocks is measured in forge CI first (the record is an inline 24-byte struct, so the extra blocks are not the record itself), removals follow in the same PR, what cannot be removed is recorded with its numbers in COLLECTION-STORAGE.md. Dependency made explicit: PR #838 (#565) is open and not merged; it edits ir/elements.trb, ir/explain-copies.trb, ir/counted.trb, the harness and the same .allocations / status-line files, so the worker rebases on it if it has landed, and otherwise does not edit those files and waits on it instead of stacking.

[loop:torbscript] decided by the loop: #566 is performance work with a measurable outcome, so no owner question. The body now has EARS requirements R1 to R7, acceptance criteria that CI can check, the seed answer (no) and one PR as its size. The attribution of the +8514 blocks is measured in forge CI first (the record is an inline 24-byte struct, so the extra blocks are not the record itself), removals follow in the same PR, what cannot be removed is recorded with its numbers in COLLECTION-STORAGE.md. Dependency made explicit: PR #838 (#565) is open and not merged; it edits `ir/elements.trb`, `ir/explain-copies.trb`, `ir/counted.trb`, the harness and the same `.allocations` / status-line files, so the worker rebases on it if it has landed, and otherwise does not edit those files and waits on it instead of stacking.
Author
Collaborator

[loop:torbscript] release by session:loop-torbscript-566-refine
refine: #566 brought to the definition of ready (goal, EARS requirements, checkable acceptance criteria, design record points, seed: no, size: one PR, dependency on PR #838 explicit) and labelled ready. next: ready

[loop:torbscript] release by session:loop-torbscript-566-refine refine: #566 brought to the definition of ready (goal, EARS requirements, checkable acceptance criteria, design record points, seed: no, size: one PR, dependency on PR #838 explicit) and labelled `ready`. next: ready
Author
Collaborator

[loop:torbscript] implement: held until PR #838 (#565) is merged, because both change the ArrayList element-step passes and this issue builds on them; implementation starts on the merged main. next: waiting (PR #838 merge gate)

[loop:torbscript] implement: held until PR #838 (#565) is merged, because both change the ArrayList element-step passes and this issue builds on them; implementation starts on the merged main. next: waiting (PR #838 merge gate)
Author
Collaborator

[loop:torbscript] claim implement by session:loop-torbscript-566-impl until 2026-11-17T01:30:00Z Reason: PR #838 (#565) is merged and main is fixed by #840 (main CI running); one worker slot free, ready issue. The worker implements on current main in one PR.

[loop:torbscript] claim implement by session:loop-torbscript-566-impl until 2026-11-17T01:30:00Z Reason: PR #838 (#565) is merged and main is fixed by #840 (main CI running); one worker slot free, ready issue. The worker implements on current main in one PR.
Author
Collaborator

[loop:torbscript] release by session:loop-torbscript-566-impl implement: the worker ran into its 3 h limit while PR #841 (head 928f4256, draft, measurement probe at inline limit 48) was waiting on tier B of the forge CI; the PR is open and still WIP, the attribution and cost tables are not written yet, next: implement (fresh worker continues on PR #841)

[loop:torbscript] release by session:loop-torbscript-566-impl implement: the worker ran into its 3 h limit while PR #841 (head 928f4256, draft, measurement probe at inline limit 48) was waiting on tier B of the forge CI; the PR is open and still WIP, the attribution and cost tables are not written yet, next: implement (fresh worker continues on PR #841)
Author
Collaborator

[loop:torbscript] claim implement by session:loop-torbscript-566-impl2 until 2026-11-17T05:30:00Z Reason: PR #841 is open as a draft with a pending forge CI; a fresh worker continues on the same branch: reads the tier B log, writes the attribution and cost tables (R1, R2, R6), lowers the budgets and takes the PR out of WIP.

[loop:torbscript] claim implement by session:loop-torbscript-566-impl2 until 2026-11-17T05:30:00Z Reason: PR #841 is open as a draft with a pending forge CI; a fresh worker continues on the same branch: reads the tier B log, writes the attribution and cost tables (R1, R2, R6), lowers the budgets and takes the PR out of WIP.
Author
Collaborator

[loop:torbscript] release by session:loop-torbscript-566-impl2 implement: PR #841 is complete and out of draft (head dfe1a61a, rebased onto main 7e267913), but the forge CI has not finished: gates, tier A, tier B, pack the seed and archives were still pending after about 60 minutes (the runs waited about 40 minutes for a runner). Left: wait for green CI on dfe1a61a, in particular tier B, because the budget of the new php-lists program has not been measured at inline limit 48 and a budget that does not match fails conformance; then request Argus's review. What changed this round: the cost table (R6) and the noise decision are in docs/design/COLLECTION-STORAGE.md section 6 and in the PR body, with the attribution table (R1/R2); network-lookup is a sixth program above its #529 value (46742 against 46213, 623 under main) and joins #842. next: ci-wait on PR #841, then review

[loop:torbscript] release by session:loop-torbscript-566-impl2 implement: PR #841 is complete and out of draft (head dfe1a61a, rebased onto main 7e267913), but the forge CI has not finished: gates, tier A, tier B, pack the seed and archives were still pending after about 60 minutes (the runs waited about 40 minutes for a runner). Left: wait for green CI on dfe1a61a, in particular tier B, because the budget of the new `php-lists` program has not been measured at inline limit 48 and a budget that does not match fails conformance; then request Argus's review. What changed this round: the cost table (R6) and the noise decision are in `docs/design/COLLECTION-STORAGE.md` section 6 and in the PR body, with the attribution table (R1/R2); `network-lookup` is a sixth program above its #529 value (46742 against 46213, 623 under main) and joins #842. next: ci-wait on PR #841, then review
argus closed this issue 2026-11-17 00:28:53 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
torbscript/language#566
No description provided.