Lists: one allocation per list again; [each acc, x] appends in place - #228
Merged
Merged
Conversation
The other accumulator idiom, f(i, [each acc, x]), copied acc on every step just as concat used to. A list literal or comprehension whose first contribution is `each <a list>` now starts as that list and appends the rest with listAppend (ListBuilder), on the VM's Accum* ops and the interpreter's evalListLiteral alike. A string, range or undef after `each` still expands as before, and `each` anywhere but first is unchanged. 50,000 steps of [each acc, i]: 6.1 s on 1.35.0, quadratic; 100,000 now take 0.06 s, and 400,000 0.19 s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Shared-prefix lists (1.35.0) gave each list a ValueList plus a separate shared_ptr-owned ListBuffer holding the vector: one more allocation per list than a plain vector, and more and odd-sized freed memory. Measured by instructions retired against 1.34.0, ordinary BOSL2 ran 1-5.6% slower, and a long docsgen's footprint rose 63% from freed large blocks the allocator kept (live heap unchanged). Now makeList puts the list and its buffer header in one block, adopting the element vector rather than copying it -- the same two allocations a plain vector cost -- and each list caches its element pointer, so a read is one hop. A list appended in place is a small ValueList viewing its parent's buffer and keeping the parent's block alive. Growth is x1.5 with no minimum, instead of x2 with at least 16 slots. ListItems is neither copyable nor movable, since an own-buffer view must not outlive its block; all list construction goes through makeList. Instructions retired vs 1.34.0 on nine BOSL2 scripts: -0.9% to +0.9% (1.35.0: +1.0% to +5.6%). Bytes allocated: +1.6% to +24% (1.35.0: +1.4% to +33.5%). A million appends still take 0.6 s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 2, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs BelfrySCAD/BelfrySCAD#668
This follows up 1.35.0's shared-prefix lists (#225). That change made accumulator loops linear but, measured properly, cost ordinary BOSL2 code 1–5.6% in instructions and inflated a long docs build's footprint. This PR keeps the linear appends and removes both costs. It also extends in-place appending to
[each acc, x].Why 1.35.0 regressed (profiled with
sample; heap inspected withheap/vmmap):ValueListheld ashared_ptrto a separately allocatedListBuffer, which held thestd::vector. That showed up as about +2.5% inmalloc/freeand +0.7% tearing down theListBuffercontrol block.Changes:
makeList: the list and its buffer header share one block, and the element vector is adopted, not copied. That's the same two allocations a plain vector cost.ValueListviewing its parent's buffer, keeping the parent's block alive through ashared_ptrthat points into it.ListItemsis neither copyable nor movable, so all list construction goes throughmakeList, which the compiler enforces.[each acc, x]: a list literal or comprehension whose first contribution iseach <list>now starts as that list (ListBuilder), on the VM and the interpreter. It was still quadratic: 50k steps took 6.1 s; 400k now take 0.2 s.Measured with instructions retired (
/usr/bin/time -l), which load doesn't affect. Wall-clock timings on this machine were distorted by unrelated load, and that's how #225 shipped a false claim.Nine BOSL2 scripts, against 1.34.0:
Bytes allocated: 1.35.0 was +1.4% to +33.5% over 1.34.0; this PR is +1.6% to +24%.
Full BOSL2 docs build:
shapes3d docs, after the run:
Append loops are still linear: a million
concatappends take 0.6 s, and #668's script stops with "Recursion detected" in under 2 s.Tested:
ctest: 2014 passed.leaks: 0.test_python_bindings.py: 61 passed, on a wheel from this branch.cubetrussimages with 1–28 edge pixels changed. Those come from Patch Manifold v3.5.2: fix a heap overflow in CsgLeafNode::Compose #227's Manifold patch, not from this PR: an unpatched build of this branch givescubetrussmeshes identical to 1.35.0's.🤖 Generated with Claude Code