Repository navigation
prelude: make Sequence::join single-pass and byte-level - #1716
Merged
Merged
Conversation
mbouaziz
commented
Nov 19, 2026
mbouaziz
force-pushed
the
feat/join-single-pass
branch
from
November 19, 2026 16:45
1739774 to
4156f00
Compare
Sequence::join now counts the total output size during the single pass that renders each element (toString still called exactly once per element), computing the separator's length once and multiplying by the element count, instead of a second pass over the collected strings. Strings are natively UTF-8 (runtime string.c), so the join now happens at the byte level: parts are collected as zero-copy Bytes views whose size() is O(1), the fill loop copies raw bytes with native Bytes::get into an Array<UInt8>, and String::fromUtf8 (a single native memcpy) builds the result. This avoids the per-char UTF-8 decode plus ?Char boxing that String::each/String::length incur via StringIterator. Adds testJoinMultibyte to cover multibyte elements and separators. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
mbouaziz
force-pushed
the
feat/join-single-pass
branch
from
November 19, 2026 16:46
4156f00 to
915486d
Compare
mbouaziz
enabled auto-merge
November 19, 2026 16:49
This was referenced Nov 19, 2026
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.
Summary
Optimizes
Sequence::join(merged in #1715) along the lines suggested in review:eachpass that renders the elements (toStringstill called exactly once per element — there is a single call site), instead of a second pass over the collected strings callingString::length(). The separator's length is computed once (separator.bytes().size()) and multiplied byparts.size() - 1.skiplang/prelude/runtime/string.c:sk_string_tis a raw byte buffer). The old fill loop iterated chars viaString::each, which goes throughStringIterator::nextCode— a branchy per-char UTF-8 decode plus?Charoption boxing per char (src/native/StringIterator.sk). The new implementation collects zero-copyBytesviews (String::bytes()), copies raw bytes with the nativeBytes::getinto anArray<UInt8>, and builds the result withString::fromUtf8, which is a single nativememcpy(sk_string_create) with no validation. Concatenating valid UTF-8 strings at the byte level is exact, including multibyte chars.Slice-copy functions vs iteration — verified against compiled output
Checked by compiling a probe (
Vector<String>.join(", ")) with the pinned stage0 against alibstd.sklibbuilt from this branch, emitting LLVM IR (--emit llvm-ir --release -O 2) and disassembling the binary:<N x i8>/shufflevector instructions in the module. The per-byte loop body istail call @SKIP_String_getByte(...)+ array-size load + bounds-check branch (sk.throwOutOfBounds) + scalarstore i8— an external call and a panic branch per byte, which block LLVM vectorization. So the iterated String→Array copy is not optimized to memcpy speed.SKIP_String_byteSize),Array::mfillzeroes viamemset, andSKIP_String__fromUtf8is onememcpyin C.unsafe_get/unsafe_setare pure load/store loops (those natives are compiler intrinsics, inlined — no calls), so they can vectorize; the existing privateArray::unsafeMoveSlicerelies on this. Butunsafe_*is private toArray, and the publicsetadds a bounds check.memcpy-basedString/Bytes→Array<UInt8>block copy at an offset would turn each part into a single memcpy instead of a per-byte call loop. That needs a small runtime addition (not done here).Validation
cd skiplang/prelude && SKTEST_JOBS=8 ../../bin/run-sktest.shwith the pinned stage0 toolchain (CI-equivalent) in a clean worktree at the branch tip: 146/146 pass, including allJoinTests andtestJoinMultibyte(multibyte elements/separators:héllo, wörld, 🎉,α→β→γ).--emit llvm-ir+ objdump as described above.skfmtclean; CircleCI on the branch head.stdbumped0.8.0 -> 0.8.1(patch: internal performance fix, no API change).AI-assisted: implemented with Mistral Vibe (
Co-Authored-Bytrailers on the commits).