Repository navigation
Description
Flow 0.12.0, macOS arm64 (Apple M-series), Python host, Accelerate BLAS.
Every span<T> element access emits a runtime bounds check, with no profile, attribute or slice form that removes it where the extent is statically known.
acc = (acc + ((int64_t)(i) < (values).len
? (values).data[i]
: (fprintf(stderr, "span index %lld out of bounds (len %lld)\n", ...),
flow_fault_handler("span index out of bounds"), (values).data[0])));This is the right default. The problem is that it is the only option, which pushes numerical code back to ptr<T> and gives up the length checking entirely.
Measurement
Two builds of the same physics kernel: a differentiable Bloch-wave forward model and its adjoint, structure factors through a matrix exponential to a weighted residual. Identical output to the last bit, same compiler host, same BLAS. One uses span throughout, the other raw ptr<T>.
Forward and backward, milliseconds, minimum of five trials:
| beams | idiomatic | raw ptr<T> |
cost |
|---|---|---|---|
| 31 | 0.93 | 0.72 | 1.29x |
| 61 | 1.40 | 1.18 | 1.18x |
| 91 | 2.81 | 2.40 | 1.17x |
| 163 | 8.13 | 7.44 | 1.09x |
| 579 | 105.84 | 104.34 | 1.01x |
The tax lives in the scalar loops, so it shrinks as BLAS comes to dominate. At the sizes this workload actually runs, 31 to 163, it is 9% to 29%.
Suggested fix
The common case is a loop whose bound is the span's own length:
for i in 0 to values.len as i32 {
acc = acc + values[i]
}
Here the index is provably in range and the check is dead. Eliding it for that pattern alone would recover most of the cost without weakening the default anywhere else.
Beyond that, span<T, N> already carries a compile-time extent, which is enough to discharge the check against a literal or a loop bound derived from N.
If a general opt-out is wanted, a safety profile knob would fit the existing structure in docs/language/safety-profiles.md, which already distinguishes what the default profile guards. But the loop-bound case is the one that matters and it needs no new surface.
Why this is worth doing
The alternative is what I ended up shipping: two builds of the same code, one idiomatic and one on pointers, kept in sync so the difference can be measured. That is not a pattern to encourage.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
godofecht commentedon Aug 21, 2026
Re-measured on a quiet machine, and the shape of the tax is different from what this issue
reports. The original table was taken during a contended sweep.
Forward and backward, minimum of five trials, load average 10:
ptr<T>The claim that the tax "shrinks to nothing as BLAS comes to dominate" does not hold. At 579
beams it is the largest of the five, 1.50x, where the contended measurement put it at 1.01x.
BLAS dominating the runtime is exactly when the earlier measurement was least able to see
it, because contention inflated both builds toward the same BLAS-bound floor.
There is now an independent check on that number. The same kernel exists in Mojo, as a
raw-pointer build, and it lands next to Flow's own raw-pointer build rather than next to the
idiomatic one:
ptr<T>Two unsafe implementations in two unrelated languages agree to 6%, and the safe one needs
1.5x the time for the same arithmetic and bit-identical output. That is a stronger case for
this issue than the original table made.
One correction in the other direction, though, on the cause. The issue attributes the tax to
the bounds check, and I cannot reproduce that. Compiling the exact generated ternary,
fprintfand fault handler included, against the same loop without it:1.04x. So on the hot elementwise loop the check itself is not where the 1.5x goes, and
eliding it may not recover what this issue expects. Something else about spans is expensive.
Passing
{ptr,len}by value and reloading.lenthrough a pointer that could alias thestore target is the first thing I would look at, since that would defeat alias analysis in a
way the check alone does not.
Numbers and method:
reports/headroom.mdin the diffFlow repo.godofecht commentedon Aug 21, 2026
Withdrawing my previous comment. The 1.50x at 579 beams was a measurement artifact and the
original table in this issue is right.
The harness ran each build's whole trial set before starting the next, so on a shared
machine each build is measured in its own window and whatever the machine was doing during
that window lands on that build. Re-running round-robin instead, one repetition of each
build per round, minimum across eight rounds:
ptr<T>The interleaved column tracks the issue's original numbers, not mine. So the tax does shrink
as BLAS comes to dominate, exactly as written here, and my claim that it grows to 1.50x at
the largest size was one busy stretch landing on one binary.
The Mojo cross-check I offered goes with it. Interleaved, the Mojo port and the idiomatic
Flow build tie at 579 (90.05 ms against 89.81 ms) rather than Mojo winning by 1.41x, so it
says nothing about the tax either way.
Two things from that comment do stand, both measured in isolation rather than by differencing
whole-program runs:
The bounds check itself is 1.04x on the hot elementwise loop. Compiling the exact generated
ternary,
fprintfand fault handler included, against the same loop without it: 0.085 msversus 0.089 ms over 335,241 complex64 elements. So the 1.34x at 31 beams is real and the
check is probably not where it goes, which is still worth knowing before anyone optimises
the check specifically.
And the tax is largest at the small end, 1.34x at 31 beams, which is where this workload
actually runs. That was the point of the issue and it survives.
Harness:
bench/interleaved.pyin the diffFlow repo, added because of this.godofecht commentedon Oct 1, 2026
Reference implementation against the retired Python compiler: tag archive/fix/615-bounds-elision (b91fd37). Useful as a design starting point for the Flow implementation.
godofecht commentedon Oct 2, 2026
Implemented in PR #1181. The C backend now elides span bounds checks for reads of the form
xs[i]insidefor i in 0 to xs.len; other span indexing keeps runtime checks.perf: first slice of numerical codegen taxes (#729) (#1346)
fix: ensure lower bound is non-negative for array bounds elision (#615)
fix: ensure lower bound is non-negative for array bounds elision (#615)
fix: ensure lower bound is non-negative for array bounds elision (#615)
fix: ensure lower bound is non-negative for array bounds elision (#615)