Skip to content

4.9.0

Latest

Choose a tag to compare

@velocityzen velocityzen released this 19 Nov 16:07
87ec183

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. append keeps 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) and sequence() now
    fill the new array through Array(capacity:initializingWith:), so the count
    stays in a register. A transform the compiler can see, such as $0 &* 2,
    now compiles to SIMD instructions, and traverse with it is about twice as
    fast as Array.map.

  • compact(), successes() and failures() don't branch per element when
    the values they keep hold no references, such as Int, Bool or an enum
    without payloads. Every element writes a value and only a kept one advances
    the output, so the speed no longer depends on whether nils 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
    traverse 417 ns 225 ns 1.9×
    traverse returning Result 424 ns 211 ns 2.0×
    traverse failing halfway 978 ns 242 ns 4.0×
    sequence() 1,972 ns 423 ns 4.7×
    compact(), every third nil 1,390 ns 359 ns 3.9×
    compact(), random nils 1,059 ns 362 ns 2.9×
    successes(), every third fails 1,293 ns 357 ns 3.6×
    successes(), random failures 985 ns 366 ns 2.7×
    failures(), every third fails 997 ns 265 ns 3.8×
    failures(), random failures 1,500 ns 264 ns 5.7×
    separate(), every third fails 1,365 ns 641 ns 2.1×
    separate(), random failures 1,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() and compact() run about 7 times and failures() 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, as successes() 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
    String or any Error, are still appended in compact(), successes(),
    failures() and separate(), 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(), a traverse that fails
    halfway, and compactMap { $0 } as the baseline for compact(). The
    splitting helpers and compact() are also measured with failures or nils
    at random positions.

🧪 Testing

  • Hegel properties check traverse, sequence(), compact(), successes(),
    failures() and separate() against the standard library on arrays from
    empty up to 300 elements, with the lengths around 64 and 128 drawn more
    often. Values include Int, a one-byte enum, a type with no bytes,
    String, Bool? and any Error. They also check that traverse stops
    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 when sequence() misses a nil.
  • Example tests cover each helper at 63, 64, 65, 127, 128, 129 and 1,000
    elements.