@@ -212,18 +212,6 @@ func (b PhysicalBlock) Clone() PhysicalBlock {
212
212
return PhysicalBlock {data : data , trailer : b .trailer }
213
213
}
214
214
215
- // CloneUsingBuf makes a copy of the block data, using the given slice if it has
216
- // enough capacity.
217
- func (b PhysicalBlock ) CloneUsingBuf (buf []byte ) (_ PhysicalBlock , newBuf []byte ) {
218
- newBuf = append (buf [:0 ], b .data ... )
219
- return PhysicalBlock {data : newBuf , trailer : b .trailer }, newBuf
220
- }
221
-
222
- // IsCompressed returns true if the block is compressed.
223
- func (b * PhysicalBlock ) IsCompressed () bool {
224
- return CompressionIndicator (b .trailer [0 ]) != NoCompressionIndicator
225
- }
226
-
227
215
// WriteTo writes the block (including its trailer) to the provided Writable. If
228
216
// err == nil, n is the number of bytes successfully written to the Writable.
229
217
//
@@ -247,35 +235,37 @@ func (b *PhysicalBlock) WriteTo(w objstorage.Writable) (n int, err error) {
247
235
}
248
236
249
237
// CompressAndChecksum compresses and checksums the provided block, returning
250
- // the compressed block and its trailer. The dst argument is used for the
251
- // compressed payload if it's sufficiently large. If it's not, a new buffer is
252
- // allocated and *dst is updated to point to it.
238
+ // the compressed block and its trailer. The result is appended to the dst
239
+ // argument.
253
240
//
254
241
// If the compressed block is not sufficiently smaller than the original block,
255
- // the compressed payload is discarded and the original, uncompressed block is
256
- // used to avoid unnecessary decompression overhead at read time.
242
+ // the compressed payload is discarded and the original, uncompressed block data
243
+ // is used to avoid unnecessary decompression overhead at read time.
257
244
func CompressAndChecksum (
258
- dst * []byte , block []byte , compression Compression , checksummer * Checksummer ,
245
+ dst * []byte , blockData []byte , compression Compression , checksummer * Checksummer ,
259
246
) PhysicalBlock {
247
+ buf := (* dst )[:0 ]
260
248
// Compress the buffer, discarding the result if the improvement isn't at
261
249
// least 12.5%.
262
250
algo := NoCompressionIndicator
263
251
if compression != NoCompression {
264
- var compressed []byte
265
- algo , compressed = compress (compression , block , * dst )
266
- if algo != NoCompressionIndicator && cap (compressed ) > cap (* dst ) {
267
- * dst = compressed [:cap (compressed )]
268
- }
269
- if len (compressed ) < len (block )- len (block )/ 8 {
270
- block = compressed
271
- } else {
252
+ algo , buf = compress (compression , blockData , buf )
253
+ if len (buf ) >= len (blockData )- len (blockData )/ 8 {
272
254
algo = NoCompressionIndicator
273
255
}
274
256
}
257
+ if algo == NoCompressionIndicator {
258
+ // We don't want to use the given blockData buffer directly: typically the
259
+ // result will be written to disk and that can mangle the buffer, leading to
260
+ // fragile code.
261
+ buf = append (buf [:0 ], blockData ... )
262
+ }
263
+
264
+ * dst = buf
275
265
276
266
// Calculate the checksum.
277
- pb := PhysicalBlock {data : block }
278
- checksum := checksummer .Checksum (block , byte (algo ))
267
+ pb := PhysicalBlock {data : buf }
268
+ checksum := checksummer .Checksum (buf , byte (algo ))
279
269
pb .trailer = MakeTrailer (byte (algo ), checksum )
280
270
return pb
281
271
}
@@ -375,19 +365,8 @@ func (b *Buffer) CompressAndChecksum() (PhysicalBlock, *BufHandle) {
375
365
// Grab a buffer to use as the destination for compression.
376
366
compressedBuf := compressedBuffers .Get ()
377
367
pb := CompressAndChecksum (& compressedBuf .b , b .h .b , b .compression , & b .checksummer )
378
- if pb .IsCompressed () {
379
- // Compression was fruitful, and pb's data points into compressedBuf. We
380
- // can reuse b.Buffer because we've copied the compressed data.
381
- b .h .b = b .h .b [:0 ]
382
- return pb , compressedBuf
383
- }
384
- // Compression was not fruitful, and pb's data points into b.h. The
385
- // compressedBuf we retrieved from the pool isn't needed, but our b.h is.
386
- // Use the compressedBuf as the new b.h.
387
- pbHandle := b .h
388
- b .h = compressedBuf
389
368
b .h .b = b .h .b [:0 ]
390
- return pb , pbHandle
369
+ return pb , compressedBuf
391
370
}
392
371
393
372
// SetCompression changes the compression algorithm used by CompressAndChecksum.
0 commit comments