Repository navigation
perf(base): Array.show walks from the last element, so it copies each element's text once - #2285
Merged
Merged
Conversation
… element's text once
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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]>
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.
Array.showwas quadratic on the C lane. Each step passed the text shown so far tofand appended", "to the result, andString.appendcopies its first argument, so the step at element k copied the whole prefix. It now walks from the last element to the first, asArray.writetakes its elements, and puts each element's text in front of the rest. Every step copies only one element's text.fis still called with"["for the first element and", "for the others, asArray.writecalls it. Under the contract in the comment aboveArray.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))withsh(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):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 throughArray.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:
U32s, with twofs, and on arrays ofStrings that hold quotes. Every case printed the same string on the C lane, Bun, Node 24 and the interpreter.-fsanitize=address,undefined -DBEND_POISON, at 1 and 4 threads, reported no errors.tests/on the JS lane with and without this change, and every test gets the same verdict.tests/show/array_elements.bendpasses on the C lane, the JS lane and the interpreter.