@@ -24,7 +24,7 @@ type Bloom struct {
24
24
25
25
func (b * Bloom ) Encode (enc * encoding.Encbuf ) error {
26
26
// divide by 8 b/c bloom capacity is measured in bits, but we want bytes
27
- buf := bytes .NewBuffer (make ([] byte , 0 , int (b .Capacity ()/ 8 )))
27
+ buf := bytes .NewBuffer (BloomPagePool . Get ( int (b .Capacity () / 8 )))
28
28
29
29
// TODO(owen-d): have encoder implement writer directly so we don't need
30
30
// to indirect via a buffer
@@ -36,6 +36,7 @@ func (b *Bloom) Encode(enc *encoding.Encbuf) error {
36
36
data := buf .Bytes ()
37
37
enc .PutUvarint (len (data )) // length of bloom filter
38
38
enc .PutBytes (data )
39
+ BloomPagePool .Put (data [:0 ]) // release to pool
39
40
return nil
40
41
}
41
42
@@ -63,14 +64,11 @@ func (b *Bloom) Decode(dec *encoding.Decbuf) error {
63
64
return nil
64
65
}
65
66
66
- func LazyDecodeBloomPage (r io.Reader , alloc Allocator , pool chunkenc.ReaderPool , page BloomPageHeader ) (* BloomPageDecoder , error ) {
67
- data , err := alloc .Get (page .Len )
68
- if err != nil {
69
- return nil , errors .Wrap (err , "allocating buffer" )
70
- }
71
- defer alloc .Put (data )
67
+ func LazyDecodeBloomPage (r io.Reader , pool chunkenc.ReaderPool , page BloomPageHeader ) (* BloomPageDecoder , error ) {
68
+ data := BloomPagePool .Get (page .Len )[:page .Len ]
69
+ defer BloomPagePool .Put (data )
72
70
73
- _ , err = io .ReadFull (r , data )
71
+ _ , err : = io .ReadFull (r , data )
74
72
if err != nil {
75
73
return nil , errors .Wrap (err , "reading bloom page" )
76
74
}
@@ -86,10 +84,7 @@ func LazyDecodeBloomPage(r io.Reader, alloc Allocator, pool chunkenc.ReaderPool,
86
84
}
87
85
defer pool .PutReader (decompressor )
88
86
89
- b , err := alloc .Get (page .DecompressedLen )
90
- if err != nil {
91
- return nil , errors .Wrap (err , "allocating buffer" )
92
- }
87
+ b := BloomPagePool .Get (page .DecompressedLen )[:page .DecompressedLen ]
93
88
94
89
if _ , err = io .ReadFull (decompressor , b ); err != nil {
95
90
return nil , errors .Wrap (err , "decompressing bloom page" )
@@ -101,18 +96,14 @@ func LazyDecodeBloomPage(r io.Reader, alloc Allocator, pool chunkenc.ReaderPool,
101
96
}
102
97
103
98
// shortcut to skip allocations when we know the page is not compressed
104
- func LazyDecodeBloomPageNoCompression (r io.Reader , alloc Allocator , page BloomPageHeader ) (* BloomPageDecoder , error ) {
99
+ func LazyDecodeBloomPageNoCompression (r io.Reader , page BloomPageHeader ) (* BloomPageDecoder , error ) {
105
100
// data + checksum
106
101
if page .Len != page .DecompressedLen + 4 {
107
102
return nil , errors .New ("the Len and DecompressedLen of the page do not match" )
108
103
}
104
+ data := BloomPagePool .Get (page .Len )[:page .Len ]
109
105
110
- data , err := alloc .Get (page .Len )
111
- if err != nil {
112
- return nil , errors .Wrap (err , "allocating buffer" )
113
- }
114
-
115
- _ , err = io .ReadFull (r , data )
106
+ _ , err := io .ReadFull (r , data )
116
107
if err != nil {
117
108
return nil , errors .Wrap (err , "reading bloom page" )
118
109
}
@@ -167,16 +158,12 @@ type BloomPageDecoder struct {
167
158
// This can only safely be used when the underlying bloom
168
159
// bytes don't escape the decoder:
169
160
// on reads in the bloom-gw but not in the bloom-compactor
170
- func (d * BloomPageDecoder ) Relinquish (alloc Allocator ) {
171
- if d == nil {
172
- return
173
- }
174
-
161
+ func (d * BloomPageDecoder ) Relinquish () {
175
162
data := d .data
176
163
d .data = nil
177
164
178
165
if cap (data ) > 0 {
179
- _ = alloc .Put (data )
166
+ BloomPagePool .Put (data )
180
167
}
181
168
}
182
169
@@ -290,7 +277,7 @@ func (b *BloomBlock) DecodeHeaders(r io.ReadSeeker) (uint32, error) {
290
277
// BloomPageDecoder returns a decoder for the given page index.
291
278
// It may skip the page if it's too large.
292
279
// NB(owen-d): if `skip` is true, err _must_ be nil.
293
- func (b * BloomBlock ) BloomPageDecoder (r io.ReadSeeker , alloc Allocator , pageIdx int , maxPageSize int , metrics * Metrics ) (res * BloomPageDecoder , skip bool , err error ) {
280
+ func (b * BloomBlock ) BloomPageDecoder (r io.ReadSeeker , pageIdx int , maxPageSize int , metrics * Metrics ) (res * BloomPageDecoder , skip bool , err error ) {
294
281
if pageIdx < 0 || pageIdx >= len (b .pageHeaders ) {
295
282
metrics .pagesSkipped .WithLabelValues (pageTypeBloom , skipReasonOOB ).Inc ()
296
283
metrics .bytesSkipped .WithLabelValues (pageTypeBloom , skipReasonOOB ).Add (float64 (b .pageHeaders [pageIdx ].DecompressedLen ))
@@ -313,9 +300,9 @@ func (b *BloomBlock) BloomPageDecoder(r io.ReadSeeker, alloc Allocator, pageIdx
313
300
}
314
301
315
302
if b .schema .encoding == chunkenc .EncNone {
316
- res , err = LazyDecodeBloomPageNoCompression (r , alloc , page )
303
+ res , err = LazyDecodeBloomPageNoCompression (r , page )
317
304
} else {
318
- res , err = LazyDecodeBloomPage (r , alloc , b .schema .DecompressorPool (), page )
305
+ res , err = LazyDecodeBloomPage (r , b .schema .DecompressorPool (), page )
319
306
}
320
307
321
308
if err != nil {
0 commit comments