s3: speed up multi-thread uploads by checksumming chunks in 1 MiB reads

WriteChunk checksums the chunk with io.Copy before sending it. For a
reader that implements neither WriterTo nor a memory backed Read - a
local file opened by a multi-thread copy - io.Copy falls back to its own
32 KiB buffer, so the source is read in 32 KiB pieces and the disk sees
thousands of requests per second where it should see hundreds.

Hand io.CopyBuffer a 1 MiB buffer instead. It is allocated for the
checksum pass rather than taken from the pool, where it could wait for
ever on a --max-buffer-memory below the page size. Readers that do
implement WriterTo, such as pool.RW, keep their fast path and don't get
a buffer.
This commit is contained in:
Roman Butor authored and Nick Craig-Wood committed 2026-10-08 14:55:14 +01:00
1 parent c79a1d9d49
commit a68480093c
1 file changed
+16 -1
+16 -1
View File
@@ -4727,8 +4727,23 @@ func (w *s3ChunkWriter) WriteChunk(ctx context.Context, chunkNumber int, reader
// create checksum of buffer for integrity checking
// currently there is no way to calculate the md5 without reading the chunk a 2nd time (1st read is in uploadMultipart)
// possible in AWS SDK v2 with trailers?
//
// io.Copy reads with a 32 KiB buffer from a reader that implements
// neither WriterTo nor a memory backed Read - which is what a local
// file opened by a multi-thread copy is - so give it a bigger one to
// keep the source reads a useful size. It isn't taken from the pool,
// where it could wait for ever on --max-buffer-memory. A reader with
// WriterTo, such as a pool.RW, doesn't need one.
m := md5.New()
currentChunkSize, err := io.Copy(m, reader)
var (
currentChunkSize int64
err error
)
if _, ok := reader.(io.WriterTo); ok {
currentChunkSize, err = io.Copy(m, reader)
} else {
currentChunkSize, err = io.CopyBuffer(m, reader, make([]byte, multipart.BufferSize))
}
if err != nil {
return -1, err
}