From 32dd3cffa6b54b332948ac6a2929458defd4838f Mon Sep 17 00:00:00 2001 From: subham sarkar Date: Tue, 20 Jul 2021 02:46:32 +0530 Subject: [PATCH] go/token: match the behavior of index selection with sort.Search MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As searchInts is a manually inlined version of: sort.Search(len(a), func(i int) bool { return a[i] > x }) - 1 Hence, h's calculation should match. It'd also bump the performance: name old time/op new time/op delta SearchInts-8 15.5ns ± 2% 13.7ns ± 4% -11.87% (p=0.008 n=5+5) Refer: https://github.com/golang/go/commit/fd37b8ccf2262bb3f0a608f7545f78a72e8d661f --- src/go/token/position.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/go/token/position.go b/src/go/token/position.go index 0d7982c6705..ce4af039237 100644 --- a/src/go/token/position.go +++ b/src/go/token/position.go @@ -540,7 +540,7 @@ func searchInts(a []int, x int) int { // TODO(gri): Remove this when compilers have caught up. i, j := 0, len(a) for i < j { - h := i + (j-i)>>1 // avoid overflow when computing h + h := int(uint(i+j) >> 1) // avoid overflow when computing h // i ≤ h < j if a[h] <= x { i = h + 1