Skip to content

Commit a098d73

Browse files
committed
perf(builtin): Iter::join accepts StringView separator
The other members of the join family — `Array::join`, `ArrayView::join`, `FixedArray::join`, `ReadOnlyArray::join`, `Deque::join` — already take `separator : StringView`. `Iter::join` was the outlier, taking `String`, which forced every StringView caller to do `separator.to_owned()` before calling it. This is an *expanding* mbti change: every existing caller keeps working (String literals and `String` values coerce to `StringView`), and StringView callers stop needing the owning copy. Internally the implementation swaps `write_string(sep)` for `write_view(sep)`, which is zero-copy on non-js StringBuilder. Supersedes #3458 — `Deque::join`'s body no longer needs to work around Iter::join's signature.
1 parent 55a95d6 commit a098d73

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

builtin/iterator.mbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,12 @@ pub fn[X] Iter::flatten(self : Iter[Iter[X]]) -> Iter[X] {
422422
///|
423423
/// Collects the elements of the iterator into a string.
424424
/// The old iterator `self` must not be used again after calling `join`.
425-
pub fn Iter::join(self : Iter[String], sep : String) -> String {
425+
pub fn Iter::join(self : Iter[String], sep : StringView) -> String {
426426
let result = StringBuilder::new()
427427
if self.next() is Some(x) {
428428
result.write_string(x)
429429
while self.next() is Some(x) {
430-
result.write_string(sep)
430+
result.write_view(sep)
431431
result.write_string(x)
432432
}
433433
}

builtin/pkg.generated.mbti

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ pub fn[X] Iter::intersperse(Self[X], X) -> Self[X]
283283
pub fn[X] Iter::iter(Self[X]) -> Self[X]
284284
#alias(iterator2)
285285
pub fn[X] Iter::iter2(Self[X]) -> Iter2[Int, X]
286-
pub fn Iter::join(Self[String], String) -> String
286+
pub fn Iter::join(Self[String], StringView) -> String
287287
#deprecated
288288
pub fn[X] Iter::just_run(Self[X], (X) -> IterResult) -> Unit
289289
pub fn[X] Iter::last(Self[X]) -> X?

0 commit comments

Comments
 (0)