Skip to content

image: decode zstd layers with zrip - #356

Merged
congwang-mk merged 3 commits into
multikernel:mainfrom
paddor:zrip
Nov 25, 2026
Merged

congwang-mk merged 3 commits into
multikernel:mainfrom
paddor:zrip

Conversation

@paddor

@paddor paddor commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor
  • Zstd layers decode with zrip 0.8.26 instead of ruzstd 0.9. zrip::FrameDecoder reads every frame of a stream and skips skippable frames, so it replaces the ZstdReader wrapper that zstd:chunked layers needed (about 60 lines).
  • zrip is pure Rust, #![forbid(unsafe_code)] in all its crates, MIT. I maintain it.
  • zrip builds with std and frame only. Its default simd feature dispatches to AVX2 code at runtime through fearless_simd, which uses unsafe; without it, layers decode about 30% slower than with it. An application that depends on zrip with simd gets it back.
  • The layer tests write their frames with zrip::compress at level 1 instead of ruzstd's Fastest; they still put a skippable frame between data frames.
  • ruzstd and twox-hash leave Cargo.lock, and the CI dependency check forbids zrip where it forbade ruzstd.

A 292 MB rootfs layer (15,188 entries: Python stdlib, docs, zoneinfo, locales, /usr/sbin) compressed with zstd -3, in an OCI layout. Pull: image::pull("oci:<dir>") into a fresh cache on tmpfs, so it verifies the blob, decodes it and unpacks the rootfs. Decode: the layer alone to io::sink with each decoder. One pinned CPU (i7-8700B, KVM), median of 5 processes (3 for the uncompressed layer), each the best of 5 rounds.

main (ruzstd) this PR (zrip)
pull, zstd layer (112.6 MB) 2.60 s 1.96 s
pull, same layer uncompressed (292.4 MB) 2.16 s 2.16 s
decode only 978 ms 358 ms

Zstd layers decode with zrip 0.8.25 instead of ruzstd. zrip's
FrameDecoder reads every frame of a stream and skips skippable frames,
so it replaces the ZstdReader wrapper that zstd:chunked layers needed.
The tests write their frames with zrip as well.

A 292 MB rootfs layer at zstd level 3 decodes in 300 ms instead of
961 ms, and pulling its OCI layout takes 1.92 s instead of 2.58 s.
zrip 0.8.26 is the current release. It changes only levels 3 and 4 of the
encoder and adds an option to drop the content checksum; layer decoding is
unchanged.

@congwang-mk congwang-mk 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.

Thanks for the PR and for the careful benchmark writeup. The swap itself looks correct to me (multi-frame, skippable frames, truncation and digest verification all still behave). A few comments inline.

cow = ["dep:walkdir", "dep:pathdiff", "dep:ring"]
profiles = ["dep:toml"]
image = ["cow", "dep:uuid", "dep:tar", "dep:flate2", "dep:ruzstd", "dep:base64", "dep:bollard", "dep:futures-util"]
image = ["cow", "dep:uuid", "dep:tar", "dep:flate2", "dep:zrip", "dep:base64", "dep:bollard", "dep:futures-util"]

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.

The dependency guard in .github/workflows/ci.yml (lines 81-86) still forbids ruzstd for the non-image feature sets. Please replace it with zrip there, otherwise nothing catches the zstd decoder leaking into those builds.

Comment thread crates/sandlock-core/Cargo.toml Outdated
tar = { version = "0.4", optional = true }
flate2 = { version = "1", default-features = false, features = ["rust_backend"], optional = true }
ruzstd = { version = "0.9", optional = true }
zrip = { version = "0.8.26", optional = true }

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.

My main hesitation is maturity: this path parses untrusted image layers, and zrip is about five months old with 46 releases (0.8.25 fixed a decoder over-reservation the day before this pin). We also do not verify diff_ids, so a wrong decode of a frame without a content checksum would go unnoticed. Is the decoder fuzzed differentially against reference zstd?

Comment thread Cargo.lock Outdated

[[package]]
name = "fearless_simd"
version = "1.2.0"

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.

Note that the default simd feature brings in fearless_simd, which does contain unsafe, so the tree is not unsafe-free. How much decode speed is lost with default-features = false, features = ["std", "frame"]?

sandlock-core takes zrip with only its std and frame features. The default
simd feature brings fearless_simd, which holds the unsafe target-feature
calls, and ldm only serves the encoder. Without the dispatch, a 292 MB
zstd -3 layer decodes in 358 ms instead of 276 ms (ruzstd: 978 ms), and
pulling it takes 1.96 s instead of 1.88 s. Applications can still enable
zrip's simd feature themselves.

The CI dependency check forbids zrip instead of ruzstd in the feature sets
without image.
@congwang-mk
congwang-mk merged commit 55390f8 into multikernel:main Nov 25, 2026
25 checks passed
@paddor

paddor commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review and the merge! Answers to your questions, for the record:

  • fearless_simd: The last commit takes zrip with std + frame only, so the tree has no fearless_simd. That also drops ldm, which only the encoder uses. Same layer and machine as in the description:

    pull decode
    ruzstd 2.60 s 978 ms
    zrip, default features 1.88 s 276 ms
    zrip, std + frame (merged) 1.96 s 358 ms

    An application that wants the speed back can depend on zrip with features = ["simd"], and Cargo enables it for sandlock as well.

  • Differential fuzzing: yes. decode_differential feeds each input to zrip's one-shot decoder, FrameDecoder and PushDecoder, and to C zstd 1.6.0, with and without a dictionary. It fails unless both accept with byte-identical output, or both reject. Other targets decode C zstd's output and corrupted frames. A weekly CI job runs each target for 30 minutes and keeps the corpora between runs. The same check runs as a regular test on every push: https://github.com/paddor/zrip/blob/main/tests/differential/check.rs

  • The 0.8.25 fix was about memory, not output. A 14-byte header could make the streaming decoders reserve 256 MiB up front. They now reserve at most 8 MiB and grow with the actual output.

  • Checksums: Zstd layers now get their content checksum checked. ruzstd computes the XXH64 but leaves the comparison to the caller, and the old ZstdReader never compared it. zrip::FrameDecoder fails with ChecksumMismatch instead. The zstd CLI writes that checksum by default.

  • diff_ids: checking them would catch any decoder bug, gzip included. I'm happy to send that as a separate PR. It costs one SHA-256 pass over the uncompressed tar, about what verifying an uncompressed layer's blob costs today.

@paddor
paddor deleted the zrip branch November 25, 2026 23:33
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.

2 participants