Skip to content

packing: replace greedy merge with statistical partitioning - #107

Merged
jlebon merged 5 commits into
coreos:mainfrom
jlebon:pr/algo-tweaks
Apr 30, 2026
Merged

jlebon merged 5 commits into
coreos:mainfrom
jlebon:pr/algo-tweaks

Conversation

@jlebon

@jlebon jlebon commented Apr 10, 2026 •

Copy link
Copy Markdown
Member

The old algorithm used a greedy merge approach (BinaryHeap-based,
minimizing total expected value loss per merge) which was fundamentally
unstable: small changes in input caused completely different merge
decisions, resulting in poor layer reuse across updates.

Additionally, because of its greediness, it easily fell into a local
optimum with a giant catch-all bucket with low stability and safer more
stable items in the other layers.

Replace it with a two-phase statistical partitioning approach inspired
by rpm-ostree's chunking algorithm:

  • Phase 1 classifies components by size, giving large components
    (linux-firmware, kernel, firefox) their own singleton layers.
  • Phase 2 assigns all remaining components to bins using stability tiers
    (high/mid/low) and deterministic name-based hashing, which ensures
    stable bin membership across builds without needing to track prior
    build state.

Also remove the stability fallback that assigned min(known)/2 to
components without stability data (xattr, bigfiles, unclaimed). These
now stay at 0.0 and are naturally handled by the stability tiers.

Benchmarked against rpm-ostree's native chunking on FCOS F43 (11
biweekly stable releases) and Silverblue F43 (3 weekly builds):

    FCOS:       46.4% reuse, 916 MiB avg download uncompressed (rpm-ostree: 34.0%, 1.2 GiB)
    Silverblue: 47.9% reuse, 2.8 GiB avg download uncompressed (rpm-ostree: 38.5%, 3.5 GiB)

Assisted-by: OpenCode (Claude Opus 4.6)

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request replaces the existing greedy clustering algorithm for OCI layer packing with a new statistical partitioning approach inspired by rpm-ostree. The new algorithm classifies components into layers based on size outliers (using median and MAD) and stability tiers, utilizing name-based hashing for deterministic binning. Feedback identifies critical logic errors where components could be silently dropped if the layer budget is exhausted or if a stability tier is allocated zero bins. Additionally, a fix is required for the use of an unstable Rust feature in the median calculation.

Comment thread src/packing.rs Resolved
Comment thread src/packing.rs Resolved
Comment thread src/packing.rs Resolved
@jlebon

jlebon commented Apr 10, 2026

Copy link
Copy Markdown
Member Author

Don't look too much at the code. This is still rough/raw from Opus. I still need to carefully go through it, but the results are encouraging. I made it sweep for optimal parameters for the FCOS and Silverblue sets (which hopefully generalizes well to non-Fedora data sets -- will try to do more validation there).

@jlebon
jlebon force-pushed the pr/algo-tweaks branch 3 times, most recently from 3a357ad to 259b0a7 Compare April 11, 2026 02:13
@cgwalters

Copy link
Copy Markdown
Member

There's plenty of other container images to look at, I think registry.redhat.io/rhelai1/bootc-cuda-rhel9 is another one we should have in our references. IIRC Chris was looking at tweaking its build to use user.component.

@castrojo

castrojo commented Apr 13, 2026 •

Copy link
Copy Markdown

ghcr.io/projectbluefin/dakota:latest is a buildstream based image. We want to just start with chunkah out of the gate, would love to offer a prototype. We at least have a few people on this, and I'm dogfooding on metal! projectbluefin/dakota#212

Happy to turn on whatever is necessary to help!

Comment thread src/packing.rs Resolved
jlebon added 2 commits April 28, 2026 12:27
Switch test-arch.sh and test-self.sh to use --write-manifest-to for
checking the unclaimed component size. The previous approach checked
the layer size via skopeo inspect, which is inaccurate since the
unclaimed component may share a layer with other components.

For test-self.sh, also drop the is_chunked shortcut so that we always
run chunkah and produce a manifest.

Assisted-by: OpenCode (Claude Opus 4.6)
Wrap the processing loop in try/finally so that temporary
localhost/tmp-chunk-src images are always removed, even if
chunking fails or is interrupted mid-run. Previously these
would leak and cause duplicate tag issues in subsequent runs.

Assisted-by: OpenCode (Claude Opus 4.6)
jlebon added 2 commits April 28, 2026 14:05
Add an OpenCode skill that guides benchmarking of packing
algorithm changes. Covers the full pipeline from building
chunkah through analyzing layer reuse, including key pitfalls
like duplicate tags and sample size sensitivity.

Assisted-by: OpenCode (Claude Opus 4.6)
OpenCode respects this also, and so does Pi. Claude, as usual, does not.
@jlebon
jlebon force-pushed the pr/algo-tweaks branch 2 times, most recently from 8d87008 to c3ce41b Compare April 29, 2026 20:59
@jlebon
jlebon marked this pull request as ready for review April 29, 2026 20:59
@jlebon

jlebon commented Apr 29, 2026

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request replaces the existing clustering-based packing algorithm with a two-phase statistical partitioning approach designed to improve layer reuse and ensure deterministic binning. Phase 1 isolates large components as singletons using median absolute deviation, while Phase 2 categorizes remaining items into stability tiers and assigns them to bins via name-based hashing. The PR also updates E2E tests to verify component sizes through generated manifests and adds comprehensive benchmarking documentation. Feedback was provided regarding the use of DefaultHasher, which may not guarantee the intended cross-build stability across different Rust toolchains or platforms.

Comment thread src/packing.rs Resolved
The old algorithm used a greedy merge approach (BinaryHeap-based,
minimizing total expected value loss per merge) which was fundamentally
unstable: small changes in input caused completely different merge
decisions, resulting in poor layer reuse across updates.

Additionally, because of its greediness, it easily fell into a local
optimum with a giant catch-all bucket with low stability and safer more
stable items in the other layers.

Replace it with a two-phase statistical partitioning approach inspired
by rpm-ostree's chunking algorithm:

- Phase 1 classifies components by size, giving large components
  (linux-firmware, kernel, firefox) their own singleton layers.
- Phase 2 assigns all remaining components to bins using stability tiers
  (high/mid/low) and deterministic name-based hashing, which ensures
  stable bin membership across builds without needing to track prior
  build state.

Also remove the stability fallback that assigned min(known)/2 to
components without stability data (xattr, bigfiles, unclaimed). These
now stay at 0.0 and are naturally handled by the stability tiers.

Benchmarked against rpm-ostree's native chunking on FCOS F43 (11
biweekly stable releases) and Silverblue F43 (3 weekly builds):

    FCOS:       46.4% reuse, 916 MiB avg download uncompressed (rpm-ostree: 34.0%, 1.2 GiB)
    Silverblue: 47.9% reuse, 2.8 GiB avg download uncompressed (rpm-ostree: 38.5%, 3.5 GiB)

Assisted-by: OpenCode (Claude Opus 4.6)
@jlebon

jlebon commented Apr 30, 2026

Copy link
Copy Markdown
Member Author

OK, I've tested this extensively now and I'm pretty satisfied with it. I've cleaned up the code a bunch and did some minor additional optimizations.

This notably brings chunkah packing performance for Silverblue to parity with rpm-ostree or above it. With infinite time, it would be cool to geek out on this more and throw more tokens at other approaches, but gains would likely not be as drastic. This is good enough and certainly better than the status quo, so let's ship it.

Some stats from recent runs, keeping 64 layers for parity with rpm-ostree:

FCOS f43 (10 biweekly updates, 64 layers)

Metric Original git main this PR
Avg reuse 34.0% 41.4% 46.4%
Avg download 1,173 MB 1,001 MB 916 MB

Silverblue f43 (10 daily updates, 64 layers)

Metric Original git main this PR
Avg reuse 72.9% 55.8% 72.9%
Avg download 1,535 MB 2,391 MB 1,466 MB

Silverblue f43 (2 weekly updates, 64 layers)

Metric Original git main this PR
Avg reuse 38.5% 35.5% 47.9%
Avg download 3,483 MB 3,485 MB 2,818 MB

(I did also test many other images, but not to the same extent as these.)

@jlebon
jlebon merged commit ba99f6a into coreos:main Apr 30, 2026
10 checks passed
@castrojo

Copy link
Copy Markdown

I'm gathering more metrics on this.

Man dude, this really made a difference in the buildstream builds. This is really going to help bootc adoption!

cc @ahmedadan

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.

4 participants