Skip to content

7z LZMA2 threads: blocks reach idle workers early; parallel budget scales with memory (2x on -mmt archives) - #318

Merged
xbmc4lyfe merged 4 commits into
mainfrom
sevenzip-lzma2-parallel
Nov 27, 2026
Merged

xbmc4lyfe merged 4 commits into
mainfrom
sevenzip-lzma2-parallel

Conversation

@xbmc4lyfe

Copy link
Copy Markdown
Collaborator

Prybar's benchmark found that extract::run on a 7-Zip -mmt LZMA2 archive ran at about 1.8 cores and that --threads made no difference. zipfast already decodes dictionary-reset blocks in parallel (#235). This PR removes the two things that kept those threads idle.

What limited it

A per-piece trace of the pipeline on the x86 set (20 blocks of 64 MiB) showed:

  1. Budget. lzma2_parallel_bytes defaulted to 256 MiB. That holds about 1.5 decoded 64 MiB blocks waiting for in-order write-out, so only about 2 blocks were ever in flight.
  2. Read-ahead. To reach block k+1 the reader has to walk all of block k's packed bytes. That walk was capped by the output handed out so far, so the second block was found only half way through the first. Stored (random-data) pieces were also read 256 KiB at a time, so the text blocks behind them reached the workers about a second late.

Change

  • SevenZipLimits::lzma2_parallel_bytes now defaults to an eighth of physical memory, clamped to 256 MiB–2 GiB. It is read with sysconf on Linux, macOS and FreeBSD; elsewhere it is 1 GiB. It is still a cap, not an allocation: memory is taken only as blocks decode ahead of the one being written.
  • Parallel::room: while a worker is idle (more idle workers than queued pieces), only the budget bounds read-ahead. Before the workers start, the lookahead may reach four dictionary windows (one 7-Zip block), so the second piece is found while the first is still decoding. A one-block stream still reads little ahead.
  • Unchanged: output order, folder and member CRCs, the error placement of a damaged block (the first failing piece in order), the 1-thread path, and the encrypted and non-LZMA2 fallbacks. Filters after LZMA2 (BCJ, Delta, …) still run sequentially over the reassembled stream.

Benchmarks

Data: 1.28 GB mixed (6 × 100 MB text, 6 × 100 MB random, 40 × 2 MB short-run data), 7z a -m0=lzma2 -mx5 -mmt. Extracted with zipfast x -mtN to local disk; wall-clock seconds.

x86, rustyget-x86-a (Xeon Platinum 8581C, 32 vCPU, Linux). Archive from 7z 23.01 -mmt=32: 20 blocks of 64 MiB. Median of 3. The host was shared, with load 5–88 during the runs, but spread within a row stayed under 3%.

threads before (main) after 7zz 26.06
1 10.58 10.42 16.00
2 4.78 4.86 7.33
4 4.43 3.55 5.59
8 4.82 2.42 4.65
32 4.89 2.16 3.83

An earlier quieter run on the same host gave mt8 5.47 → 2.65 and mt32 5.50 → 2.27, with 7zz mt32 at 4.10.

Apple M5 (this Mac, 10 cores). Archive from 7zz 26.06 -mmt=10: 10 blocks of 128 MiB. The host was loaded (load 5–9), so these are the median of 5 interleaved rounds:

threads before (main) after 7zz 26.06
1 7.04 7.42 4.99
2 4.33 4.43 3.42
4 4.37 2.42 2.49
8 4.46 2.48 2.17
10 4.11 2.61 2.00
  • One thread runs the same code as before (LzmaReader); the 1-thread differences are noise.
  • Two threads are a tie: the old budget already fit two workers.
  • On Apple Silicon, 7zz's single-thread decoder is faster than liblzma, which sets the remaining gap.
  • With many threads the critical path is now the slowest single block. In this set that is the LZMA-coded first block of short-run data, about 1.6 s on x86.
  • Peak RSS on x86 at mt8 is about 1.15 GB, against 2.1 GB for 7zz at mt32.

Tests

  • tests/sevenzip_lzma2_threads.rs:
    • Delta and x86 BCJ after a multi-block LZMA2 coder (Delta checked against the original data; BCJ output identical to 1 thread, including a damaged control byte in a middle block).
    • Shuffled block layouts over 4 seeds (stored and LZMA blocks of random sizes, random member cuts, whole and with one damaged block), at 2/3/4/8 threads and several budgets, compared with 1 thread.
    • Bounds on the default budget.
  • lzma2_par unit test idle_workers_get_the_pieces_after_stored_ones_early: every piece reaches a thread before the first is handed out. It fails on main (4 of 7).
  • The existing lzma2_par unit tests (memory bounds, cancellation, panics, damage) pass. fmt and both clippy sets are clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LG44gZ63jJDJGeNcamsGrj

xbmc4lyfe and others added 2 commits November 26, 2026 17:11
…udget scales with memory

The 7z extractor already decoded LZMA2 dictionary-reset blocks on several threads (#235), but on
7-Zip -mmt archives with 64-128 MiB blocks it ran at about 1.8 cores whatever the thread count:

- The 256 MiB budget held about one and a half 64 MiB blocks in flight. The default is now an
  eighth of physical memory, 256 MiB to 2 GiB (1 GiB where memory can't be read).
- Reading ahead to find the next block was bounded by the output so far, so the second block was
  found half way through the first, and stored (random-data) blocks were read 256 KiB at a time,
  so LZMA blocks after them reached the workers late. Now, while a worker waits with no piece,
  only the budget bounds reading ahead; before the workers start the lookahead may reach four
  dictionary windows (one 7-Zip block).

Output order, CRC checks and error placement are unchanged. Tests: BCJ and Delta after a
multi-block LZMA2 coder, damage in the middle, shuffled block layouts, idle-worker read-ahead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LG44gZ63jJDJGeNcamsGrj
@xbmc4lyfe

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T01:51:43.285377Z 6a47b24 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 24486cb00f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@xbmc4lyfe

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: e96c3eaa96

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@xbmc4lyfe

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 6a47b24dbf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6a47b24dbf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/sevenzip.rs
Comment on lines +132 to +135
let (pages, page) = unsafe { (libc::sysconf(libc::_SC_PHYS_PAGES), libc::sysconf(libc::_SC_PAGESIZE)) };
let pages = u64::try_from(pages).ok().filter(|&n| n > 0)?;
let page = u64::try_from(page).ok().filter(|&n| n > 0)?;
pages.checked_mul(page)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Account for the container memory limit when sizing the budget

On Linux containers with a memory limit below the host's RAM, _SC_PHYS_PAGES reports host memory rather than the cgroup allowance. For example, a 512 MiB container on a host with at least 16 GiB now gets a 2 GiB parallel budget instead of the previous 256 MiB. This can cause an OOM kill during multi-block extraction: lzma2_par::plan permits additional workers and decoded blocks accumulate behind the head up to this budget. The normal extraction paths in src/extract.rs construct SevenZipLimits::default() directly, so they inherit this regression. Bound the detected memory by the effective cgroup memory limit before calculating the default.

Useful? React with 👍 / 👎.

@xbmc4lyfe

Copy link
Copy Markdown
Collaborator Author

Local CI (.claude/skills/ci-local/run.sh --host rustyget-x86-a, full): fmt, clippy (plain and persistent-partials), workspace tests (plain 292-331 s, persistent-partials 368-411 s), doctests (both). ALL GATES PASSED @ 6a47b24. Codex review of 6a47b24: no findings. Branch up to date with origin/main.

@xbmc4lyfe
xbmc4lyfe merged commit bb6bbb1 into main Nov 27, 2026
5 of 7 checks passed
@xbmc4lyfe
xbmc4lyfe deleted the sevenzip-lzma2-parallel branch November 27, 2026 02:03
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.

1 participant