Skip to content

Commit

Permalink
util: make FastIntMap return -1 when not found always
Browse files Browse the repository at this point in the history
Before this change, it would only return -1 if it had not outgrown
the small map. This lead to the oddities uncovered in #72718.

Release note: None
  • Loading branch information
ajwerner committed Nov 15, 2021
1 parent cf963af commit a242793
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pkg/util/fast_int_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (m *FastIntMap) Unset(key int) {
delete(m.large, key)
}

// Get returns the current value mapped to key, or ok=false if the
// Get returns the current value mapped to key, or (-1, false) if the
// key is unmapped.
func (m FastIntMap) Get(key int) (value int, ok bool) {
if m.large == nil {
Expand All @@ -78,8 +78,10 @@ func (m FastIntMap) Get(key int) (value int, ok bool) {
val := m.getSmallVal(uint32(key))
return int(val), (val != -1)
}
value, ok = m.large[key]
return value, ok
if value, ok = m.large[key]; ok {
return value, true
}
return -1, false
}

// GetDefault returns the current value mapped to key, or 0 if the key is
Expand Down Expand Up @@ -144,7 +146,7 @@ func (m FastIntMap) MaxKey() (_ int, ok bool) {
}

// MaxValue returns the maximum value that is in the map. If the map
// is empty, returns ok=false.
// is empty, returns (0, false).
func (m FastIntMap) MaxValue() (_ int, ok bool) {
if m.large == nil {
// In the small case, all values are positive.
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/fast_int_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"

"github.com/cockroachdb/cockroach/pkg/util/randutil"
"github.com/stretchr/testify/require"
)

func TestFastIntMap(t *testing.T) {
Expand Down Expand Up @@ -44,6 +45,9 @@ func TestFastIntMap(t *testing.T) {
k, v, ok, expV, expOk,
)
}
if !ok {
require.Equal(t, -1, v)
}
}

if e := fm.Empty(); e != (len(m) == 0) {
Expand Down

0 comments on commit a242793

Please sign in to comment.