Skip to content

perf(base): Array.show walks from the last element, so it copies each element's text once - #2285

Merged
nicolas-abril merged 2 commits into
bendlang:mainfrom
rbmr:perf/array-show-linear
Nov 24, 2026
Merged

nicolas-abril merged 2 commits into
bendlang:mainfrom
rbmr:perf/array-show-linear

Conversation

@rbmr

@rbmr rbmr commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Array.show was quadratic on the C lane. Each step passed the text shown so far to f and appended ", " to the result, and String.append copies its first argument, so the step at element k copied the whole prefix. It now walks from the last element to the first, as Array.write takes its elements, and puts each element's text in front of the rest. Every step copies only one element's text.

f is still called with "[" for the first element and ", " for the others, as Array.write calls it. Under the contract in the comment above Array.write, "f(s, x) being s followed by x's text", the output is the same string.

Timings

Array.show(~U32, ~sh, Array.new(U32, dn, 7)) with sh(s, x) = s ++ U32.show(x), printing the length. C lane, one thread, pinned to one core, best of 3 runs (one run for main at 65,536 cells):

cells main this
1,024 11 ms 3 ms
4,096 183 ms 2 ms
16,384 999 ms 4 ms
65,536 57.5 s 5 ms
262,144 16 ms
1,048,576 58 ms

Bun, Node 24 and the interpreter take the same time before and after (Bun 33 ms, Node 32–44 ms, interpreter about 300 ms at 16,384 and 65,536 cells). The JS lane prints a pure main's arrays through Array.show, and the walk stays a tail loop, so it does not deepen the stack.

Verified

Linux x86_64, Bun 1.4.0, clang 23.1.6:

  • I compared the old definition, kept under another name, with the new one on arrays of 1 to 1,024 distinct U32s, with two fs, and on arrays of Strings that hold quotes. Every case printed the same string on the C lane, Bun, Node 24 and the interpreter.
  • The same program under -fsanitize=address,undefined -DBEND_POISON, at 1 and 4 threads, reported no errors.
  • I ran all 1,079 tests under tests/ on the JS lane with and without this change, and every test gets the same verdict. tests/show/array_elements.bend passes on the C lane, the JS lane and the interpreter.

@nicolas-abril
nicolas-abril merged commit f7a5f62 into bendlang:main Nov 24, 2026
1 check passed
nicolas-abril added a commit to nicolas-abril/bend that referenced this pull request Nov 24, 2026
, bendlang#2224-bendlang#2226, bendlang#2231, bendlang#2285, bendlang#2286, bendlang#2288 and others)

One conflict, in emit_unfold: bendlang#2288 annotates an unfolded call's lets with
the argument types instantiated by the earlier arguments; bendlang#2162's deep unfold
keeps its arguments unannotated there and annotates them the same way after
folding (its type walk renamed D, beside bendlang#2288's T). Both kept.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 participants