Repository navigation
Description
What I see
On the C backend, Array.to_list of an Array<F32> costs about 190-208 user-mode instructions and 11-15 ns per cell on one thread, 13-15x a C loop that builds the same list. A plain Bend loop of Array.get that reads from the last index down and conses each number builds the same list for about 35 instructions and 2.6-4 ns a cell.
The repro (array_to_list_walk.bend, below) fills 64 fresh arrays of 2^20 F32, converts each one and sums the list. MODE 0n uses Array.to_list, MODE 1n the Array.get loop. Bend 2.0.39, clang 23.1.3, Core Ultra 7 155H, one thread pinned with taskset -c 3, +RTS --threads 1 -RTS, 5 alternated runs (instructions from perf stat -e instructions:u, wall time from /usr/bin/time):
| instructions | per cell (fill + convert + sum) | time | |
|---|---|---|---|
Array.to_list |
12,826-12,834 M | 191 | 0.86-0.87 s |
loop of Array.get, last index down |
2,318-2,326 M | 34.6 | 0.23 s |
Both print the same sum (23.789732). Per cell, Array.to_list alone is about 157 instructions and 9.6 ns more than the loop.
Across sizes (my bench, the conversion of a fresh Array.new of 2^d F32 plus the sum of its list, per cell, against a C loop that writes the same list into an arena and sums it):
| cells | Array.to_list |
C |
|---|---|---|
| 2^12 | 192 instr, 11.3 ns | 14 instr, 1.4 ns |
| 2^16 | 195 instr, 11.7 ns | 14 instr, 1.4 ns |
| 2^20 | 199 instr, 13.1 ns | 14 instr, 1.8 ns |
| 2^24 | 208 instr, 15.2 ns | 14 instr, 5.5-6.2 ns |
Why, as far as I can tell
Array.to_list.go matches the array: ANode{xs, ys} → to_list.go(xs, to_list.go(ys, acc)). In the C lane matching ANode is blk_half twice, and each call allocates a block for its half and copies the cells into it (the high call frees the source). So every level of the tree allocates and copies, down to one block per leaf: 2n - 1 allocations and frees and about n log2 n copied cells for an n-cell array. The copies are cheap per cell (that is why the count grows only from 192 to 208 between 2^12 and 2^24); the block per node is most of the cost.
The JS lane had the same shape and got a flat walk in #1661: reading the flat array backward and prepending list nodes. The same thing would work in C: Array.to_list on a flat block is n reads and n conses with no new blocks. A Bend-level version of it is the MODE 1n loop above, which is what I use in my own code now; it needs the array's size, which Base gets from Array.size.
Repro
# MODE 0n: Array.to_list; MODE 1n: the same list read by index, from the last cell down, so it needs no reverse.
# Each run fills 64 fresh arrays of 2^20 F32 (Array.new), converts each one and sums the list.
import Base
def lsum(xs: List<F32>, acc: F32) -> F32:
match xs:
case Nil{}:
acc
case Con{+h, t}:
lsum(t, (acc + h : F32))
# n + 1 cells ending at index i, read from i down onto acc
def by_get(n: Nat, +i: U32, acc: List<F32>, r: Array<F32> & F32) -> List<F32>:
match n r:
case 0n Tuple{a, +v}:
v <> acc
case 1n+p Tuple{a, +v}:
by_get(p, (i - 1 : U32), v <> acc, Array.get(F32, a, (i - 1 : U32)))
def conv(m: Nat, a: Array<F32>) -> List<F32>:
match m:
case 0n:
Array.to_list(~F32, a)
case 1n+q:
by_get(1048575n, 1048575, Nil{}, Array.get(F32, a, 1048575))
def go(k: Nat, +acc: F32) -> F32:
match k:
case 0n:
acc
case 1n+p:
go(p, (lsum(conv(MODE, Array.new(F32, 20n, acc)), 0.0) * 0.000001 : F32))
def main() -> F32:
go(64n, 1.0)sed 's/MODE/0n/' array_to_list_walk.bend > m0.bend && bend m0.bend -o m0
sed 's/MODE/1n/' array_to_list_walk.bend > m1.bend && bend m1.bend -o m1
perf stat -e instructions:u taskset -c 3 ./m0 +RTS --threads 1 -RTS
perf stat -e instructions:u taskset -c 3 ./m1 +RTS --threads 1 -RTSI haven't checked Array<T> of boxed values or the GPU lane. If a flat walk in C is welcome, I can try it as a PR with the same measurements.
Activity
perf(runtime): matching ANode views the block instead of copying it
perf(runtime): matching ANode views the block instead of copying it