Skip to content

Commit

Permalink
internal/{rangedel,rangekey}: take Span by value
Browse files Browse the repository at this point in the history
Pass a Span rather than a pointer to a Span to rangedel.Encode and
rangekey.Encode. This avoids an unnecessary allocation in some instances.
  • Loading branch information
jbowens committed Jul 31, 2024
1 parent 0ef38b3 commit 6e60d6b
Show file tree
Hide file tree
Showing 20 changed files with 50 additions and 55 deletions.
6 changes: 3 additions & 3 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ func runBuildRemoteCmd(td *datadriven.TestData, d *DB, storage remote.Storage) e
if rdi := b.newRangeDelIter(nil, math.MaxUint64); rdi != nil {
s, err := rdi.First()
for ; s != nil && err == nil; s, err = rdi.Next() {
err = rangedel.Encode(s, func(k base.InternalKey, v []byte) error {
err = rangedel.Encode(*s, func(k base.InternalKey, v []byte) error {
k.SetSeqNum(0)
return w.Raw().Add(k, v)
})
Expand Down Expand Up @@ -691,7 +691,7 @@ func runBuildCmd(td *datadriven.TestData, d *DB, fs vfs.FS) error {
if rdi := b.newRangeDelIter(nil, math.MaxUint64); rdi != nil {
s, err := rdi.First()
for ; s != nil && err == nil; s, err = rdi.Next() {
err = rangedel.Encode(s, func(k base.InternalKey, v []byte) error {
err = rangedel.Encode(*s, func(k base.InternalKey, v []byte) error {
k.SetSeqNum(0)
return w.Raw().Add(k, v)
})
Expand Down Expand Up @@ -1086,7 +1086,7 @@ func runDBDefineCmdReuseFS(td *datadriven.TestData, opts *Options) (*DB, error)
}
if data[:i] == "rangekey" {
span := keyspan.ParseSpan(data[i:])
err := rangekey.Encode(&span, func(k base.InternalKey, v []byte) error {
err := rangekey.Encode(span, func(k base.InternalKey, v []byte) error {
return mem.set(k, v)
})
if err != nil {
Expand Down
27 changes: 11 additions & 16 deletions ingest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ func TestIngestLoad(t *testing.T) {
for _, data := range strings.Split(td.Input, "\n") {
if strings.HasPrefix(data, "rangekey: ") {
data = strings.TrimPrefix(data, "rangekey: ")
s := keyspan.ParseSpan(data)
err := w.EncodeSpan(&s)
err := w.EncodeSpan(keyspan.ParseSpan(data))
if err != nil {
return err.Error()
}
Expand Down Expand Up @@ -1104,21 +1103,20 @@ func testIngestSharedImpl(
return nil
},
func(start, end []byte, seqNum base.SeqNum) error {
require.NoError(t, w.EncodeSpan(&keyspan.Span{
require.NoError(t, w.EncodeSpan(keyspan.Span{
Start: start,
End: end,
Keys: []keyspan.Key{{Trailer: base.MakeTrailer(0, base.InternalKeyKindRangeDelete)}},
}))
return nil
},
func(start, end []byte, keys []keyspan.Key) error {
s := keyspan.Span{
require.NoError(t, w.EncodeSpan(keyspan.Span{
Start: start,
End: end,
Keys: keys,
KeysOrder: 0,
}
require.NoError(t, w.EncodeSpan(&s))
}))
return nil
},
func(sst *SharedSSTMeta) error {
Expand Down Expand Up @@ -1604,21 +1602,20 @@ func TestConcurrentExcise(t *testing.T) {
return nil
},
func(start, end []byte, seqNum base.SeqNum) error {
require.NoError(t, w.EncodeSpan(&keyspan.Span{
require.NoError(t, w.EncodeSpan(keyspan.Span{
Start: start,
End: end,
Keys: []keyspan.Key{{Trailer: base.MakeTrailer(0, base.InternalKeyKindRangeDelete)}},
}))
return nil
},
func(start, end []byte, keys []keyspan.Key) error {
s := keyspan.Span{
require.NoError(t, w.EncodeSpan(keyspan.Span{
Start: start,
End: end,
Keys: keys,
KeysOrder: 0,
}
require.NoError(t, w.EncodeSpan(&s))
}))
return nil
},
func(sst *SharedSSTMeta) error {
Expand Down Expand Up @@ -2041,21 +2038,20 @@ func TestIngestExternal(t *testing.T) {
return nil
},
func(start, end []byte, seqNum base.SeqNum) error {
require.NoError(t, w.EncodeSpan(&keyspan.Span{
require.NoError(t, w.EncodeSpan(keyspan.Span{
Start: start,
End: end,
Keys: []keyspan.Key{{Trailer: base.MakeTrailer(0, base.InternalKeyKindRangeDelete)}},
}))
return nil
},
func(start, end []byte, keys []keyspan.Key) error {
s := keyspan.Span{
require.NoError(t, w.EncodeSpan(keyspan.Span{
Start: start,
End: end,
Keys: keys,
KeysOrder: 0,
}
require.NoError(t, w.EncodeSpan(&s))
}))
return nil
},
nil,
Expand Down Expand Up @@ -3236,8 +3232,7 @@ func TestIngest_UpdateSequenceNumber(t *testing.T) {
for _, data := range strings.Split(input, "\n") {
if strings.HasPrefix(data, "rangekey: ") {
data = strings.TrimPrefix(data, "rangekey: ")
s := keyspan.ParseSpan(data)
err := w.EncodeSpan(&s)
err := w.EncodeSpan(keyspan.ParseSpan(data))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/compact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (r *Runner) writeKeysToTable(tw *sstable.RawWriter) (splitKey []byte, _ err
case base.InternalKeyKindRangeDelete:
// The previous span (if any) must end at or before this key, since the
// spans we receive are non-overlapping.
if err := tw.EncodeSpan(&r.lastRangeDelSpan); r.err != nil {
if err := tw.EncodeSpan(r.lastRangeDelSpan); r.err != nil {
return nil, err
}
r.lastRangeDelSpan.CopyFrom(r.iter.Span())
Expand All @@ -194,7 +194,7 @@ func (r *Runner) writeKeysToTable(tw *sstable.RawWriter) (splitKey []byte, _ err
case base.InternalKeyKindRangeKeySet, base.InternalKeyKindRangeKeyUnset, base.InternalKeyKindRangeKeyDelete:
// The previous span (if any) must end at or before this key, since the
// spans we receive are non-overlapping.
if err := tw.EncodeSpan(&r.lastRangeKeySpan); err != nil {
if err := tw.EncodeSpan(r.lastRangeKeySpan); err != nil {
return nil, err
}
r.lastRangeKeySpan.CopyFrom(r.iter.Span())
Expand Down
7 changes: 3 additions & 4 deletions internal/compact/spans.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func SplitAndEncodeSpan(
}

if upToKey == nil || cmp(span.End, upToKey) <= 0 {
if err := tw.EncodeSpan(span); err != nil {
if err := tw.EncodeSpan(*span); err != nil {
return err
}
span.Reset()
Expand All @@ -202,12 +202,11 @@ func SplitAndEncodeSpan(
}

// Split the span at upToKey and encode the first part.
splitSpan := keyspan.Span{
if err := tw.EncodeSpan(keyspan.Span{
Start: span.Start,
End: upToKey,
Keys: span.Keys,
}
if err := tw.EncodeSpan(&splitSpan); err != nil {
}); err != nil {
return err
}
span.Start = append(span.Start[:0], upToKey...)
Expand Down
2 changes: 1 addition & 1 deletion internal/rangedel/rangedel.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// closure with the encoded internal keys that represent the Span's state. The
// keys and values passed to emit are only valid until the closure returns. If
// emit returns an error, Encode stops and returns the error.
func Encode(s *keyspan.Span, emit func(k base.InternalKey, v []byte) error) error {
func Encode(s keyspan.Span, emit func(k base.InternalKey, v []byte) error) error {
for _, k := range s.Keys {
if k.Kind() != base.InternalKeyKindRangeDelete {
return base.CorruptionErrorf("pebble: rangedel.Encode cannot encode %s key", k.Kind())
Expand Down
6 changes: 3 additions & 3 deletions internal/rangekey/rangekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import (
// closure with the encoded internal keys that represent the Span's state. The
// keys and values passed to emit are only valid until the closure returns.
// If emit returns an error, Encode stops and returns the error.
func Encode(s *keyspan.Span, emit func(k base.InternalKey, v []byte) error) error {
func Encode(s keyspan.Span, emit func(k base.InternalKey, v []byte) error) error {
enc := Encoder{Emit: emit}
return enc.Encode(s)
}
Expand All @@ -82,7 +82,7 @@ type Encoder struct {
//
// The encoded key-value pair passed to Emit is only valid until the closure
// completes.
func (e *Encoder) Encode(s *keyspan.Span) error {
func (e *Encoder) Encode(s keyspan.Span) error {
if s.Empty() {
return nil
}
Expand Down Expand Up @@ -127,7 +127,7 @@ func (e *Encoder) Encode(s *keyspan.Span) error {

// flush constructs internal keys for accumulated key state, and emits the
// internal keys.
func (e *Encoder) flush(s *keyspan.Span, seqNum base.SeqNum, del bool) error {
func (e *Encoder) flush(s keyspan.Span, seqNum base.SeqNum, del bool) error {
if len(e.sets) > 0 {
ik := base.MakeInternalKey(s.Start, seqNum, base.InternalKeyKindRangeKeySet)
l := EncodedSetValueLen(s.End, e.sets)
Expand Down
2 changes: 1 addition & 1 deletion level_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func TestCheckLevelsCornerCases(t *testing.T) {
}
frag.Finish()
for _, v := range tombstones {
if err := rangedel.Encode(&v, w.Add); err != nil {
if err := rangedel.Encode(v, w.Add); err != nil {
return err.Error()
}
}
Expand Down
4 changes: 2 additions & 2 deletions level_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (lt *levelIterTest) runBuild(d *datadriven.TestData) string {
if err != nil {
return err.Error()
}
if err := w.EncodeSpan(&span); err != nil {
if err := w.EncodeSpan(span); err != nil {
return err.Error()
}
default:
Expand All @@ -257,7 +257,7 @@ func (lt *levelIterTest) runBuild(d *datadriven.TestData) string {
}
f.Finish()
for _, v := range tombstones {
if err := rangedel.Encode(&v, w.Add); err != nil {
if err := rangedel.Encode(v, w.Add); err != nil {
return err.Error()
}
}
Expand Down
2 changes: 1 addition & 1 deletion merging_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func TestMergingIterCornerCases(t *testing.T) {
}
frag.Finish()
for _, v := range tombstones {
if err := rangedel.Encode(&v, w.Add); err != nil {
if err := rangedel.Encode(v, w.Add); err != nil {
return err.Error()
}
}
Expand Down
2 changes: 1 addition & 1 deletion metamorphic/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func writeSSTForIngestion(
collapsed.Keys[i].Trailer = base.MakeTrailer(0, collapsed.Keys[i].Kind())
}
keyspan.SortKeysByTrailer(&collapsed.Keys)
if err := w.Raw().EncodeSpan(&collapsed); err != nil {
if err := w.Raw().EncodeSpan(collapsed); err != nil {
return nil, err
}
}
Expand Down
12 changes: 5 additions & 7 deletions metamorphic/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -1934,12 +1934,11 @@ func (r *replicateOp) runSharedReplicate(
return w.DeleteRange(start, end)
},
func(start, end []byte, keys []keyspan.Key) error {
s := keyspan.Span{
return w.Raw().EncodeSpan(keyspan.Span{
Start: start,
End: end,
Keys: keys,
}
return w.Raw().EncodeSpan(&s)
})
},
func(sst *pebble.SharedSSTMeta) error {
sharedSSTs = append(sharedSSTs, *sst)
Expand Down Expand Up @@ -1998,12 +1997,11 @@ func (r *replicateOp) runExternalReplicate(
return w.DeleteRange(start, end)
},
func(start, end []byte, keys []keyspan.Key) error {
s := keyspan.Span{
return w.Raw().EncodeSpan(keyspan.Span{
Start: start,
End: end,
Keys: keys,
}
return w.Raw().EncodeSpan(&s)
})
},
nil,
func(sst *pebble.ExternalFile) error {
Expand Down Expand Up @@ -2107,7 +2105,7 @@ func (r *replicateOp) run(t *Test, h historyRecorder) {
rangeKeys := iter.RangeKeys()
rkStart, rkEnd := iter.RangeBounds()

span := &keyspan.Span{Start: rkStart, End: rkEnd, Keys: make([]keyspan.Key, len(rangeKeys))}
span := keyspan.Span{Start: rkStart, End: rkEnd, Keys: make([]keyspan.Key, len(rangeKeys))}
for i := range rangeKeys {
span.Keys[i] = keyspan.Key{
Trailer: base.MakeTrailer(0, base.InternalKeyKindRangeKeySet),
Expand Down
4 changes: 2 additions & 2 deletions replay/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ func loadFlushedSSTableKeys(
defer iter.Close()
s, err := iter.First()
for ; s != nil; s, err = iter.Next() {
if err := rangedel.Encode(s, func(k base.InternalKey, v []byte) error {
if err := rangedel.Encode(*s, func(k base.InternalKey, v []byte) error {
var key flushedKey
key.Trailer = k.Trailer
bufs.alloc, key.UserKey = bufs.alloc.Copy(k.UserKey)
Expand All @@ -1049,7 +1049,7 @@ func loadFlushedSSTableKeys(
defer iter.Close()
s, err := iter.First()
for ; s != nil; s, err = iter.Next() {
if err := rangekey.Encode(s, func(k base.InternalKey, v []byte) error {
if err := rangekey.Encode(*s, func(k base.InternalKey, v []byte) error {
var key flushedKey
key.Trailer = k.Trailer
bufs.alloc, key.UserKey = bufs.alloc.Copy(k.UserKey)
Expand Down
7 changes: 5 additions & 2 deletions scan_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,11 @@ func TestScanInternal(t *testing.T) {
keys[i].Trailer = base.MakeTrailer(0, keys[i].Kind())
}
keyspan.SortKeysByTrailer(&keys)
newSpan := &keyspan.Span{Start: span.Start, End: span.End, Keys: keys}
require.NoError(t, w.Raw().EncodeSpan(newSpan))
require.NoError(t, w.Raw().EncodeSpan(keyspan.Span{
Start: span.Start,
End: span.End,
Keys: keys,
}))
}
require.NoError(t, err)
}
Expand Down
2 changes: 1 addition & 1 deletion sstable/block_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ func (p *keyCountCollector) AddPointKey(k InternalKey, _ []byte) error {
}

func (p *keyCountCollector) AddRangeKeys(span Span) error {
return rangekey.Encode(&span, func(k base.InternalKey, v []byte) error {
return rangekey.Encode(span, func(k base.InternalKey, v []byte) error {
p.table++
return nil
})
Expand Down
6 changes: 3 additions & 3 deletions sstable/raw_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ func (w *RawWriter) encodeFragmentedRangeKeySpan(span keyspan.Span) {
w.rangeKeySpan = span
w.rangeKeySpan.Keys = w.rangeKeysBySuffix.Keys
if w.err == nil {
w.err = w.EncodeSpan(&w.rangeKeySpan)
w.err = w.EncodeSpan(w.rangeKeySpan)
}
}

Expand Down Expand Up @@ -1660,15 +1660,15 @@ func (w *RawWriter) UnsafeLastPointUserKey() []byte {
//
// This is a low-level API that bypasses the fragmenter. The spans passed to
// this function must be fragmented and ordered.
func (w *RawWriter) EncodeSpan(span *keyspan.Span) error {
func (w *RawWriter) EncodeSpan(span keyspan.Span) error {
if span.Empty() {
return nil
}
if span.Keys[0].Kind() == base.InternalKeyKindRangeDelete {
return rangedel.Encode(span, w.Add)
}
for i := range w.blockPropCollectors {
if err := w.blockPropCollectors[i].AddRangeKeys(*span); err != nil {
if err := w.blockPropCollectors[i].AddRangeKeys(span); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions sstable/rowblk/rowblk_fragment_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func TestBlockFragmentIterator(t *testing.T) {
}
for _, s := range spans {
if s.Keys[0].Kind() == base.InternalKeyKindRangeDelete {
rangedel.Encode(&s, emitFn)
rangedel.Encode(s, emitFn)
} else {
rangekey.Encode(&s, emitFn)
rangekey.Encode(s, emitFn)
}
}
blockData := w.Finish()
Expand Down
2 changes: 1 addition & 1 deletion sstable/suffix_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func rewriteRangeKeyBlockToWriter(r *Reader, w *RawWriter, from, to []byte) erro
s.Keys[i].Suffix = to
}

if err := w.EncodeSpan(s); err != nil {
if err := w.EncodeSpan(*s); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion table_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func TestTableRangeDeletionIter(t *testing.T) {
for _, line := range strings.Split(td.Input, "\n") {
s := keyspan.ParseSpan(line)
// Range dels can be written sequentially. Range keys must be collected.
rKeySpan := &keyspan.Span{Start: s.Start, End: s.End}
rKeySpan := keyspan.Span{Start: s.Start, End: s.End}
for _, k := range s.Keys {
if rangekey.IsRangeKey(k.Kind()) {
rKeySpan.Keys = append(rKeySpan.Keys, k)
Expand Down
2 changes: 1 addition & 1 deletion tool/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ func (f *findT) searchTables(stdout io.Writer, searchKey []byte, refs []findRef)
} else {
// Use rangedel.Encode to add a reference for each key
// within the span.
err := rangedel.Encode(rangeDel, func(k base.InternalKey, v []byte) error {
err := rangedel.Encode(*rangeDel, func(k base.InternalKey, v []byte) error {
refs = append(refs, findRef{
key: k.Clone(),
value: slices.Clone(v),
Expand Down
2 changes: 1 addition & 1 deletion tool/sstable.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func (s *sstableT) runScan(cmd *cobra.Command, args []string) {
bytes.HasPrefix(rangeDel.Start, s.filter)) &&
r.Compare(s.filter, rangeDel.End) < 0) {
fmt.Fprint(stdout, prefix)
if err := rangedel.Encode(rangeDel, func(k base.InternalKey, v []byte) error {
if err := rangedel.Encode(*rangeDel, func(k base.InternalKey, v []byte) error {
formatKeyValue(stdout, s.fmtKey, s.fmtValue, &k, v)
return nil
}); err != nil {
Expand Down

0 comments on commit 6e60d6b

Please sign in to comment.