@@ -252,17 +252,6 @@ func (b *PhysicalBlock) WriteTo(w objstorage.Writable) (n int, err error) {
252
252
return len (b .data ) + len (b .trailer ), nil
253
253
}
254
254
255
- // CompressAndChecksumBufHandle is like CompressAndChecksum, but it will
256
- // retrieve a buffer for the compressed block from a sync.Pool and return a
257
- // *TempBuffer that the caller can use to release the buffer back to the pool.
258
- func CompressAndChecksumBufHandle (
259
- blockData []byte , compression Compression , checksummer * Checksummer ,
260
- ) (PhysicalBlock , * TempBuffer ) {
261
- compressedBuf := NewTempBuffer ()
262
- pb := CompressAndChecksum (& compressedBuf .b , blockData , compression , checksummer )
263
- return pb , compressedBuf
264
- }
265
-
266
255
// CompressAndChecksum compresses and checksums the provided block, returning
267
256
// the compressed block and its trailer. The result is appended to the dst
268
257
// argument.
@@ -299,108 +288,26 @@ func CompressAndChecksumWithCompressor(
299
288
return pb
300
289
}
301
290
302
- // A Buffer is a buffer for encoding a block. The caller mutates the buffer to
303
- // construct the uncompressed block, and calls CompressAndChecksum to produce
304
- // the physical, possibly-compressed PhysicalBlock. A Buffer recycles byte
305
- // slices used in construction of the uncompressed block and the compressed
306
- // physical block.
307
- type Buffer struct {
308
- h * TempBuffer
309
- compression Compression
310
- checksummer Checksummer
311
- }
312
-
313
- // Init configures the BlockBuffer with the specified compression and checksum
314
- // type.
315
- func (b * Buffer ) Init (compression Compression , checksumType ChecksumType ) {
316
- b .h = NewTempBuffer ()
317
- b .h .b = b .h .b [:0 ]
318
- b .compression = compression
319
- b .checksummer .Type = checksumType
320
- }
321
-
322
- // Checksummer returns the Checksummer for the Buffer.
323
- func (b * Buffer ) Checksummer () * Checksummer {
324
- return & b .checksummer
325
- }
326
-
327
- // Get returns the byte slice currently backing the Buffer.
328
- func (b * Buffer ) Get () []byte {
329
- return b .h .b
330
- }
331
-
332
- // Size returns the current size of the buffer.
333
- func (b * Buffer ) Size () int {
334
- return len (b .h .b )
335
- }
336
-
337
- // Append appends the contents of v to the buffer, returning the offset at which
338
- // it was appended, growing the buffer if necessary.
339
- func (b * Buffer ) Append (v []byte ) int {
340
- // We may need to grow b.Buffer to accommodate the new value. If necessary,
341
- // double the size of the buffer until it's sufficiently large.
342
- off := len (b .h .b )
343
- newLen := off + len (v )
344
- if cap (b .h .b ) < newLen {
345
- size := max (2 * cap (b .h .b ), 1024 )
346
- for size < newLen {
347
- size *= 2
348
- }
349
- b .h .b = slices .Grow (b .h .b , size - len (b .h .b ))
350
- }
351
- b .h .b = b .h .b [:newLen ]
352
- if n := copy (b .h .b [off :], v ); n != len (v ) {
353
- panic ("incorrect length computation" )
354
- }
355
- return off
356
- }
357
-
358
- // Resize resizes the buffer to the specified length, allocating if necessary.
359
- func (b * Buffer ) Resize (length int ) {
360
- if length > cap (b .h .b ) {
361
- b .h .b = slices .Grow (b .h .b , length - len (b .h .b ))
362
- }
363
- b .h .b = b .h .b [:length ]
364
- }
365
-
366
- // CompressAndChecksum compresses and checksums the block data, returning a
367
- // PhysicalBlock that is owned by the caller. The returned PhysicalBlock's
368
- // memory is backed by the returned TempBuffer. If non-nil, the returned
369
- // TempBuffer may be Released once the caller is done with the physical block to
370
- // recycle the block's underlying memory.
371
- //
372
- // When CompressAndChecksum returns, the callee has been reset and is ready to
373
- // be reused.
374
- func (b * Buffer ) CompressAndChecksum () (PhysicalBlock , * TempBuffer ) {
291
+ // CompressAndChecksumToTempBuffer compresses and checksums the provided block
292
+ // into a TempBuffer. The caller should Release() the TempBuffer once it is no
293
+ // longer necessary.
294
+ func CompressAndChecksumToTempBuffer (
295
+ blockData []byte , compressor Compressor , checksummer * Checksummer ,
296
+ ) (PhysicalBlock , * TempBuffer ) {
375
297
// Grab a buffer to use as the destination for compression.
376
298
compressedBuf := NewTempBuffer ()
377
- pb := CompressAndChecksum (& compressedBuf .b , b .h .b , b .compression , & b .checksummer )
378
- b .h .b = b .h .b [:0 ]
299
+ pb := CompressAndChecksumWithCompressor (& compressedBuf .b , blockData , compressor , checksummer )
379
300
return pb , compressedBuf
380
301
}
381
302
382
- // SetCompression changes the compression algorithm used by CompressAndChecksum.
383
- func (b * Buffer ) SetCompression (compression Compression ) {
384
- b .compression = compression
385
- }
386
-
387
- // Release may be called when a buffer will no longer be used. It releases to
388
- // pools any memory held by the Buffer so that it may be reused.
389
- func (b * Buffer ) Release () {
390
- if b .h != nil {
391
- b .h .Release ()
392
- b .h = nil
393
- }
394
- }
395
-
396
303
// TempBuffer is a buffer that is used temporarily and is released back to a
397
304
// pool for reuse.
398
305
type TempBuffer struct {
399
306
b []byte
400
307
}
401
308
402
- // NewTempBuffer returns a TempBuffer from the pool. TempBuffer.b will have zero
403
- // length and arbitrary capacity.
309
+ // NewTempBuffer returns a TempBuffer from the pool. The buffer will have zero
310
+ // size and length and arbitrary capacity.
404
311
func NewTempBuffer () * TempBuffer {
405
312
tb := tempBufferPool .Get ().(* TempBuffer )
406
313
if invariants .Enabled && len (tb .b ) > 0 {
@@ -409,6 +316,39 @@ func NewTempBuffer() *TempBuffer {
409
316
return tb
410
317
}
411
318
319
+ // Data returns the byte slice currently backing the Buffer.
320
+ func (tb * TempBuffer ) Data () []byte {
321
+ return tb .b
322
+ }
323
+
324
+ // Size returns the current size of the buffer.
325
+ func (tb * TempBuffer ) Size () int {
326
+ return len (tb .b )
327
+ }
328
+
329
+ // Append appends the contents of v to the buffer, growing the buffer if
330
+ // necessary. Returns the offset at which it was appended.
331
+ func (tb * TempBuffer ) Append (v []byte ) (startOffset int ) {
332
+ startOffset = len (tb .b )
333
+ tb .b = append (tb .b , v ... )
334
+ return startOffset
335
+ }
336
+
337
+ // Resize resizes the buffer to the specified length, allocating if necessary.
338
+ // If the length is longer than the current length, the values of the new bytes
339
+ // are arbitrary.
340
+ func (tb * TempBuffer ) Resize (length int ) {
341
+ if length > cap (tb .b ) {
342
+ tb .b = slices .Grow (tb .b , length - len (tb .b ))
343
+ }
344
+ tb .b = tb .b [:length ]
345
+ }
346
+
347
+ // Reset is equivalent to Resize(0).
348
+ func (tb * TempBuffer ) Reset () {
349
+ tb .b = tb .b [:0 ]
350
+ }
351
+
412
352
// Release releases the buffer back to the pool for reuse.
413
353
func (tb * TempBuffer ) Release () {
414
354
// Note we avoid releasing buffers that are larger than the configured
0 commit comments