From 36a30a40b1902b119e15927b58ce7d5567e0e57d Mon Sep 17 00:00:00 2001 From: Abhilaksh Sharma Date: Mon, 16 Nov 2026 19:38:57 +0530 Subject: [PATCH 1/2] blob/gcsblob: size the upload buffer to the blob in WriteAll 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. --- blob/blob.go | 8 +++- blob/driver/driver.go | 4 ++ blob/gcsblob/gcsblob.go | 10 +++- blob/gcsblob/gcsblob_test.go | 89 ++++++++++++++++++++++++++++++++++-- 4 files changed, 105 insertions(+), 6 deletions(-) diff --git a/blob/blob.go b/blob/blob.go index 4877f7281d..e967bc8acb 100644 --- a/blob/blob.go +++ b/blob/blob.go @@ -1214,7 +1214,7 @@ func (b *Bucket) WriteAll(ctx context.Context, key string, p []byte, opts *Write sum := md5.Sum(p) optsToUse.ContentMD5 = sum[:] } - w, err := b.NewWriter(ctx, key, &optsToUse) + w, err := b.newWriter(ctx, key, &optsToUse, max(len(p), 1)) if err != nil { return err } @@ -1255,6 +1255,11 @@ func (b *Bucket) Upload(ctx context.Context, key string, r io.Reader, opts *Writ // The caller must call Close on the returned Writer, even if the write is // aborted. func (b *Bucket) NewWriter(ctx context.Context, key string, opts *WriterOptions) (_ *Writer, err error) { + return b.newWriter(ctx, key, opts, 0) +} + +// newWriter is NewWriter with a driver.WriterOptions.SizeHint. +func (b *Bucket) newWriter(ctx context.Context, key string, opts *WriterOptions, sizeHint int) (_ *Writer, err error) { if !utf8.ValidString(key) { return nil, gcerr.Newf(gcerr.InvalidArgument, nil, "blob: NewWriter key must be a valid UTF-8 string: %q", key) } @@ -1269,6 +1274,7 @@ func (b *Bucket) NewWriter(ctx context.Context, key string, opts *WriterOptions) ContentMD5: opts.ContentMD5, BufferSize: opts.BufferSize, MaxConcurrency: opts.MaxConcurrency, + SizeHint: sizeHint, BeforeWrite: opts.BeforeWrite, DisableContentTypeDetection: opts.DisableContentTypeDetection, IfNotExist: opts.IfNotExist, diff --git a/blob/driver/driver.go b/blob/driver/driver.go index ccb2171b7b..d8e60865d0 100644 --- a/blob/driver/driver.go +++ b/blob/driver/driver.go @@ -84,6 +84,10 @@ type WriterOptions struct { BufferSize int // MaxConcurrency changes the default concurrency for uploading parts. MaxConcurrency int + // SizeHint, if positive, is an upper bound on the number of bytes that + // will be written. Drivers may use it to avoid allocating buffers larger + // than the blob. Bucket.WriteAll sets it. + SizeHint int // CacheControl specifies caching attributes that services may use // when serving the blob. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control diff --git a/blob/gcsblob/gcsblob.go b/blob/gcsblob/gcsblob.go index b66ef9c5cd..2ba8c3d637 100644 --- a/blob/gcsblob/gcsblob.go +++ b/blob/gcsblob/gcsblob.go @@ -945,7 +945,7 @@ func (b *bucket) NewTypedWriter(ctx context.Context, key, contentType string, op w.ContentEncoding = opts.ContentEncoding w.ContentLanguage = opts.ContentLanguage w.ContentType = contentType - w.ChunkSize = bufferSize(opts.BufferSize) + w.ChunkSize = bufferSize(opts.BufferSize, opts.SizeHint) w.Metadata = opts.Metadata w.MD5 = opts.ContentMD5 w.ForceEmptyContentType = opts.DisableContentTypeDetection @@ -1112,8 +1112,14 @@ func (b *bucket) SignedURL(ctx context.Context, key string, dopts *driver.Signed return storage.SignedURL(b.name, key, opts) } -func bufferSize(size int) int { +func bufferSize(size, sizeHint int) int { if size == 0 { + // The Writer allocates a buffer of this size, so don't make it much + // larger than the blob. It must be larger than the blob for the upload + // to fit in a single request. + if sizeHint > 0 && sizeHint < googleapi.DefaultUploadChunkSize { + return sizeHint + 1 + } return googleapi.DefaultUploadChunkSize } else if size > 0 { return size diff --git a/blob/gcsblob/gcsblob_test.go b/blob/gcsblob/gcsblob_test.go index f4285d7c4a..08e2a11da9 100644 --- a/blob/gcsblob/gcsblob_test.go +++ b/blob/gcsblob/gcsblob_test.go @@ -397,8 +397,9 @@ func (verifyContentLanguage) ListObjectCheck(o *blob.ListObject) error { // GCS-specific unit tests. func TestBufferSize(t *testing.T) { tests := []struct { - size int - want int + size int + sizeHint int + want int }{ { size: 5 * 1024 * 1024, @@ -412,15 +413,97 @@ func TestBufferSize(t *testing.T) { size: -1024, want: 0, }, + { + sizeHint: 10 * 1024, + want: 10*1024 + 1, + }, + { + sizeHint: googleapi.DefaultUploadChunkSize - 1, + want: googleapi.DefaultUploadChunkSize, + }, + { + sizeHint: googleapi.DefaultUploadChunkSize, + want: googleapi.DefaultUploadChunkSize, + }, + { + size: 5 * 1024 * 1024, + sizeHint: 10 * 1024, + want: 5 * 1024 * 1024, + }, + { + size: -1024, + sizeHint: 10 * 1024, + want: 0, + }, } for i, test := range tests { - got := bufferSize(test.size) + got := bufferSize(test.size, test.sizeHint) if got != test.want { t.Errorf("%d) got buffer size %d, want %d", i, got, test.want) } } } +// TestWriteAllChunkSize checks that WriteAll doesn't give the storage.Writer +// a buffer larger than the blob. +func TestWriteAllChunkSize(t *testing.T) { + ctx := context.Background() + b, err := OpenBucket(ctx, &gcp.HTTPClient{}, "bucket", nil) + if err != nil { + t.Fatal(err) + } + defer closeWithErrorCheck(t, b) + + errStop := errors.New("stop before upload") + chunkSize := func(t *testing.T, write func(*blob.WriterOptions) error, bufferSize int) int { + got := -1 + opts := &blob.WriterOptions{ + BufferSize: bufferSize, + ContentType: "application/octet-stream", + BeforeWrite: func(as func(any) bool) error { + var sw *storage.Writer + if as(&sw) { + got = sw.ChunkSize + } + return errStop + }, + } + if err := write(opts); !errors.Is(err, errStop) { + t.Fatalf("got err %v, want %v", err, errStop) + } + return got + } + writeAll := func(n int) func(*blob.WriterOptions) error { + return func(opts *blob.WriterOptions) error { + return b.WriteAll(ctx, "key", make([]byte, n), opts) + } + } + newWriter := func(opts *blob.WriterOptions) error { + _, err := b.NewWriter(ctx, "key", opts) + return err + } + + tests := []struct { + description string + write func(*blob.WriterOptions) error + bufferSize int + want int + }{ + {"WriteAll small", writeAll(10 * 1024), 0, 10*1024 + 1}, + {"WriteAll empty", writeAll(0), 0, 2}, + {"WriteAll default size", writeAll(googleapi.DefaultUploadChunkSize), 0, googleapi.DefaultUploadChunkSize}, + {"WriteAll with BufferSize", writeAll(10 * 1024), 5 * 1024 * 1024, 5 * 1024 * 1024}, + {"NewWriter", newWriter, 0, googleapi.DefaultUploadChunkSize}, + } + for _, test := range tests { + t.Run(test.description, func(t *testing.T) { + if got := chunkSize(t, test.write, test.bufferSize); got != test.want { + t.Errorf("got ChunkSize %d, want %d", got, test.want) + } + }) + } +} + func TestOpenBucket(t *testing.T) { tests := []struct { description string From 8d1bbf8b506be6dfe54502793da632a16539f4b5 Mon Sep 17 00:00:00 2001 From: Abhilaksh Sharma Date: Mon, 23 Nov 2026 13:50:32 +0530 Subject: [PATCH 2/2] blob: keep the caller in the unclosed Writer log, and test WriteAll without ContentType newWriter adds a stack frame, so use runtime.Caller(2) like newRangeReader. --- blob/blob.go | 2 +- blob/gcsblob/gcsblob_test.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/blob/blob.go b/blob/blob.go index e967bc8acb..5bc5bf3dba 100644 --- a/blob/blob.go +++ b/blob/blob.go @@ -1346,7 +1346,7 @@ func (b *Bucket) newWriter(ctx context.Context, key string, opts *WriterOptions, w.opts = dopts w.buf = bytes.NewBuffer([]byte{}) } - _, file, lineno, ok := runtime.Caller(1) + _, file, lineno, ok := runtime.Caller(2) runtime.SetFinalizer(w, func(w *Writer) { if !w.closed { var caller string diff --git a/blob/gcsblob/gcsblob_test.go b/blob/gcsblob/gcsblob_test.go index 08e2a11da9..eb93b3f99b 100644 --- a/blob/gcsblob/gcsblob_test.go +++ b/blob/gcsblob/gcsblob_test.go @@ -478,6 +478,13 @@ func TestWriteAllChunkSize(t *testing.T) { return b.WriteAll(ctx, "key", make([]byte, n), opts) } } + // Without ContentType, the driver writer is created after content type detection. + writeAllDetect := func(n int) func(*blob.WriterOptions) error { + return func(opts *blob.WriterOptions) error { + opts.ContentType = "" + return b.WriteAll(ctx, "key", make([]byte, n), opts) + } + } newWriter := func(opts *blob.WriterOptions) error { _, err := b.NewWriter(ctx, "key", opts) return err @@ -491,6 +498,7 @@ func TestWriteAllChunkSize(t *testing.T) { }{ {"WriteAll small", writeAll(10 * 1024), 0, 10*1024 + 1}, {"WriteAll empty", writeAll(0), 0, 2}, + {"WriteAll small without ContentType", writeAllDetect(10 * 1024), 0, 10*1024 + 1}, {"WriteAll default size", writeAll(googleapi.DefaultUploadChunkSize), 0, googleapi.DefaultUploadChunkSize}, {"WriteAll with BufferSize", writeAll(10 * 1024), 5 * 1024 * 1024, 5 * 1024 * 1024}, {"NewWriter", newWriter, 0, googleapi.DefaultUploadChunkSize},