Skip to content

workflows/release-binaries: Fix zstd compression - #219587

Merged
keith merged 4 commits into
llvm:mainfrom
keith:ks/workflows-release-binaries-fix-zstd-compression
Sep 8, 2026
Merged

keith merged 4 commits into
llvm:mainfrom
keith:ks/workflows-release-binaries-fix-zstd-compression

Conversation

@keith

@keith keith commented Aug 28, 2026

Copy link
Copy Markdown
Member

23.1.0 was the first e2e release with zstd archives. I noticed that once
it shipped they didn't have a meaningful size improvement over the
pre-existing archives. Turns out this is because by default zstd
compresses over a 128MB window which wasn't large enough to get
meaningful savings. We now use 1GB for this which uses more memory but
should take the archive from the 1.8GB that the xz release is, to ~1gb.
I played around with a few options and this was the best time vs
compress vs memory usage ratio.

Assisted-By: codex

23.1.0 was the first e2e release with zstd archives. I noticed that once
it shipped they didn't have a meaningful size improvement over the
pre-existing archives. Turns out this is because by default zstd
compresses over a 128MB window which wasn't large enough to get
meaningful savings. We now use 1GB for this which uses more memory but
should take the archive from the 1.8GB that the xz release is, to ~1gb.
I played around with a few options and this was the best time vs
compress vs memory usage ratio.

Assisted-By: codex
@keith

keith commented Aug 28, 2026

Copy link
Copy Markdown
Member Author

cc @tstellar @tru

@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-github-workflow

Author: Keith Smiley (keith)

Changes

23.1.0 was the first e2e release with zstd archives. I noticed that once
it shipped they didn't have a meaningful size improvement over the
pre-existing archives. Turns out this is because by default zstd
compresses over a 128MB window which wasn't large enough to get
meaningful savings. We now use 1GB for this which uses more memory but
should take the archive from the 1.8GB that the xz release is, to ~1gb.
I played around with a few options and this was the best time vs
compress vs memory usage ratio.

Assisted-By: codex


Full diff: https://github.com/llvm/llvm-project/pull/219587.diff

2 Files Affected:

  • (modified) .github/workflows/release-binaries.yml (+22-11)
  • (added) llvm/utils/release/repack-zstd.py (+97)
diff --git a/.github/workflows/release-binaries.yml b/.github/workflows/release-binaries.yml
index c6fd531da39b0..279ca19ffd03e 100644
--- a/.github/workflows/release-binaries.yml
+++ b/.github/workflows/release-binaries.yml
@@ -256,12 +256,12 @@ jobs:
       id: setup-stage
       shell: bash
       run: |
-        build_prefix=`pwd`
-        if [ "${{ runner.os }}" = "Linux" ]; then
-          sudo chown $USER:$USER /mnt/
+        build_prefix="$GITHUB_WORKSPACE"
+        if [ "$RUNNER_OS" = "Linux" ]; then
+          sudo chown "$USER:$USER" /mnt/
           build_prefix=/mnt/
         fi
-        echo "build-prefix=$build_prefix" >> $GITHUB_OUTPUT
+        echo "build-prefix=$build_prefix" >> "$GITHUB_OUTPUT"
 
     - name: Configure Linux/MacOS
       id: build
@@ -325,14 +325,25 @@ jobs:
       run: |
         cat S:/llvm/utils/release/llvm_package_$($env:LLVM_VERSION)/build_$($env:BUILD_DIR_SUFFIX)/_CPack_Packages/win64/WIX/wix.log
 
+    # The release ref may predate the compression utility, so check it out
+    # from the same revision as this workflow.
+    - name: Checkout release compression utility
+      uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
+      with:
+        repository: ${{ job.workflow_repository }}
+        ref: ${{ job.workflow_sha }}
+        persist-credentials: false
+        path: release-compression
+        sparse-checkout: llvm/utils/release/repack-zstd.py
+        sparse-checkout-cone-mode: false
+
     - name: Create zstd compressed tarball
-      shell: bash
-      env:
-        RELEASE_BINARY_FILENAME: ${{ needs.prepare.outputs.release-binary-filename }}
-        RELEASE_BINARY_FILENAME_ZSTD: ${{ needs.prepare.outputs.release-binary-filename-zstd }}
-        ZSTD: ${{ inputs.runs-on == 'windows-11-arm' && './zstd.exe' || 'zstd' }}
-      run: |
-        xz -dc "$RELEASE_BINARY_FILENAME" | "$ZSTD" --ultra -22 -T0 -o "$RELEASE_BINARY_FILENAME_ZSTD"
+      run: >-
+        python release-compression/llvm/utils/release/repack-zstd.py
+        --input "${{ needs.prepare.outputs.release-binary-filename }}"
+        --output "${{ needs.prepare.outputs.release-binary-filename-zstd }}"
+        --work-dir "${{ steps.setup-stage.outputs.build-prefix }}"
+        --zstd "${{ inputs.runs-on == 'windows-11-arm' && './zstd.exe' || 'zstd' }}"
 
     - name: Generate sha256 digest for binaries
       id: digest
diff --git a/llvm/utils/release/repack-zstd.py b/llvm/utils/release/repack-zstd.py
new file mode 100644
index 0000000000000..7d84ed2c60c3e
--- /dev/null
+++ b/llvm/utils/release/repack-zstd.py
@@ -0,0 +1,97 @@
+#!/usr/bin/env python3
+# ===-- repack-zstd.py ------------------------------------------------------===#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ===----------------------------------------------------------------------===#
+
+import argparse
+from pathlib import Path
+import subprocess
+import sys
+import tarfile
+import tempfile
+
+ZSTD_COMPRESSION_LEVEL = 21
+ZSTD_WINDOW_LOG = 30
+ZSTD_JOB_SIZE = "1024M"
+
+
+def normalize_metadata(info: tarfile.TarInfo) -> tarfile.TarInfo:
+    info.uid = info.gid = 0
+    info.uname = info.gname = "root"
+    return info
+
+
+def repack(input_path: Path, output_path: Path, work_dir: Path, zstd: str) -> None:
+    if not input_path.name.endswith(".tar.xz"):
+        raise ValueError(f"input filename does not end in .tar.xz: {input_path}")
+
+    package_name = input_path.name.removesuffix(".tar.xz")
+    # Six workers kept the ARM64 release benchmark under 20 minutes. macOS
+    # uses fewer workers because its release runners have less memory.
+    thread_count = 4 if sys.platform == "darwin" else 6
+
+    with tempfile.TemporaryDirectory(prefix="zstd-repack-", dir=work_dir) as temp:
+        temp_dir = Path(temp)
+        extract_dir = temp_dir / "contents"
+        extract_dir.mkdir()
+
+        with tarfile.open(input_path, "r:xz") as archive:
+            archive.extractall(extract_dir, filter="data")
+
+        package_dir = extract_dir / package_name
+        if not package_dir.is_dir():
+            raise ValueError(f"archive does not contain {package_name}/")
+
+        uncompressed_tar = temp_dir / f"{package_name}.tar"
+        with tarfile.open(
+            uncompressed_tar, "w", format=tarfile.GNU_FORMAT, dereference=False
+        ) as archive:
+            # TarFile.add() visits directory contents in sorted order.
+            archive.add(
+                package_dir,
+                arcname=package_name,
+                filter=normalize_metadata,
+            )
+
+        subprocess.run(
+            [
+                zstd,
+                "--ultra",
+                f"-{ZSTD_COMPRESSION_LEVEL}",
+                # A 1 GiB window captures duplicate code across the large,
+                # statically linked tools. Match the job size to the window.
+                f"--long={ZSTD_WINDOW_LOG}",
+                f"-T{thread_count}",
+                f"-B{ZSTD_JOB_SIZE}",
+                str(uncompressed_tar),
+                "-o",
+                str(output_path),
+            ],
+            check=True,
+        )
+
+
+def main() -> None:
+    parser = argparse.ArgumentParser(
+        description="Repack a release tar.xz as a name-sorted tar.zst"
+    )
+    parser.add_argument("--input", type=Path, required=True)
+    parser.add_argument("--output", type=Path, required=True)
+    parser.add_argument("--work-dir", type=Path, required=True)
+    parser.add_argument("--zstd", default="zstd")
+    args = parser.parse_args()
+
+    repack(
+        args.input.resolve(),
+        args.output.resolve(),
+        args.work_dir.resolve(),
+        args.zstd,
+    )
+
+
+if __name__ == "__main__":
+    main()

Comment thread .github/workflows/release-binaries.yml Resolved
Comment thread .github/workflows/release-binaries.yml Resolved
Comment thread .github/workflows/release-binaries.yml Resolved
@keith

keith commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

in the end the window is really the most important part. we could save another 30mb or so by re-compressing from an entirely unarchived directory but i don't think it's worth it for the maintenance of the scripts

@keith
keith requested a review from tstellar September 1, 2026 16:06
@keith

keith commented Sep 5, 2026

Copy link
Copy Markdown
Member Author
1.9G	LLVM-23.1.0-Linux-X64.tar.xz
1.9G	old.zstd
1.1G	new.zstd

I think we're good here, but the windows job timed out, it looks like the job is on the limit regardless of this change? what's the rec? should we bump that timeout? 😬

@tstellar tstellar 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.

LGTM. We can't bump the timeout for that job. I've been working on other ways to speed it up.

@keith
keith merged commit 9f51fa4 into llvm:main Sep 8, 2026
31 of 32 checks passed
@keith
keith deleted the ks/workflows-release-binaries-fix-zstd-compression branch September 8, 2026 18:25
@keith

keith commented Sep 8, 2026

Copy link
Copy Markdown
Member Author

/cherry-pick 9f51fa4

@llvmbot

llvmbot commented Sep 8, 2026

Copy link
Copy Markdown
Member

/cherry-pick 9f51fa4

Error: Command failed due to missing milestone.

@keith

keith commented Sep 8, 2026

Copy link
Copy Markdown
Member Author

/cherry-pick 9f51fa4

@github-project-automation github-project-automation Bot moved this from Needs Triage to Done in LLVM Release Status Sep 8, 2026
@llvmbot

llvmbot commented Sep 8, 2026

Copy link
Copy Markdown
Member

/pull-request #222172

aobolensk pushed a commit to aobolensk/llvm-project that referenced this pull request Sep 15, 2026
23.1.0 was the first e2e release with zstd archives. I noticed that once
it shipped they didn't have a meaningful size improvement over the
pre-existing archives. Turns out this is because by default zstd
compresses over a 128MB window which wasn't large enough to get
meaningful savings. We now use 1GB for this which uses more memory but
should take the archive from the 1.8GB that the xz release is, to ~1gb.
I played around with a few options and this was the best time vs
compress vs memory usage ratio.

Assisted-By: codex
dyung pushed a commit to llvmbot/llvm-project that referenced this pull request Sep 19, 2026
23.1.0 was the first e2e release with zstd archives. I noticed that once
it shipped they didn't have a meaningful size improvement over the
pre-existing archives. Turns out this is because by default zstd
compresses over a 128MB window which wasn't large enough to get
meaningful savings. We now use 1GB for this which uses more memory but
should take the archive from the 1.8GB that the xz release is, to ~1gb.
I played around with a few options and this was the best time vs
compress vs memory usage ratio.

Assisted-By: codex
(cherry picked from commit 9f51fa4)
johejo added a commit to johejo/bigquery-emulator-duckdb that referenced this pull request Sep 27, 2026
The official LLVM 23.1.0-23.1.2 Linux lld links ICU 70 dynamically and
only runs on Ubuntu 22.04 (llvm/llvm-project#215764), so link with the
host ld on Linux as on macOS.

Stay on 23.1.1 so toolchains_llvm's default .tar.zst archives work: from
23.1.2 (llvm/llvm-project#219587) they use a 1 GiB window, beyond Bazel's
128 MiB zstd limit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
johejo added a commit to johejo/bigquery-emulator-duckdb that referenced this pull request Sep 27, 2026
The official LLVM 23.1.0-23.1.2 Linux lld links ICU 70 dynamically and
only runs on Ubuntu 22.04 (llvm/llvm-project#215764), so link with the
host ld on Linux as on macOS.

Stay on 23.1.1 so toolchains_llvm's default .tar.zst archives work: from
23.1.2 (llvm/llvm-project#219587) they use a 1 GiB window, beyond Bazel's
128 MiB zstd limit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

4 participants