Skip to content

Commit

Permalink
rowenc: calculate key prefix length without allocating
Browse files Browse the repository at this point in the history
We now calculate the key prefix length directly instead of creating a
throw-away key.

Release note: None
  • Loading branch information
RaduBerinde committed Feb 18, 2022
1 parent 2690fff commit 2be05a8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pkg/sql/rowenc/index_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/errors"
)

Expand Down Expand Up @@ -46,8 +47,9 @@ func InitIndexFetchSpec(

maxKeysPerRow := table.IndexKeysPerRow(index)
s.MaxKeysPerRow = uint32(maxKeysPerRow)
// TODO(radu): calculate the length without actually generating a throw-away key.
s.KeyPrefixLength = uint32(len(MakeIndexKeyPrefix(codec, table.GetID(), index.GetID())))
s.KeyPrefixLength = uint32(len(codec.TenantPrefix()) +
encoding.EncodedLengthUvarintAscending(uint64(s.TableID)) +
encoding.EncodedLengthUvarintAscending(uint64(index.GetID())))

s.FamilyDefaultColumns = table.FamilyDefaultColumns()

Expand Down
25 changes: 25 additions & 0 deletions pkg/util/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,31 @@ func EncodeUvarintAscending(b []byte, v uint64) []byte {
}
}

// EncodedLengthUvarintAscending returns the length of the variable length
// representation, i.e. the number of bytes appended by EncodeUvarintAscending.
func EncodedLengthUvarintAscending(v uint64) int {
switch {
case v <= intSmall:
return 1
case v <= 0xff:
return 2
case v <= 0xffff:
return 3
case v <= 0xffffff:
return 4
case v <= 0xffffffff:
return 5
case v <= 0xffffffffff:
return 6
case v <= 0xffffffffffff:
return 7
case v <= 0xffffffffffffff:
return 8
default:
return 9
}
}

// EncodeUvarintDescending encodes the uint64 value so that it sorts in
// reverse order, from largest to smallest.
func EncodeUvarintDescending(b []byte, v uint64) []byte {
Expand Down
11 changes: 11 additions & 0 deletions pkg/util/encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ func TestEncodeDecodeUvarint(t *testing.T) {
testCustomEncodeUint64(testCases, EncodeUvarintAscending, t)
}

func TestEncodedLengthUvarintAscending(t *testing.T) {
for i := 0; i < 100; i++ {
v := rand.Uint64()
exp := len(EncodeUvarintAscending(nil, v))
actual := EncodedLengthUvarintAscending(v)
if actual != exp {
t.Fatalf("incorrect encoded length for %d: %d (expected %d)", v, actual, exp)
}
}
}

func TestEncodeDecodeUvarintDescending(t *testing.T) {
testBasicEncodeDecodeUint64(EncodeUvarintDescending, DecodeUvarintDescending, true, true, true, t)
testCases := []testCaseUint64{
Expand Down

0 comments on commit 2be05a8

Please sign in to comment.