Skip to content

Use klauspost/compress/gzip for decode - #13560

Merged
AkihiroSuda merged 1 commit into
containerd:mainfrom
jgehrcke:klauspost-gzip-decomp
Jun 30, 2026
Merged

AkihiroSuda merged 1 commit into
containerd:mainfrom
jgehrcke:klauspost-gzip-decomp

Conversation

@jgehrcke

@jgehrcke jgehrcke commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor

Related to #13559.

Swap the stdlib gzip decompression code for Klaus Post's inflate code in gzipDecompress. klauspost/compress is already a dependency, used by containerd to decompress zstd OCI blobs. This patch affects only gzip layer decompression.

Measurement

Gzip decode time for each image's largest layer on arm64 (NVIDIA Grace), Go 1.26.3, in seconds (mean of n=6, relative standard deviation under 1.5%):

image compressed → decompressed stdlib klauspost speedup
nvcr.io/nvidia/cuda:13.1.2-devel-ubi9 1.8 → 3.4 GiB 13.6 9.1 ~1.5×
docker.io/pytorch/pytorch:latest 3.4 → 7.0 GiB 41.7 28.8 ~1.4×

Allocation churn (measured for the cuda image): stdlib 305 MiB / 1.91M mallocs → klauspost 0.18 MiB / 1,948. We won't speculate about the practical impact, but it is a clear qualitative improvement.

Precedent

As of today, CRI-O, Podman, Buildah, and Skopeo all use klauspost's inflate code for decompressing gzip OCI layers: they share the common implementation in containers/image. Its gzip decompressor has returned pgzip.NewReader(r) since containers/image#543. For regular gzip blobs, pgzip falls back to klauspost's flate. This PR adopts the same inflate core directly.

Other popular projects expose klauspost's inflate code to untrusted or external input: VictoriaMetrics and Prometheus (HTTP bodies), NATS (inbound client WebSocket frames), IBM/sarama (Kafka message sets), MinIO (S3 Select and client .tar.gz uploads), CockroachDB (SQL decompress() on arbitrary bytes); also on external data at rest in Grafana Loki and TiDB.

Notes

The Go maintainers already trust klauspost's gzip code: in 1.27 the standard library will use klauspost's methods for compression (not yet decompression), see golang/go#75532.

The speedup is algorithmic, not optimized for any particular architecture.

The change is small and isolated, so backporting to containerd 1.7 and 2.x is worth discussing.

Use klauspost/compress for gzip layer decompression

Copilot AI review requested due to automatic review settings June 9, 2026 08:59
@github-project-automation github-project-automation Bot moved this to Needs Triage in Pull Request Review Jun 9, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR switches the internal gzip decompression path (and its corresponding test assertion) to use github.com/klauspost/compress/gzip, and updates vendored module metadata accordingly.

Changes:

  • Use klauspost/compress/gzip’s NewReader in gzipDecompress.
  • Update compression tests to assert against *klauspost/compress/gzip.Reader.
  • Refresh vendor/modules.txt to include the newly vendored gzip/flate subpackages.

Reviewed changes

Copilot reviewed 2 out of 25 changed files in this pull request and generated no comments.

File Description
vendor/modules.txt Adds missing vendored package entries for klauspost gzip/flate.
pkg/archive/compression/compression_test.go Updates imports and type assertion to match klauspost gzip reader.
pkg/archive/compression/compression.go Switches gzip decompression to klauspost gzip reader when not using external gzip.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jgehrcke

jgehrcke commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor Author

About the failing CI checks:

I handed the Fuzzing / go test -fuzz (pull_request) output to Claude and it roughly says (I edited it):

Unpatched stdlib runs at ~1.59M execs/sec and PASSES; CI (klauspost) ran at ~26k execs/sec (≈60× slower) and timed out. The patch genuinely slows this fuzzer. The harness creates a reader and discards it (no decode), so it's hammering NewReader in a tight loop; klauspost's NewReader does more per-call setup (it eagerly allocates its reusable buffers; exactly what makes it fast and low-alloc for real full-layer decodes).

I couldn't yet find the root cause for exit code 1 in the Vagrant/fedora44 CI output. Edit: this failure seems to be as of an unrelated cleanup order instability:

[...] After the test finished and its temp dir was torn down, that goroutine tried to finish a collection context, failed (readdirent on the already-removed mount-manager.v1.bolt dir), and logged an error

corresponding snippet:

panic: Log in goroutine after TestCRIImagePullTimeout/HoldingContentOpenWriterWithTransferService has completed:
       level=error msg="failed to finish collection context" ... gc.go:453
       error="readdirent /tmp/.../io.containerd.mount-manager.v1.bolt/...: no such file or directory"

@mikebrow

mikebrow commented Jun 15, 2026 •

Copy link
Copy Markdown
Member

ci issues were unrelated flakes, restarted and all passed, rebasing

@mikebrow
mikebrow force-pushed the klauspost-gzip-decomp branch from 4c27b90 to d5bbaf2 Compare June 15, 2026 18:36
@fidencio

fidencio commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

@k8s-ci-robot k8s-ci-robot added the size/XXL label last week

That's inaccurate, as the change itself is quite small (apart from vendoring).

Nice optimization here!

@mikebrow mikebrow left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Replace the stdlib compress/gzip decoder with klauspost's inflate.
Also see
containerd#13559

Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
Copilot AI review requested due to automatic review settings June 15, 2026 19:34
@mikebrow
mikebrow force-pushed the klauspost-gzip-decomp branch from d5bbaf2 to d8f13bf Compare June 15, 2026 19:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 25 changed files in this pull request and generated no new comments.

@github-project-automation github-project-automation Bot moved this from Needs Triage to Review In Progress in Pull Request Review Jun 16, 2026
@jgehrcke

Copy link
Copy Markdown
Contributor Author

Thanks everyone for your feedback. What's the 'merge process' from here looking like?

More importantly, I'd love to hear your opinion on potentially back-porting this.

Upside: reaching more users much faster.

Downside: well, back-porting "performance optimizations" is delicate territory -- might be excluded by principle here or not, don't know enough about containerd's philosophy here.

Actual, practical risk: I don't quite see one right now, but maybe on the supply chain / compliance front there may be tangible, strong arguments to not do this.

What do you all think?

@mikebrow

Copy link
Copy Markdown
Member

cc @dmcgowan @fuweid

@ningmingxiao

Copy link
Copy Markdown
Contributor

how about add some config to enable it ?

@fuweid

fuweid commented Jun 22, 2026

Copy link
Copy Markdown
Member

sounds good to me.

is it possible to compare benchmark with pgzip? currently, we use gpzip external binary to make it fast.
We can add some benchmark code for that as well. Once go libary improves that performance, we can switch back to go stdlib in the future.

#2640

@fuweid fuweid added this to the 2.4 milestone Jun 22, 2026
@djdongjin

Copy link
Copy Markdown
Member

how about add some config to enable it ?

I don't think we need a config to enable/disable it if it's a common perf optimization

@jgehrcke

jgehrcke commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor Author

is it possible to compare benchmark with pgzip?

:) That's the same code.

I answered that in the PR description above:

For regular gzip blobs, pgzip falls back to klauspost's flate

With "regular gzip blobs" I refer to those that have to be decompressed serially because they were compressed serially (/linearly, not in independent blocks). In that case, pgzip can not unfold its "parallelized decompression" magic; and falls back to using https://github.com/klauspost/compress/blob/v1.18.6/flate/inflate.go.

we use pgzip external binary to make it fast.

Just to reiterate that: for pgzip to unfold its parallel decomp potential, the input must have been compressed in a special way (in independent blocks). The vast majority of gzip-compressed OCI blobs in the wild have not been compressed with that method.

@AkihiroSuda AkihiroSuda left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks good, but we do not backport performance improvement PRs

@AkihiroSuda
AkihiroSuda added this pull request to the merge queue Jun 30, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 30, 2026
@AkihiroSuda
AkihiroSuda added this pull request to the merge queue Jun 30, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 30, 2026
@AkihiroSuda
AkihiroSuda added this pull request to the merge queue Jun 30, 2026
Merged via the queue into containerd:main with commit a6f337b Jun 30, 2026
114 of 126 checks passed
@github-project-automation github-project-automation Bot moved this from Review In Progress to Done in Pull Request Review Jun 30, 2026
@jgehrcke

Copy link
Copy Markdown
Contributor Author

Thanks for merge.

we do not backport performance improvement PRs

Thanks for the feedback @AkihiroSuda! I find this an expected point of view, also quoting myself from above:

Downside: well, back-porting "performance optimizations" is delicate territory -- might be excluded by principle here [...]

So, I empathize and think that this is the right stance in general.

I have to ask: must we use policy to govern that decision, or can we use pragmatism in this case? I take "there is no room for pragmatism here; we tried bending rules before and we don't want that anymore" as an answer.

If pragmatism is still allowed: I do believe that are good reasons to deviate from the rule in this special case.

(We have been trying to walk through the full picture and creating a mental model as exhaustive as possible about the overall trade-off, thinking through advantages and disadvantages and risk analysis; this patch addresses a pull-time bottleneck in a significant way, affecting many deployments and is practically risk-free and hence we think one absolutely could justify back-porting with the goal to get this to many users quickly).

@AkihiroSuda

AkihiroSuda commented Jun 30, 2026 •

Copy link
Copy Markdown
Member

practically risk-free

potentially comes with risks of regressions and vulnerabilities

@jgehrcke

jgehrcke commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

potentially comes with risks of regressions and vulnerabilities

Of course. I hope you can see that I understand that principled thinking and usually also defend it :-).

The CRI-O ecosystem has used the very same decompression code for decompressing regular gzip container images for years (also see "Precedent" in the PR description). That is a strong data point in this risk analysis; and is the major reason for why I wrote "practically risk-free".

Again, I understand the principled push-back, and I confidently defend that typically as well. I came from your point of view, and the more I looked I realized "oh, we may actually be able to do this responsibly". So, after looking at the details here, I have so far not yet found a strong argument to not do this. I think there may be valid arguments around compliance. I do believe the actual regression/security side of things is surprisingly(!) easy to reason about here.

I just now saw:

general policy of primarily backporting bugfixes

in the policy. Maybe the "primarily" leaves a bit of wiggle room here? :-) In any case, thanks for having this discussion!

@dmcgowan dmcgowan changed the title pkg/archive/compression: use klauspost/compress/gzip for decode Use klauspost/compress/gzip for decode Jul 28, 2026
@dmcgowan dmcgowan added the area/distribution Image Distribution label Jul 28, 2026
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
containerd 2.4.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>Welcome to the v2.4.0 release of containerd!

containerd 2.4 is a regular (non-LTS) release with a shorter support window,
intended for users who want to adopt new features sooner. As the release
following the 2.3 LTS, it is the point in the release cycle where previously
deprecated features may be removed, so this release may include breaking
changes; check the notes below and clear any deprecation warnings from your
current version before upgrading.

Users prioritizing stability and a longer support lifecycle should stay on the
2.3 LTS release.

### Highlights

#### Container Runtime Interface (CRI)

* Enable mount manager for image mounts in CRI ([#13542](containerd/containerd#13542))
* Export sandbox image and CNI directory configuration in CRI plugin info ([#13940](containerd/containerd#13940))
* Set default runtimeFeatures.UserNamespacesHostNetwork to true ([#13162](containerd/containerd#13162))
* Support OCI runtime feature introspection for non-runc runtimes ([#13504](containerd/containerd#13504))

#### Image Distribution

* Apply hardening to strip sensitive authentication headers when fetching descriptor URLs ([#12889](containerd/containerd#12889))
* Support propagating HTTP 299 warning headers from registries to the resolver ([#12698](containerd/containerd#12698))
* Use klauspost/compress for gzip layer decompression ([#13560](containerd/containerd#13560))

#### Image Storage

* Add client options to fetch all layer content during unpack even when snapshots exist ([#14126](containerd/containerd#14126))
* Include media type in content create events ([#13833](containerd/containerd#13833))
* Add forward References to the GC collection context ([#13634](containerd/containerd#13634))

#### Node Resource Interface (NRI)

* Expose container image name, digest, and config digest to NRI plugins ([#13960](containerd/containerd#13960))
* Emit deprecation warnings for plugins using deprecated NRI interfaces ([#13916](containerd/containerd#13916))

#### Runtime

* Mask /proc/interrupts and CPU thermal throttle sysfs paths in Linux containers by default ([#14090](containerd/containerd#14090))
* Add UpdateSandbox RPC to propagate sandbox controller updates to the shim ([#14105](containerd/containerd#14105))
* Avoid immediately restarting containers with restart=always policy after they are explicitly stopped ([#13993](containerd/containerd#13993))
* Pass tracing context from shim to runc and hooks ([#14036](containerd/containerd#14036))
* Fix user and group lookup failures in container rootfs containing symlinked /etc/passwd or /etc/group ([#13818](containerd/containerd#13818))
* Implement Windows named-pipe server and log streaming support in pkg/shim ([#13948](containerd/containerd#13948))
* Enable log scrubbing by default on Windows ([#13837](containerd/containerd#13837))
* Allow specifying parent checkpoint directory when checkpointing with runc ([#13699](containerd/containerd#13699))

#### Snapshotters

* Add Prometheus metrics for EROFS snapshotter layer content cache ([#13941](containerd/containerd#13941))
* Support warm image cache for erofs snapshotter ([#13813](containerd/containerd#13813))
* Add max size label for snapshots ([#13520](containerd/containerd#13520))

#### Breaking

* Remove deprecated CRI and tracing configuration options:
  * Remove `enable_cdi` in CRI runtime configuration (CDI is now always enabled)
  * Remove `bin_dir` in CRI CNI configuration (use `bin_dirs`)
  * Remove `endpoint`, `protocol`, and `insecure` in OTLP tracing processor configuration (use standard OTLP environment variables)
  * Remove `service_name` and `sampling_ratio` in internal tracing configuration (use standard OpenTelemetry environment variables) ([#14166](containerd/containerd#14166))
* Remove restore in CreateContainer ([#13871](containerd/containerd#13871))

#### Deprecations

* Deprecate containerd.io/runtime-allow-mounts shim annotation in favor of MountCapabilities bootstrap extension ([#14002](containerd/containerd#14002))
* Remove deprecated shim.Command from pkg ([#13991](containerd/containerd#13991))
* Deprecate task API address and version fields in runc options and move to CreateTaskRequest ([#13360](containerd/containerd#13360))

Please try out the release binaries and report any issues at
https://github.com/containerd/containerd/issues.

### Contributors

* Maksym Pavlenko
* Sebastiaan van Stijn
* Samuel Karp
* Derek McGowan
* Wei Fu
* Akihiro Suda
* Mike Brown
* Paweł Gronowski
* Chris Henzie
* Phil Estes
* Brian Goff
* Austin Vazquez
* ningmingxiao
* Jordan Liggitt
* Akhil Mohan
* Eshaan Mathur
* Krisztian Litkey
* Chris Ayoub
* Kazuyoshi Kato
* Kir Kolyshkin
* Sergey Kanzhelev
* Ahmet Alp Balkan
* Arpit Jain
* Cindy Li
* Damien Grisonnet
* Esteban Ginez
* Gao Xiang
* Harsh Rawat
* Laura Lorenz
* Maksim An
* Oleh Konko
* Philip Laine
* Abhishek Bhunia
* Alan Grosskurth
* Albin Kerouanton
* Alex Lyn
* Aman Raj
* Amir Alavi
* Amit Barve
* Andrew Halaney
* AprilNEA
* Arjun Yogidas
* Ayato Tokubi
* Aysha Afrah Ziya
* Ben Cressey
* Bing Hongtao
* Chris Crone
* Craig Gumbley
* Daniel De Graaf
* Davanum Srinivas
* Dr. Jan-Philip Gehrcke
* Harshal Patel
* Henry Wang
* Hsiu-Chi Tsai
* JP Phillips
* Jing Chen
* Kohei Tokunaga
* LEI WANG
* Martín Fernández
* Mikhail Dmitrichenko
* Nahum Litvin
* Nikolaus Schuetz
* Pablo Garcia Caceres
* Paco Xu
* Robert Cronin
* SaloniRathi
* Shambhavi Srivastava
* Tianon Gravi
* XlabAI
* Yuanliang Zhang
* ayush-panta
* crawfordxx
* cshung
* match man
* s3onghyun
* 归寂
* 徐晓伟

### Changes
<details><summary>658 commits</summary>
<p>

  * [`647fafa847`](containerd/containerd@647fafa) Prepare release notes for v2.4.0
* Prepare release notes for api/v1.12.0 ([#14170](containerd/containerd#14170))
  * [`5c4ea21de3`](containerd/containerd@5c4ea21) Prepare release notes for api/v1.12.0
* Deprecations and removals for 2.4 ([#14166](containerd/containerd#14166))
  * [`531b3a37b9`](containerd/containerd@531b3a3) tracing: remove deprecated tracing config options
  * [`ca8579a334`](containerd/containerd@ca8579a) tracing: add tests for otlp exporter and env vars
  * [`ee024b7c99`](containerd/containerd@ee024b7) tracing: remove deprecated otlp configs
  * [`f7c654fb4f`](containerd/containerd@f7c654f) cri: remove deprecated cni bin_dir
  * [`4f7de25abb`](containerd/containerd@4f7de25) cri: remove enable_cdi config option
  * [`830b48d3fd`](containerd/containerd@830b48d) cri: delay registry config removal to 2.7
* Prepare release notes for v2.4.0-rc.0 ([#14115](containerd/containerd#14115))
  * [`c02620e398`](containerd/containerd@c02620e) Prepare release notes for v2.4.0-rc.0
  * [`02c7c97f43`](containerd/containerd@02c7c97) Update release doc for 2.4.0 release
  * [`484e5aba58`](containerd/containerd@484e5ab) vendor: github.com/containerd/containerd/api v1.12.0-rc.1
  * [`67174d675c`](containerd/containerd@67174d6) mailmap: add Paweł Gronowski
* Update erofs snapshotter to record blob source ([#14107](containerd/containerd#14107))
  * [`697a7571a4`](containerd/containerd@697a757) erofs: give a dm-verity device a name unique to its mount
  * [`f75817eb3b`](containerd/containerd@f75817e) erofs: record where a layer blob is
  * [`d92088d822`](containerd/containerd@d92088d) erofs: refuse to apply into a read-only snapshot
  * [`8cc0b076a6`](containerd/containerd@8cc0b07) erofs: serve layer content cache on parented Prepare
* build(deps): bump the golang-x group with 4 updates ([#14150](containerd/containerd#14150))
  * [`75138b3fda`](containerd/containerd@75138b3) build(deps): bump the golang-x group with 4 updates
* build(deps): bump github.com/klauspost/compress from 1.19.2 to 1.20.0 ([#14153](containerd/containerd#14153))
  * [`353342cae6`](containerd/containerd@353342c) build(deps): bump github.com/klauspost/compress from 1.19.2 to 1.20.0
* pkg/oci: mask thermal interrupt info ([#14090](containerd/containerd#14090))
  * [`c176f185b0`](containerd/containerd@c176f185b08e14bde055d81dc509

See merge request: Harmonybrew/homebrew-core!20372
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

10 participants