Skip to content

Span indexing has no bounds-check elision, costing 9-29% on numerical kernels #615

Closed

Description

@godofecht

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.

Activity

  1. godofecht commented on Aug 21, 2026

    @godofecht
    ContributorAuthor

    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:

    beams idiomatic raw ptr<T> cost this issue said
    31 0.91 ms 0.67 ms 1.35x 1.29x
    61 1.38 ms 1.14 ms 1.21x 1.18x
    91 3.16 ms 3.11 ms 1.02x 1.17x
    163 8.13 ms 6.69 ms 1.21x 1.09x
    579 127.03 ms 84.57 ms 1.50x 1.01x

    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:

    579 beams, fwd+bwd ms
    Flow, raw ptr<T> 84.57
    Mojo, raw pointers 89.83
    Flow, idiomatic spans 127.03
    PyTorch 137.34

    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,
    fprintf and fault handler included, against the same loop without it:

    one pass over 335,241 complex64
    plain, real scalar 0.085 ms
    plus the bounds-check ternary 0.089 ms

    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 .len through a pointer that could alias the
    store 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.md in the diffFlow repo.

  2. godofecht commented on Aug 21, 2026

    @godofecht
    ContributorAuthor

    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:

    beams idiomatic raw ptr<T> tax, interleaved tax, my last comment tax, this issue
    31 0.902 ms 0.671 ms 1.34x 1.35x 1.29x
    61 1.374 ms 1.141 ms 1.20x 1.21x 1.18x
    91 2.839 ms 2.396 ms 1.18x 1.02x 1.17x
    163 7.673 ms 6.737 ms 1.14x 1.21x 1.09x
    579 89.807 ms 86.030 ms 1.04x 1.50x 1.01x

    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, fprintf and fault handler included, against the same loop without it: 0.085 ms
    versus 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.py in the diffFlow repo, added because of this.

  3. added
    1.0-stabilisation
    P1-high
    on Aug 21, 2026
  4. godofecht commented on Oct 1, 2026

    @godofecht
    ContributorAuthor

    Reference implementation against the retired Python compiler: tag archive/fix/615-bounds-elision (b91fd37). Useful as a design starting point for the Flow implementation.

  5. godofecht commented on Oct 2, 2026

    @godofecht
    ContributorAuthor

    Implemented in PR #1181. The C backend now elides span bounds checks for reads of the form xs[i] inside for i in 0 to xs.len; other span indexing keeps runtime checks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    1.0-stabilisationP1-highcompilerenhancementmedium

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions