1
0
mirror of https://github.com/golang/go synced 2024-09-29 14:34:30 -06:00

go/token: match the behavior of index selection with sort.Search

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: fd37b8ccf2
This commit is contained in:
subham sarkar 2021-07-20 02:46:32 +05:30
parent 404127c30f
commit 32dd3cffa6

View File

@ -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