ContainsCursor: name each arrayHas case and document the invariant by example#46
Merged
Merged
Conversation
… example Review follow-up on #44: the combined gap guard (i == 0 || c[i-1] < y) packed two distinct shapes into one condition, and nothing explained why exactly one predecessor compare suffices. Split the switch into single-purpose cases (hit / forward / before-first / in-gap / behind-gap) and add a worked example to the doc showing where each probe shape lands, plus why the single-gap check cannot be extended to earlier gaps (locating which earlier gap holds y is the binary search itself) and why explicit y < c[0] / y > c[N-1] boundary cases are not needed (both are answered from the cursor's neighbours without re-reading the array edges). No behavioral change; benchmarks unchanged (smallgap ~5.3 ns/op).
There was a problem hiding this comment.
Orca Security Scan Summary
| Status | Check | Issues by priority | |
|---|---|---|---|
| Infrastructure as Code | View in Orca | ||
| SAST | View in Orca | ||
| Secrets | View in Orca | ||
| Vulnerabilities | View in Orca |
aliszka
approved these changes
Jul 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to the #44 review discussion on
arrayHasreadability.What
No behavioral change. The switch now has one case per probe shape, and the doc carries a worked example:
Why not the boundary-case formulation
The suggested
y < c[0]/y > c[N-1]/y == c[i]/y > c[i]/findshape was considered and deliberately not used:y < c[0]andy > c[N-1]are already answered by the cursor cases without touching the array edges:i == 0withc[i] > yis "y precedes everything", andi == arrNwithc[i-1] < yis "y beyond the last value". Readingc[N-1]explicitly would add a potentially cold cache-line access to every probe (the whole point of the cursor is staying on the warm neighbourhood).findis what the original code did — restoring it would take smallgap from ~5.3 back to ~15+ ns/op.c[i-2] < y < c[i-1], …): the single gap below the cursor is special because the invariant brackets it with one compare that's already in cache. Locating which earlier gap holds y is precisely the binary search — those cases are, collectively, thedefault: findbranch.Verification
Equivalence + edge tests +
-race+ full suite green; benchmarks unchanged (smallgap 5.28 ns/op, biggap 21.8, random parity).