Skip to content

storage: size file reader buffers for small blobs - #5070

Merged
milosgajdos merged 2 commits into
distribution:mainfrom
perfloop-oss:perfloop-pr-open-fxsyv033s2
Nov 10, 2026
Merged

milosgajdos merged 2 commits into
distribution:mainfrom
perfloop-oss:perfloop-pr-open-fxsyv033s2

Conversation

@perfloop-agent

Copy link
Copy Markdown
Contributor

Cold reads currently reserve a 4 MiB buffer even for a small blob. Cap the initial buffer at the known blob size, leaving the buffering library's minimum and the existing 4 MiB ceiling intact. Allocation remains lazy, and seeks reuse the same buffer. No storage-driver interface or configuration changes are needed.

For a direct download of the existing 575-byte image-config fixture, median allocation fell from 4,200,063 to 5,570 B/download across 11 paired runs, with 100 downloads per arm per run. This includes descriptor-cache reuse and HTTP response generation with the in-memory storage driver, not remote-backend or network throughput.

registry/storage/filereader_benchmark_test.go provides the native cold-reader benchmark for small and large streams. The sizing test covers empty, tiny and ceiling-sized buffers and verifies reuse after seeking.

Backend throughput, production size mix and concurrency remain unmeasured. Redirected downloads and the direct-file path proposed in #5052 are outside the benefiting operation.

Go tests passed for file-reader sizing and reuse, blob reading and upload validation (including non-resumable hashing), and blob HTTP handling. Scoped static analysis reported no issues.

AI assisted with this change.

cc @milosgajdos

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart. To ask for a change, comment or review in plain words. How Perfloop handles pull requests.

Signed-off-by: Perfloop Agent <[email protected]>
Signed-off-by: Tomás Senart <[email protected]>
Signed-off-by: Perfloop Agent <[email protected]>
Signed-off-by: Tomás Senart <[email protected]>
@milosgajdos
milosgajdos merged commit 7ef76eb into distribution:main Nov 10, 2026
25 checks passed
jgehrcke added a commit to jgehrcke/distribution that referenced this pull request Nov 12, 2026
This patch fixes two defects in blob GET requests on the filesystem
driver and significantly reduces the CPU time that the registry consumes
for serving them. It passes the blob file directly to http.ServeContent.

This patch was extracted from distribution#5052. That PR builds on this patch to
enable sendfile(2), which requires passing an *os.File to
http.ServeContent.

Performance impact
------------------

With a warm page cache, this patch reduces registry total CPU time by
13% to 42% per whole-blob GET (100 KiB to 32 MiB) and by 9% to 58% per
range GET.

Before this patch, fileReader allocated a buffer of up to 4 MiB per GET
and copied blobs larger than 32 KiB a second time. Now ServeBlob passes
the *os.File to http.ServeContent.

Registry total CPU time per GET in ms (mean +/- sample SD), warm page
cache, linux/arm64:

    Transport   Blob    Before (n=15)      Now (n=15)         Change
    plain HTTP   4 MiB   1.994 +/- 0.026    1.160 +/- 0.009   -41.8%
    plain HTTP  32 MiB   9.423 +/- 0.061    6.851 +/- 0.021   -27.3%
    TLS          4 MiB   3.401 +/- 0.027    2.404 +/- 0.018   -29.3%
    TLS         32 MiB  20.159 +/- 0.063   17.528 +/- 0.113   -13.0%

The 4 MiB buffer dates from February 2015 (0270bec). That commit
message records no measurement or specific reasoning. Does removing this
buffer hurt? Would a smaller buffer be better? With a warm page cache,
no: buffers from 32 KiB to 1 MiB (tested on d14ff2f) used the same or
more registry total CPU time than this patch. With a cold page cache,
removing the buffer has a cost that is small compared to the 13% to 42%
gain with a warm page cache: registry total CPU time was 2% to 7% higher
(1 GiB blobs, local NVMe drive).

Fixed
-----

The registry now opens the blob file before it sets the response
status, and sets Content-Length from the file size instead of the blob
descriptor. Passing the file to http.ServeContent requires both
changes. Each change fixes one defect (distribution#5080):

- The blob file is missing or cannot be opened.
  Before: The registry sent status 200, a Content-Length header and no
  body (missing file: issues distribution#5010 and distribution#2966).
  Now: The registry sends status 404 with error code BLOB_UNKNOWN for a
  missing file. It sends status 500 for other open errors.
- The blob file size is less than the size in the blob descriptor.
  Before: The registry set Content-Length from the size in the blob
  descriptor and sent a truncated body (curl, for example, reports
  error 18).
  Now: The registry sets Content-Length from the file size.

Before this patch, skopeo logged "unexpected EOF" and retried in both
cases.

Remarks
-------

This patch applies only to the filesystem driver, when its Reader
returns an *os.File. For S3, Azure, GCS and all other storage drivers,
the registry still uses fileReader and its buffer (capped at blob size
since distribution#5070). HEAD responses are unchanged.

If the blob file is missing, the registry now answers every GET with
status 404. Previously, it answered some conditional GETs with status
304 or 412. The blob file is authoritative.

The registry still sets Docker-Content-Digest and ETag from the blob
descriptor. When the blob file is smaller than the size in the blob
descriptor, the transfer now completes. Only a client that checks size
or digest detects the mismatch.

net/http can use sendfile(2) for plain HTTP only. For TLS, distribution#5052
reduces CPU time no further.

Related issues: distribution#2367, distribution#5010, distribution#2966, distribution#2778. Related PR: distribution#5008.

Fixes distribution#5080

Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
jgehrcke added a commit to jgehrcke/distribution that referenced this pull request Nov 12, 2026
This patch fixes two defects in blob GET requests on the filesystem
driver and significantly reduces the CPU time that the registry consumes
for serving them. It passes the blob file directly to http.ServeContent.

This patch was extracted from distribution#5052. That PR builds on this patch to
enable sendfile(2), which requires passing an *os.File to
http.ServeContent.

Performance impact
------------------

With a warm page cache, this patch reduces registry total CPU time by
13% to 42% per whole-blob GET (100 KiB to 32 MiB) and by 9% to 58% per
range GET.

Before this patch, fileReader allocated a buffer of up to 4 MiB per GET
and copied blobs larger than 32 KiB a second time. Now ServeBlob passes
the *os.File to http.ServeContent.

Registry total CPU time per GET in ms (mean +/- sample SD), warm page
cache, linux/arm64:

    Transport   Blob    Before (n=15)      Now (n=15)         Change
    plain HTTP   4 MiB   1.994 +/- 0.026    1.160 +/- 0.009   -41.8%
    plain HTTP  32 MiB   9.423 +/- 0.061    6.851 +/- 0.021   -27.3%
    TLS          4 MiB   3.401 +/- 0.027    2.404 +/- 0.018   -29.3%
    TLS         32 MiB  20.159 +/- 0.063   17.528 +/- 0.113   -13.0%

The 4 MiB buffer dates from February 2015 (0270bec). That commit
message records no measurement or specific reasoning. Does removing this
buffer hurt? Would a smaller buffer be better? With a warm page cache,
no: buffers from 32 KiB to 1 MiB (tested on d14ff2f) used the same or
more registry total CPU time than this patch. With a cold page cache,
removing the buffer has a cost that is small compared to the 13% to 42%
gain with a warm page cache: registry total CPU time was 2% to 7% higher
(1 GiB blobs, local NVMe drive).

Fixed
-----

The registry now opens the blob file before it sets the response
status, and sets Content-Length from the file size instead of the blob
descriptor. Passing the file to http.ServeContent requires both
changes. Each change fixes one defect (distribution#5080):

- The blob file is missing or cannot be opened.
  Before: The registry sent status 200, a Content-Length header and no
  body (missing file: issues distribution#5010 and distribution#2966).
  Now: The registry sends status 404 with error code BLOB_UNKNOWN for a
  missing file. It sends status 500 for other open errors.
- The blob file size is less than the size in the blob descriptor.
  Before: The registry set Content-Length from the size in the blob
  descriptor and sent a truncated body (curl, for example, reports
  error 18).
  Now: The registry sets Content-Length from the file size.

Before this patch, skopeo logged "unexpected EOF" and retried in both
cases.

Remarks
-------

This patch applies only to the filesystem driver, when its Reader
returns an *os.File. For S3, Azure, GCS and all other storage drivers,
the registry still uses fileReader and its buffer (capped at blob size
since distribution#5070). HEAD responses are unchanged.

If the blob file is missing, the registry now answers every GET with
status 404. Previously, it answered some conditional GETs with status
304 or 412. The blob file is authoritative.

In a pull-through cache, the registry now answers a GET with status 404
when the local blob file is missing and its blob descriptor is still
cached. The registry does not fetch the blob from the upstream again.
Fetching it again is what distribution#5010 prefers, and is left for a follow-up.

The registry still sets Docker-Content-Digest and ETag from the blob
descriptor. When the blob file is smaller than the size in the blob
descriptor, the transfer now completes. Only a client that checks size
or digest detects the mismatch.

net/http can use sendfile(2) for plain HTTP only. For TLS, distribution#5052
reduces CPU time no further.

Related issues: distribution#2367, distribution#5010, distribution#2966, distribution#2778. Related PR: distribution#5008.

Fixes distribution#5080

Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
jgehrcke added a commit to jgehrcke/distribution that referenced this pull request Nov 12, 2026
This patch fixes two defects in blob GET requests on the filesystem
driver and significantly reduces the CPU time that the registry consumes
for serving them. It passes the blob file directly to http.ServeContent.

This patch was extracted from distribution#5052. That PR builds on this patch to
enable sendfile(2), which requires passing an *os.File to
http.ServeContent.

Performance impact
------------------

With a warm page cache, this patch reduces registry total CPU time by
13% to 42% per whole-blob GET (100 KiB to 32 MiB) and by 9% to 58% per
range GET.

Before this patch, fileReader allocated a buffer of up to 4 MiB per GET
and copied blobs larger than 32 KiB a second time. Now ServeBlob passes
the *os.File to http.ServeContent.

Registry total CPU time per GET in ms (mean +/- sample SD), warm page
cache, linux/arm64:

    Transport   Blob    Before (n=15)      Now (n=15)         Change
    plain HTTP   4 MiB   1.994 +/- 0.026    1.160 +/- 0.009   -41.8%
    plain HTTP  32 MiB   9.423 +/- 0.061    6.851 +/- 0.021   -27.3%
    TLS          4 MiB   3.401 +/- 0.027    2.404 +/- 0.018   -29.3%
    TLS         32 MiB  20.159 +/- 0.063   17.528 +/- 0.113   -13.0%

The 4 MiB buffer dates from February 2015 (0270bec). That commit
message records no measurement or specific reasoning. Does removing this
buffer hurt? Would a smaller buffer be better? With a warm page cache,
no: buffers from 32 KiB to 1 MiB (tested on d14ff2f) used the same or
more registry total CPU time than this patch. With a cold page cache,
removing the buffer has a cost that is small compared to the 13% to 42%
gain with a warm page cache: registry total CPU time was 2% to 7% higher
(1 GiB blobs, local NVMe drive).

Fixed
-----

The registry now opens the blob file before it sets the response
status, and sets Content-Length from the file size instead of the blob
descriptor. Passing the file to http.ServeContent requires both
changes. Each change fixes one defect (distribution#5080):

- The blob file is missing or cannot be opened.
  Before: The registry sent status 200, a Content-Length header and no
  body (missing file: issues distribution#5010 and distribution#2966).
  Now: The registry sends status 404 with error code BLOB_UNKNOWN for a
  missing file. It sends status 500 for other open errors.
- The blob file size is less than the size in the blob descriptor.
  Before: The registry set Content-Length from the size in the blob
  descriptor and sent a truncated body (curl, for example, reports
  error 18).
  Now: The registry sets Content-Length from the file size.

Before this patch, skopeo logged "unexpected EOF" and retried in both
cases.

Remarks
-------

This patch applies only to the filesystem driver, when its Reader
returns an *os.File. For S3, Azure, GCS and all other storage drivers,
the registry still uses fileReader and its buffer (capped at blob size
since distribution#5070). HEAD responses are unchanged.

If the blob file is missing, the registry now answers every GET with
status 404. Previously, it answered some conditional GETs with status
304 or 412. The blob file is authoritative, not the blob descriptor,
which can be a stale entry in the blob descriptor cache.

In a pull-through cache, the registry now answers a GET with status 404
when the local blob file is missing and its blob descriptor is still
cached. The registry does not fetch the blob from the upstream again.
Fetching it again is what distribution#5010 prefers, and is left for a follow-up.

The registry still sets Docker-Content-Digest and ETag from the blob
descriptor. When the size of the blob file differs from the size in the
blob descriptor, the registry now sends the whole file, and the transfer
completes. Only a client that checks size or digest detects the
mismatch.

net/http can use sendfile(2) for plain HTTP only. For TLS, distribution#5052 reduced
registry total CPU time by at most 7% more (measured for range GETs).

Related issues: distribution#2367, distribution#5010, distribution#2966, distribution#2778. Related PR: distribution#5008.

Fixes distribution#5080

Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
jgehrcke added a commit to jgehrcke/distribution that referenced this pull request Nov 12, 2026
This patch fixes two defects in blob GET requests on the filesystem
driver and significantly reduces the CPU time that the registry consumes
for serving them. It passes the blob file directly to
`http.ServeContent`.

This patch was extracted from distribution#5052. That PR builds on this patch to
enable sendfile(2), which requires passing an `*os.File` to
`http.ServeContent`.

Performance impact
------------------

With a warm page cache, this patch reduces registry total CPU time by
13% to 42% per whole-blob GET (100 KiB to 32 MiB) and by 9% to 58% per
range GET.

Before this patch, `fileReader` allocated a buffer of up to 4 MiB per
GET and copied blobs larger than 32 KiB a second time. Now `ServeBlob`
passes `*os.File` directly to `http.ServeContent`.

Registry total CPU time per GET in ms (mean +/- sample SD), warm page
cache, linux/arm64:

    Transport   Blob    Before (n=15)      Now (n=15)         Change
    plain HTTP   4 MiB   1.994 +/- 0.026    1.160 +/- 0.009   -41.8%
    plain HTTP  32 MiB   9.423 +/- 0.061    6.851 +/- 0.021   -27.3%
    TLS          4 MiB   3.401 +/- 0.027    2.404 +/- 0.018   -29.3%
    TLS         32 MiB  20.159 +/- 0.063   17.528 +/- 0.113   -13.0%

The 4 MiB buffer dates from February 2015 (0270bec). That commit
message records no measurement or specific reasoning.

Does removing this buffer hurt? Would a smaller buffer be better? With a
warm page cache, no: buffers from 32 KiB to 1 MiB (tested on d14ff2f)
used the same or more registry total CPU time than this patch. With a
cold page cache, removing the buffer has a cost that is small compared
to the aforementioned gains (registry total CPU time was 2% to 7% higher
for 1 GiB blobs, local NVMe drive).

Fixed
-----

The registry now opens the blob file before it sets the response
status, and sets Content-Length from the file size instead of the blob
descriptor. Passing the file to `http.ServeContent` requires both
changes. Each change fixes one defect (distribution#5080):

- The blob file is missing or cannot be opened.
  Before: The registry sent status 200, a Content-Length header and no
  body (missing file: issues distribution#5010 and distribution#2966).
  Now: The registry sends status 404 with error code BLOB_UNKNOWN for a
  missing file. It sends status 500 for other open errors.
- The blob file size is smaller than the size stored in the blob
  descriptor.
  Before: The registry set Content-Length from the size in the blob
  descriptor and sent a truncated body (curl, for example, reports
  error 18).
  Now: The registry sets Content-Length from the file size.

Before this patch, skopeo for example would log "unexpected EOF" and
retry in both cases.

Remarks
-------

This patch applies only to the filesystem driver, when its `Reader`
returns an `*os.File`. For S3, Azure, GCS and all other storage drivers,
the registry still uses `fileReader` and its buffer (capped at blob size
since distribution#5070). HEAD responses are unchanged.

If the blob file is missing, the registry now answers every GET with
status 404. Previously, it answered some conditional GETs with status
304 or 412. The blob file is authoritative, not the blob descriptor,
which can be a stale entry in the blob descriptor cache.

In a pull-through cache, the registry now answers a GET with status 404
when the local blob file is missing and its blob descriptor is still
cached. The registry does not fetch the blob from the upstream again.
Fetching it again is what distribution#5010 prefers, and is left for a follow-up.

The registry still sets Docker-Content-Digest and ETag from the blob
descriptor. When the blob file is smaller or larger than the size in the
blob descriptor, the registry now sends the whole file, and the transfer
completes. Only a client that checks size or digest detects the
mismatch.

Tests
-----

The new tests fail without the changes to `blobserver.go` and `blob.go`,
except `TestBlobServerOtherDriverReaderCalls`, which covers what this
patch does not change: one `Reader` call per GET on other drivers.

Related issues: distribution#2367, distribution#5010, distribution#2966, distribution#2778. Related PR: distribution#5008.

Fixes distribution#5080

Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
jgehrcke added a commit to jgehrcke/distribution that referenced this pull request Nov 17, 2026
This patch fixes two defects in blob GET requests on the filesystem
driver and significantly reduces the CPU time that the registry consumes
for serving them. It passes the blob file directly to
`http.ServeContent`.

This patch was extracted from distribution#5052. That PR builds on this patch to
enable sendfile(2), which requires passing an `*os.File` to
`http.ServeContent`.

Performance impact
------------------

With a warm page cache, this patch reduces registry total CPU time by
13% to 42% per whole-blob GET (100 KiB to 32 MiB) and by 9% to 58% per
range GET.

Before this patch, `fileReader` allocated a buffer of up to 4 MiB per
GET and copied blobs larger than 32 KiB a second time. Now `ServeBlob`
passes `*os.File` directly to `http.ServeContent`.

Registry total CPU time per GET in ms (mean +/- sample SD), warm page
cache, linux/arm64:

    Transport   Blob    Before (n=15)      Now (n=15)         Change
    plain HTTP   4 MiB   1.994 +/- 0.026    1.160 +/- 0.009   -41.8%
    plain HTTP  32 MiB   9.423 +/- 0.061    6.851 +/- 0.021   -27.3%
    TLS          4 MiB   3.401 +/- 0.027    2.404 +/- 0.018   -29.3%
    TLS         32 MiB  20.159 +/- 0.063   17.528 +/- 0.113   -13.0%

The 4 MiB buffer dates from February 2015 (0270bec). That commit
message records no measurement or specific reasoning.

Does removing this buffer hurt? Would a smaller buffer be better? With a
warm page cache, no: buffers from 32 KiB to 1 MiB (tested on d14ff2f)
used the same or more registry total CPU time than this patch. With a
cold page cache, removing the buffer has a cost that is small compared
to the aforementioned gains (registry total CPU time was 2% to 7% higher
for 1 GiB blobs, local NVMe drive).

Fixed
-----

The registry now opens the blob file before it sets the response
status, and sets Content-Length from the file size instead of the blob
descriptor. Passing the file to `http.ServeContent` requires both
changes. Each change fixes one defect (distribution#5080):

- The blob file is missing or cannot be opened.
  Before: The registry sent status 200, a Content-Length header and no
  body (missing file: issues distribution#5010 and distribution#2966).
  Now: The registry sends status 404 with error code BLOB_UNKNOWN for a
  missing file. It sends status 500 for other open errors.
- The blob file size is smaller than the size stored in the blob
  descriptor.
  Before: The registry set Content-Length from the size in the blob
  descriptor and sent a truncated body (curl, for example, reports
  error 18).
  Now: The registry sets Content-Length from the file size.

Before this patch, skopeo for example would log "unexpected EOF" and
retry in both cases.

Remarks
-------

For blob GETs, the filesystem driver opts in through `ReadSeeker`
and returns the original `*os.File`. Drivers and middleware without
`ReadSeeker` use `fileReader` and its buffer (capped at blob size
since distribution#5070).

If the blob file is missing, the registry now answers every GET with
status 404. Previously, it answered some conditional GETs with status
304 or 412. The blob file is authoritative, not the blob descriptor,
which can be a stale entry in the blob descriptor cache.

In a pull-through cache, the registry now answers a GET with status 404
when the local blob file is missing and its blob descriptor is still
cached. The registry does not fetch the blob from the upstream again.
Fetching it again is what distribution#5010 prefers, and is left for a follow-up.

The registry still sets Docker-Content-Digest and ETag from the blob
descriptor. When the blob file is smaller or larger than the size in the
blob descriptor, the registry now sends the whole file, and the transfer
completes. Only a client that checks size or digest detects the
mismatch.

Tests
-----

The stale-size and missing-file tests cover the two GET fixes.
`TestBlobServerDriverWithoutReadSeeker` checks that a range GET through
a filesystem wrapper makes one `Reader` call at the requested offset.

Related issues: distribution#2367, distribution#5010, distribution#2966, distribution#2778. Related PR: distribution#5008.

Fixes distribution#5080

Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants