cmd/compile: prove should be taught about the bounds of (i+j)/2 #54544
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
NeedsFix
The path to resolution is known, but the work has not been done.
Performance
Milestone
sort.Search, and its inlined copy in go/token.searchInts, both have code like:
Note that
a[h]
causes a bounds check, as the compiler doesn't know that0 <= h < len(a)
. We intuitively know that, becauseh
is the middle point betweeni
andj
, thusi <= h <= j
.i
andj
themselves start as0
andlen(a)
and move closer towardsh
.It would be nice if the prove pass could be smart enough to tell that
a[h]
here does not need a bounds check. I can force its hand viaif h >= 0 && h < len(a) && a[h] <= x {
, but that doesn't result in a noticeable improvement:I realize that this request is fairly specific to binary searches, but I reckon that's a common enough occurrence that we should teach prove about it.
cc @egonelbre @golang/compiler
The text was updated successfully, but these errors were encountered: