Repository navigation
The public API is the same as in 4.8.0. The synchronous array helpers build
their results faster: from 64 elements, traverse, sequence(), compact(),
successes() and failures() write each value straight into the new array
instead of appending it, and run 1.9 to 5.7 times faster at 1,024 elements.
⚡ Performance
-
Array helpers write into the new array's storage.
appendkeeps the
array's count in memory, and in a loop that skips elements or can stop
early, each element waited for the previous one's count to be written and
read back. From 64 elements,traverse(both forms) andsequence()now
fill the new array throughArray(capacity:initializingWith:), so the count
stays in a register. A transform the compiler can see, such as$0 &* 2,
now compiles to SIMD instructions, andtraversewith it is about twice as
fast asArray.map. -
compact(),successes()andfailures()don't branch per element when
the values they keep hold no references, such asInt,Boolor an enum
without payloads. Every element writes a value and only a kept one advances
the output, so the speed no longer depends on whethernils or failures
come in a pattern the CPU can predict.separate()does the same from 128
elements, since it builds two arrays. -
At 1,024 elements, on an M4 Max:
Helper 4.8.0 4.9.0 Faster traverse417 ns 225 ns 1.9× traversereturningResult424 ns 211 ns 2.0× traversefailing halfway978 ns 242 ns 4.0× sequence()1,972 ns 423 ns 4.7× compact(), every thirdnil1,390 ns 359 ns 3.9× compact(), randomnils1,059 ns 362 ns 2.9× successes(), every third fails1,293 ns 357 ns 3.6× successes(), random failures985 ns 366 ns 2.7× failures(), every third fails997 ns 265 ns 3.8× failures(), random failures1,500 ns 264 ns 5.7× separate(), every third fails1,365 ns 641 ns 2.1× separate(), random failures1,094 ns 639 ns 1.7× The benchmarks reuse one array, and the CPU learns even a random pattern
across calls. Measured over 512 different random arrays instead, as in real
use,successes()andcompact()run about 7 times andfailures()12
times faster than in 4.8.0. -
Fewer allocations and releases.
failures()now reserves room for the
rest of the array at its first failure, assuccesses()already reserves
room for the whole array, so it allocates once instead of growing: one
allocation instead of six at 1,024 elements. An array without failures
still allocates nothing. Every rewritten helper also does one or two
releases fewer per call. -
What stays as it was. Arrays shorter than 64 elements keep the 4.8.0
loops, because the standard library allocates a buffer to fill about 16 ns
more slowly than one to append to. At 16 elements, the helpers run within 3%
of 4.8.0's instruction counts. Values that hold references, such as
Stringorany Error, are still appended incompact(),successes(),
failures()andseparate(), where skipping without a branch would cost
more than it saves. A transform the compiler can't inline costs the same as
before, since the call takes most of the time. -
New benchmarks cover
compact(),sequence(), atraversethat fails
halfway, andcompactMap { $0 }as the baseline forcompact(). The
splitting helpers andcompact()are also measured with failures ornils
at random positions.
🧪 Testing
- Hegel properties check
traverse,sequence(),compact(),successes(),
failures()andseparate()against the standard library on arrays from
empty up to 300 elements, with the lengths around 64 and 128 drawn more
often. Values includeInt, a one-byte enum, a type with no bytes,
String,Bool?andany Error. They also check thattraversestops
calling the transform at the first failure and releases every value it
made before it. Mutation checks confirmed the properties fail when the
output advances before its write and whensequence()misses anil. - Example tests cover each helper at 63, 64, 65, 127, 128, 129 and 1,000
elements.