mirror of
https://github.com/prometheus/prometheus.git
synced 2025-08-07 06:37:17 +02:00
TSDB: Demonstrating Chunk pool is unnecessary
We only Put chunks in one place, that isn't called very often, so almost every Get results in allocating fresh memory. Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
parent
4dacd7572a
commit
c5e8fe6b71
@ -273,7 +273,6 @@ func (nopIterator) Err() error { return nil }
|
|||||||
|
|
||||||
// Pool is used to create and reuse chunk references to avoid allocations.
|
// Pool is used to create and reuse chunk references to avoid allocations.
|
||||||
type Pool interface {
|
type Pool interface {
|
||||||
Put(Chunk) error
|
|
||||||
Get(e Encoding, b []byte) (Chunk, error)
|
Get(e Encoding, b []byte) (Chunk, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -322,34 +321,6 @@ func (p *pool) Get(e Encoding, b []byte) (Chunk, error) {
|
|||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pool) Put(c Chunk) error {
|
|
||||||
var sp *sync.Pool
|
|
||||||
var ok bool
|
|
||||||
switch c.Encoding() {
|
|
||||||
case EncXOR:
|
|
||||||
_, ok = c.(*XORChunk)
|
|
||||||
sp = &p.xor
|
|
||||||
case EncHistogram:
|
|
||||||
_, ok = c.(*HistogramChunk)
|
|
||||||
sp = &p.histogram
|
|
||||||
case EncFloatHistogram:
|
|
||||||
_, ok = c.(*FloatHistogramChunk)
|
|
||||||
sp = &p.floatHistogram
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("invalid chunk encoding %q", c.Encoding())
|
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
// This may happen often with wrapped chunks. Nothing we can really do about
|
|
||||||
// it but returning an error would cause a lot of allocations again. Thus,
|
|
||||||
// we just skip it.
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Reset(nil)
|
|
||||||
sp.Put(c)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// FromData returns a chunk from a byte slice of chunk data.
|
// FromData returns a chunk from a byte slice of chunk data.
|
||||||
// This is there so that users of the library can easily create chunks from
|
// This is there so that users of the library can easily create chunks from
|
||||||
// bytes.
|
// bytes.
|
||||||
|
@ -158,46 +158,8 @@ func TestPool(t *testing.T) {
|
|||||||
stream: []byte("test"),
|
stream: []byte("test"),
|
||||||
count: 0,
|
count: 0,
|
||||||
}, b)
|
}, b)
|
||||||
|
|
||||||
b.count = 1
|
|
||||||
require.NoError(t, p.Put(c))
|
|
||||||
require.Equal(t, &bstream{
|
|
||||||
stream: nil,
|
|
||||||
count: 0,
|
|
||||||
}, b)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("put bad chunk wrapper", func(t *testing.T) {
|
|
||||||
// When a wrapping chunk poses as an encoding it can't be converted to, Put should skip it.
|
|
||||||
c := fakeChunk{
|
|
||||||
encoding: EncXOR,
|
|
||||||
t: t,
|
|
||||||
}
|
|
||||||
require.NoError(t, p.Put(c))
|
|
||||||
})
|
|
||||||
t.Run("put invalid encoding", func(t *testing.T) {
|
|
||||||
c := fakeChunk{
|
|
||||||
encoding: EncNone,
|
|
||||||
t: t,
|
|
||||||
}
|
|
||||||
require.EqualError(t, p.Put(c), `invalid chunk encoding "none"`)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
type fakeChunk struct {
|
|
||||||
Chunk
|
|
||||||
|
|
||||||
encoding Encoding
|
|
||||||
t *testing.T
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c fakeChunk) Encoding() Encoding {
|
|
||||||
return c.encoding
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c fakeChunk) Reset([]byte) {
|
|
||||||
c.t.Fatal("Reset should not be called")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func benchmarkIterator(b *testing.B, newChunk func() Chunk) {
|
func benchmarkIterator(b *testing.B, newChunk func() Chunk) {
|
||||||
|
@ -882,12 +882,6 @@ func (c DefaultBlockPopulator) PopulateBlock(ctx context.Context, metrics *Compa
|
|||||||
for _, chk := range chks {
|
for _, chk := range chks {
|
||||||
meta.Stats.NumSamples += uint64(chk.Chunk.NumSamples())
|
meta.Stats.NumSamples += uint64(chk.Chunk.NumSamples())
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, chk := range chks {
|
|
||||||
if err := chunkPool.Put(chk.Chunk); err != nil {
|
|
||||||
return fmt.Errorf("put chunk: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ref++
|
ref++
|
||||||
}
|
}
|
||||||
if err := set.Err(); err != nil {
|
if err := set.Err(); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user