Skip to content

blob/gcsblob: size the upload buffer to the blob in WriteAll - #3858

Open
iamabhilaksh wants to merge 2 commits into
google:masterfrom
iamabhilaksh:gcsblob-writeall-sizehint
Open

iamabhilaksh wants to merge 2 commits into
google:masterfrom
iamabhilaksh:gcsblob-writeall-sizehint

Conversation

@iamabhilaksh

@iamabhilaksh iamabhilaksh commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem

When BufferSize is not set, gcsblob gives every upload a 16 MiB memory buffer, even for a 1 KiB blob. Many small writes at once can use hundreds of MB: 16 writes of 10 KiB grew memory by about 269 MB on real GCS. WriteAll knows the blob's size but didn't pass it on. Details in #3857.

Fixes #3857

Change

  • WriteAll now tells the driver how big the blob is, through a new SizeHint field in driver.WriterOptions.
  • gcsblob uses it to make the upload buffer just bigger than the blob, instead of 16 MiB. One byte bigger keeps the upload to a single request. The storage library rounds the buffer up to 256 KiB, so small writes use 256 KiB.
  • NewWriter now goes through a small helper, so the "Writer was never closed" log uses runtime.Caller(2) to keep pointing at the caller's code, like the Reader does.
  • TestBufferSize checks the buffer size for different hints. TestWriteAllChunkSize checks the buffer gcsblob gets from WriteAll (small, empty, no ContentType, 16 MiB, with BufferSize) and from NewWriter.

Impact and risk

  • Impact: small WriteAll calls on GCS use about 98% less memory.
  • Risk: low. Nothing changes when BufferSize is set, for NewWriter and Upload, for blobs of 16 MiB or more, or for other drivers.

Testing

  • The small, empty and no-ContentType cases in TestWriteAllChunkSize fail on master (got ChunkSize 16777216) and pass with this change.
  • Real GCS:
    • Small writes (up to 100 KiB) allocate about 0.30 MB instead of 16.8 MB.
    • 16 small writes at the same time use about 4.6 MB of extra memory instead of 269 MB.
    • Same number of requests and same speed.
    • Retries still work: with retries on and a test server that fails the first upload, the retry sends the full data.
  • go test -race, go vet, gofmt and golangci-lint v2.14.0 pass on go1.27.3 and go1.26.10.

When BufferSize is 0, each storage.Writer allocated a 16 MiB buffer,
even for tiny writes. WriteAll now passes the blob size to the driver
as a hint, and gcsblob uses it to cap the buffer.
…ithout ContentType

newWriter adds a stack frame, so use runtime.Caller(2) like newRangeReader.
@iamabhilaksh
iamabhilaksh marked this pull request as ready for review November 23, 2026 08:32

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

blob/gcsblob: every write allocates a 16 MiB buffer, even for small objects

1 participant