Skip to content

prelude: make Sequence::join single-pass and byte-level - #1716

Merged
mbouaziz merged 2 commits into
mainfrom
feat/join-single-pass
Nov 19, 2026
Merged

mbouaziz merged 2 commits into
mainfrom
feat/join-single-pass

Conversation

@mbouaziz

@mbouaziz mbouaziz commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Optimizes Sequence::join (merged in #1715) along the lines suggested in review:

  1. Count during the single pass. The total output size is now accumulated in the same each pass that renders the elements (toString still called exactly once per element — there is a single call site), instead of a second pass over the collected strings calling String::length(). The separator's length is computed once (separator.bytes().size()) and multiplied by parts.size() - 1.
  2. Byte-level join. Strings are natively UTF-8 (skiplang/prelude/runtime/string.c: sk_string_t is a raw byte buffer). The old fill loop iterated chars via String::each, which goes through StringIterator::nextCode — a branchy per-char UTF-8 decode plus ?Char option boxing per char (src/native/StringIterator.sk). The new implementation collects zero-copy Bytes views (String::bytes()), copies raw bytes with the native Bytes::get into an Array<UInt8>, and builds the result with String::fromUtf8, which is a single native memcpy (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 a libstd.sklib built from this branch, emitting LLVM IR (--emit llvm-ir --release -O 2) and disassembling the binary:

  • The join fill loop is emitted without any vectorization: zero <N x i8>/shufflevector instructions in the module. The per-byte loop body is tail call @SKIP_String_getByte(...) + array-size load + bounds-check branch (sk.throwOutOfBounds) + scalar store 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.
  • What is fast already: the counting pass is O(1) per element (SKIP_String_byteSize), Array::mfill zeroes via memset, and SKIP_String__fromUtf8 is one memcpy in C.
  • Array→Array loops over unsafe_get/unsafe_set are pure load/store loops (those natives are compiler intrinsics, inlined — no calls), so they can vectorize; the existing private Array::unsafeMoveSlice relies on this. But unsafe_* is private to Array, and the public set adds a bounds check.
  • Conclusion: a native slice-copy would be faster for the fill loop — e.g. a memcpy-based String/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.sh with the pinned stage0 toolchain (CI-equivalent) in a clean worktree at the branch tip: 146/146 pass, including all JoinTests and testJoinMultibyte (multibyte elements/separators: héllo, wörld, 🎉, α→β→γ).
  • Codegen inspected via --emit llvm-ir + objdump as described above.
  • skfmt clean; CircleCI on the branch head.
  • std bumped 0.8.0 -> 0.8.1 (patch: internal performance fix, no API change).

AI-assisted: implemented with Mistral Vibe (Co-Authored-By trailers on the commits).

@mbouaziz
mbouaziz force-pushed the feat/join-single-pass branch from 1739774 to 4156f00 Compare November 19, 2026 16:45
mbouaziz and others added 2 commits November 19, 2026 16:46
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
mbouaziz force-pushed the feat/join-single-pass branch from 4156f00 to 915486d Compare November 19, 2026 16:46
@mbouaziz
mbouaziz enabled auto-merge November 19, 2026 16:49
@mbouaziz
mbouaziz merged commit a7c96f9 into main Nov 19, 2026
21 checks passed
@mbouaziz
mbouaziz deleted the feat/join-single-pass branch November 19, 2026 17:05
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.

1 participant