Skip to content

Commit

Permalink
sstable: revert change to binary search calculation
Browse files Browse the repository at this point in the history
Revert index calculation for binary search in rowblk_iter.go to use casting (like in old iterations).
Binary search was originally modified to factor for int32 overflow, however this is not relevant
because a cast to uint will fix the overflow error. Reverting back to old calculation for
consistency with other of binary search calculations and (presumably) less instructions.
  • Loading branch information
EdwardX29 committed Jan 13, 2025
1 parent 38fb051 commit e019acf
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions sstable/rowblk/rowblk_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ func (i *Iter) SeekGE(key []byte, flags base.SeekGEFlags) *base.InternalKV {
// Invariant: f(index-1) == false, f(upper) == true.
upper := i.numRestarts
for index < upper {
h := index + ((upper - index) >> 1) // avoid overflow when computing h
h := int32(uint(index+upper) >> 1) // avoid overflow when computing h

// index ≤ h < upper
offset := decodeRestart(i.data[i.restarts+4*offsetInBlock(h):])
Expand Down Expand Up @@ -766,7 +766,7 @@ func (i *Iter) SeekLT(key []byte, flags base.SeekLTFlags) *base.InternalKV {
// Invariant: f(index-1) == false, f(upper) == true.
upper := i.numRestarts
for index < upper {
h := index + ((upper - index) >> 1) // avoid overflow when computing h
h := int32(uint(index+upper) >> 1) // avoid overflow when computing h

// index ≤ h < upper
offset := decodeRestart(i.data[i.restarts+4*offsetInBlock(h):])
Expand Down Expand Up @@ -1293,7 +1293,7 @@ func (i *Iter) nextPrefixV3(succKey []byte) *base.InternalKV {
// Invariant: f(index-1) == false, f(upper) == true.
upper := i.numRestarts
for index < upper {
h := index + ((upper - index) >> 1) // avoid overflow when computing h
h := int32(uint(index+upper) >> 1) // avoid overflow when computing h

// index ≤ h < upper
offset := decodeRestart(i.data[i.restarts+4*offsetInBlock(h):])
Expand Down Expand Up @@ -1496,7 +1496,7 @@ start:
// Invariant: f(index-1) == false, f(upper) == true.
upper := i.numRestarts
for index < upper {
h := index + ((upper - index) >> 1) // avoid overflow when computing h
h := int32(uint(index+upper) >> 1) // avoid overflow when computing h

// index ≤ h < upper
offset := decodeRestart(i.data[i.restarts+4*offsetInBlock(h):])
Expand Down

0 comments on commit e019acf

Please sign in to comment.