Skip to content

Commit

Permalink
sstable: use sync.Pool for interval block collectors
Browse files Browse the repository at this point in the history
  • Loading branch information
RaduBerinde committed Jul 22, 2024
1 parent 232b685 commit b38f48b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
3 changes: 3 additions & 0 deletions iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ func (c *minSeqNumPropertyCollector) SupportsSuffixReplacement() bool {
return false
}

// Close is part of the BlockPropertyCollector interface.
func (c *minSeqNumPropertyCollector) Close() {}

// minSeqNumFilter is a BlockPropertyFilter that uses the
// minSeqNumPropertyCollector data to filter out entire tables.
type minSeqNumFilter struct {
Expand Down
20 changes: 19 additions & 1 deletion sstable/block_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ type BlockPropertyCollector interface {
// Finish appends the property value to buf and resets the collector to an
// empty state.
Finish(buf []byte) []byte

// Close can be used to allow the implementation to be reused in the future.
// Once Close is called, the object must no longer be used.
Close()
}

// BlockPropertyFilter is used in an Iterator to filter sstables and blocks
Expand Down Expand Up @@ -250,11 +254,19 @@ func NewBlockIntervalCollector(
if mapper == nil {
panic("mapper must be provided")
}
return &BlockIntervalCollector{
c := blockIntervalCollectorPool.Get().(*BlockIntervalCollector)
*c = BlockIntervalCollector{
name: name,
mapper: mapper,
suffixReplacer: suffixReplacer,
}
return c
}

var blockIntervalCollectorPool = sync.Pool{
New: func() interface{} {
return &BlockIntervalCollector{}
},
}

// Name is part of the BlockPropertyCollector interface.
Expand Down Expand Up @@ -328,6 +340,12 @@ func (b *BlockIntervalCollector) Finish(buf []byte) []byte {
return result
}

// Close is part of the BlockPropertyCollector interface.
func (b *BlockIntervalCollector) Close() {
*b = BlockIntervalCollector{}
blockIntervalCollectorPool.Put(b)
}

// BlockInterval represents the [Lower, Upper) interval of 64-bit values
// corresponding to a set of keys. The meaning of the values themselves is
// opaque to the BlockIntervalCollector.
Expand Down
3 changes: 3 additions & 0 deletions sstable/block_property_obsolete.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func (o *obsoleteKeyBlockPropertyCollector) Finish(buf []byte) []byte {
return res
}

// Close is part of the BlockPropertyCollector interface.
func (o *obsoleteKeyBlockPropertyCollector) Close() {}

// AddCollected is part of the BlockPropertyCollector interface.
func (o *obsoleteKeyBlockPropertyCollector) AddCollected(oldProp []byte) error {
isObsolete, err := obsoleteKeyBlockPropertyDecode(oldProp)
Expand Down
3 changes: 3 additions & 0 deletions sstable/block_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,9 @@ func (p *keyCountCollector) SupportsSuffixReplacement() bool {
return true
}

// Close is part of the BlockPropertyCollector interface.
func (p *keyCountCollector) Close() {}

type intSuffixIntervalMapper struct {
suffixLen int
}
Expand Down
10 changes: 10 additions & 0 deletions sstable/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,16 @@ func (w *Writer) Close() (err error) {
indexBlockBufPool.Put(w.indexBlock)
w.indexBlock = nil

// Close property collectors. Closing is optional, we don't need to do it in
// error paths.
for _, p := range w.blockPropCollectors {
p.dataBlock.Close()
p.indexBlock.Close()
p.rangeKeyBlock.Close()
p.table.Close()
}
w.blockPropCollectors = nil

// Make any future calls to Set or Close return an error.
w.err = errWriterClosed
return nil
Expand Down

0 comments on commit b38f48b

Please sign in to comment.