Summary
Eliminate the runtime array bounds guard for dynamic indices that are provably in bounds, recovering performance/size while keeping safety.
Background
#164 emits a runtime guard (index >= length -> unreachable) before every dynamic-index array access in Compile-mode builds (Debug and Release). Many of those indices are statically known to be in range, so the guard is dead weight there.
Proposal
Add a lightweight range/dominator analysis that proves 0 <= i < length for a dynamic access and lets codegen drop the guard for that specific access. Target the common, decidable patterns:
- Loop-bounded indices:
loop (i < len) { arr[i]; ... } — the loop condition bounds i in the body.
- Guard-dominated indices: an access dominated by
if i < N { ... arr[i] ... } with no reassignment of i between guard and use.
- Constant-propagated indices that resolve to an in-range literal (already covered statically by A037 for literals).
Where the analysis cannot prove in-bounds, the runtime guard stays (safe default). False negatives (keep the guard) are acceptable; false positives (drop a needed guard) are not.
Relationship
Future work.
Summary
Eliminate the runtime array bounds guard for dynamic indices that are provably in bounds, recovering performance/size while keeping safety.
Background
#164 emits a runtime guard (
index >= length -> unreachable) before every dynamic-index array access in Compile-mode builds (Debug and Release). Many of those indices are statically known to be in range, so the guard is dead weight there.Proposal
Add a lightweight range/dominator analysis that proves
0 <= i < lengthfor a dynamic access and lets codegen drop the guard for that specific access. Target the common, decidable patterns:loop (i < len) { arr[i]; ... }— the loop condition boundsiin the body.if i < N { ... arr[i] ... }with no reassignment ofibetween guard and use.Where the analysis cannot prove in-bounds, the runtime guard stays (safe default). False negatives (keep the guard) are acceptable; false positives (drop a needed guard) are not.
Relationship
if i < Nguard becomes exactly the evidence this analysis would consume to elide the trap.Future work.