Skip to content

prelude/skstore: avoid temporary collections (Sequence::toArray, direct getArray, more mapToArray sites) - #1715

Merged
mbouaziz merged 8 commits into
mainfrom
feat/avoid-temporary-collections
Nov 19, 2026
Merged

mbouaziz merged 8 commits into
mainfrom
feat/avoid-temporary-collections

Conversation

@mbouaziz

@mbouaziz mbouaziz commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-ups to #1707 (Sequence::mapToArray) and the now-redundant #1704: more places where temporary collections can be avoided. Eight small commits, one logical change each:

  1. prelude: add Sequence::toArray — new Sequence::toArray(): Array<T> API (base default collect(Array)). IndexedSequence overrides it with a single Array::fillBy pass, Bytes fills directly from its native buffer via get, and Array copies with fillBy (the result must be frozen, so aliasing this is unsound). Vector/List/Queue/Set/SortedSet already had toArray and now override the base method. Includes ToArrayTest; bumps std 0.7.0 -> 0.8.0 (minor: new API).
  2. prelude: use toArray instead of collect(Array) in String::join and poll — String::join goes through Sequence::toArray (dispatching to the direct implementations); the System poll handler converts its read slice via Bytes::toArray, resolving a TODO: Get rid of .collect(Array).
  3. skstore: build getArray results directly from the match range — IFixedDir::getArray/getArraySourceKey previously did getAll(...).map(...).collect(Array) (iterator -> Vector -> Array). They now use a new findAllRange helper (findFirstBy + linear count, shared with findAllBy) and one Array::fillBy over the matching index range.
  4. prelude: avoid temporary collections at more call sites — mapToArray where a mapped Vector was immediately copied to an Array (RangeMap.fromRangeMap, JSON object fields, Doc.inspect), and toArray() instead of collect(Array) on Vectors (IO.read_to_string/read_line, Unsafe hex formatting), resolving the remaining collect(Array) TODOs.
  5. prelude: implement Sequence::join calling toString once per element — Sequence::join no longer does values().collect(Vector).join(separator) (Vector + Array temporaries). It renders each element with toString exactly once, collecting the rendered strings (with separators interleaved) into a single Vector<String>, then flattens that into the output char buffer. The Vector::join/List::join overrides that built intermediate collections to reach Array::join are removed in favor of the base implementation. Adds JoinTest.
  6. prelude: String::join delegates to Sequence::join — String::join(pieces) is now pieces.join(this), so it no longer materializes pieces.toArray() first; Array keeps its optimized join override.
  7. prelude: slice the popen read buffer once per read — the poll handler evaluated buf.slice(0, nbytes) twice per read (once for captured.extend, once for the handler string); the slice is bound once and passed to both.
  8. skstore: fix findAllBy/findAllRange docstrings — findAllBy's docstring had ended up stacked above findAllRange; both are now documented properly (findAllRange returns (i, n) with matches exactly i..i+n-1, n == 0 when none).

Not done (needs compiler/native work, out of scope here)

  • Array::filter/filterMap single-pass builds would need an Array shrink/truncate native (none exists).
  • Iterator-sourced collects (items(), keys(), getIter()) can't avoid the Vector temp without indexed access; Vector::mcreateFromIterator already presizes from Iterator::sizeHint.

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: 145/145 pass, including JoinTest (join over Array/Vector/List/Range, empty sequences, custom Show elements), ToArrayTest, MapToArrayTest, and all testCompactFixed* suites (which exercise getArray/getArraySourceKey over wide key/source ranges).
  • All touched files formatted with skfmt.
  • CircleCI on this branch's head.

Notes

  • std bumped 0.7.0 -> 0.8.0 in the toArray commit (minor: new API).
  • AI-assisted: implemented with Mistral Vibe (commit messages and this PR description drafted with its help; Co-Authored-By trailers on the commits).

mbouaziz and others added 8 commits November 19, 2026 14:13
Adds Sequence::toArray, returning an Array with the sequence's items.
IndexedSequence overrides it with a single Array::fillBy pass, Bytes
fills directly from its native buffer, and Array copies via fillBy (the
result must be frozen, so aliasing `this` is not sound). Vector, List,
Queue, Set and SortedSet already had toArray and now override the base
method. Base default: collect(Array).

std bumped to 0.7.0 (minor: new prelude API).

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
String::join now goes through Sequence::toArray (dispatching to the
direct implementations), and the System poll handler converts the read
buffer slice via Bytes::toArray, resolving the last TODO about
.collect(Array).

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
IFixedDir::getArray/getArraySourceKey built their result via
getAll(...).map(...).collect(Array) (iterator -> Vector -> Array). They
now use findAllRange (findFirstBy plus a linear count, shared with
findAllBy) and a single Array::fillBy over the matching range, avoiding
the intermediate Vector.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Use mapToArray where a mapped Vector was immediately copied to an Array
(RangeMap.fromRangeMap, JSON object fields, Doc.inspect), and toArray
instead of collect(Array) on Vectors (IO.read_to_string/read_line,
Unsafe hex formatting), resolving the existing TODOs -- collect(Array)
on a Vector goes iterator -> Vector -> Array.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Sequence::join previously did values().collect(Vector).join(separator),
which routed through Vector::join's toArray(). It now renders each
element with toString exactly once, collecting the strings (with
separators interleaved) into a single Vector that is then flattened into
the output buffer. The Vector::join and List::join overrides, which built
intermediate collections just to call Array::join, are removed in favor
of the base implementation.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
String::join(pieces) now calls pieces.join(this), dispatching to the
direct implementations instead of materializing pieces.toArray() first.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
The poll handler evaluated buf.slice(0, nbytes) twice per read (once for
captured.extend, once for the handler string) and then copied the slice
again with toArray(); the slice is already an Array, so bind it once and
pass it to both.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
The findAllBy docstring ended up stacked above findAllRange and findAllBy
itself had none; findAllRange's contract was also imprecise about the
no-match case. Document both properly: findAllRange returns (i, n) with
matches exactly i..i+n-1 (n == 0 when none), findAllBy enumerates them.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
@mbouaziz
mbouaziz force-pushed the feat/avoid-temporary-collections branch from 8b3143c to 3881578 Compare November 19, 2026 14:21
@mbouaziz
mbouaziz merged commit 96d46e4 into main Nov 19, 2026
21 checks passed
@mbouaziz
mbouaziz deleted the feat/avoid-temporary-collections branch November 19, 2026 14:58
mbouaziz added a commit that referenced this pull request Nov 19, 2026
## 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 `count - 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.

## On slice-copy functions vs iteration

Investigated while doing this:

- **Array → Array:** element-wise loops are already native —
`Array::unsafe_get`/`unsafe_set` are `native` direct accesses, so a
simple copy loop compiles to a tight load/store loop that LLVM
auto-vectorizes to memcpy speed (the existing private
`Array::unsafeMoveSlice` is exactly this pattern). A dedicated native
memmove wrapper would save little beyond what `unsafe_*` already skip
(bounds checks).
- **String → buffer:** char iteration is *not* memcpy-like (UTF-8 decode
+ `?Char` boxing per char, see above), which is why the byte-level copy
matters here. There is currently no `memcpy` exposed in the prelude
`Unsafe` module, so a native `Bytes`/`Array` slice-copy would be
compiler/native work — not needed for this change since the per-byte
loop over native `get`/`set` is already a tight vectorizable loop.

## 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 `JoinTest`s and a new
`testJoinMultibyte` (multibyte elements/separators: `héllo, wörld, 🎉`,
`α→β→γ`).
- `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).
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