Skip to content

Commit b38f48b

Browse files
committed
sstable: use sync.Pool for interval block collectors
1 parent 232b685 commit b38f48b

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

Diff for: iterator_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ func (c *minSeqNumPropertyCollector) SupportsSuffixReplacement() bool {
280280
return false
281281
}
282282

283+
// Close is part of the BlockPropertyCollector interface.
284+
func (c *minSeqNumPropertyCollector) Close() {}
285+
283286
// minSeqNumFilter is a BlockPropertyFilter that uses the
284287
// minSeqNumPropertyCollector data to filter out entire tables.
285288
type minSeqNumFilter struct {

Diff for: sstable/block_property.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ type BlockPropertyCollector interface {
131131
// Finish appends the property value to buf and resets the collector to an
132132
// empty state.
133133
Finish(buf []byte) []byte
134+
135+
// Close can be used to allow the implementation to be reused in the future.
136+
// Once Close is called, the object must no longer be used.
137+
Close()
134138
}
135139

136140
// BlockPropertyFilter is used in an Iterator to filter sstables and blocks
@@ -250,11 +254,19 @@ func NewBlockIntervalCollector(
250254
if mapper == nil {
251255
panic("mapper must be provided")
252256
}
253-
return &BlockIntervalCollector{
257+
c := blockIntervalCollectorPool.Get().(*BlockIntervalCollector)
258+
*c = BlockIntervalCollector{
254259
name: name,
255260
mapper: mapper,
256261
suffixReplacer: suffixReplacer,
257262
}
263+
return c
264+
}
265+
266+
var blockIntervalCollectorPool = sync.Pool{
267+
New: func() interface{} {
268+
return &BlockIntervalCollector{}
269+
},
258270
}
259271

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

343+
// Close is part of the BlockPropertyCollector interface.
344+
func (b *BlockIntervalCollector) Close() {
345+
*b = BlockIntervalCollector{}
346+
blockIntervalCollectorPool.Put(b)
347+
}
348+
331349
// BlockInterval represents the [Lower, Upper) interval of 64-bit values
332350
// corresponding to a set of keys. The meaning of the values themselves is
333351
// opaque to the BlockIntervalCollector.

Diff for: sstable/block_property_obsolete.go

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ func (o *obsoleteKeyBlockPropertyCollector) Finish(buf []byte) []byte {
5050
return res
5151
}
5252

53+
// Close is part of the BlockPropertyCollector interface.
54+
func (o *obsoleteKeyBlockPropertyCollector) Close() {}
55+
5356
// AddCollected is part of the BlockPropertyCollector interface.
5457
func (o *obsoleteKeyBlockPropertyCollector) AddCollected(oldProp []byte) error {
5558
isObsolete, err := obsoleteKeyBlockPropertyDecode(oldProp)

Diff for: sstable/block_property_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,9 @@ func (p *keyCountCollector) SupportsSuffixReplacement() bool {
13691369
return true
13701370
}
13711371

1372+
// Close is part of the BlockPropertyCollector interface.
1373+
func (p *keyCountCollector) Close() {}
1374+
13721375
type intSuffixIntervalMapper struct {
13731376
suffixLen int
13741377
}

Diff for: sstable/writer.go

+10
Original file line numberDiff line numberDiff line change
@@ -2091,6 +2091,16 @@ func (w *Writer) Close() (err error) {
20912091
indexBlockBufPool.Put(w.indexBlock)
20922092
w.indexBlock = nil
20932093

2094+
// Close property collectors. Closing is optional, we don't need to do it in
2095+
// error paths.
2096+
for _, p := range w.blockPropCollectors {
2097+
p.dataBlock.Close()
2098+
p.indexBlock.Close()
2099+
p.rangeKeyBlock.Close()
2100+
p.table.Close()
2101+
}
2102+
w.blockPropCollectors = nil
2103+
20942104
// Make any future calls to Set or Close return an error.
20952105
w.err = errWriterClosed
20962106
return nil

0 commit comments

Comments
 (0)