Skip to content

7z LZMA2: the default thread count starts only the workers the budget gives room (fixes slower-than--mt16 on 32 cores) - #323

Open
xbmc4lyfe wants to merge 2 commits into
mainfrom
sevenzip-default-threads
Open

xbmc4lyfe wants to merge 2 commits into
mainfrom
sevenzip-default-threads

Conversation

@xbmc4lyfe

@xbmc4lyfe xbmc4lyfe commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Prybar reported that after #318, extracting a 7z made of LZMA2 blocks at the default thread count was much slower than -mt8 on a 32-core x86 host (15.7 s against 5.5 s; 7zz took 8.0 s).

Root cause

With threads: None the 7z run uses one thread per core, so it asks for 32 LZMA2 block workers. lzma2_par::plan limited the workers only by their own cost (a third of the 2 GiB budget, which pays for 38 at -mx5), so all 32 started. That left 1.5 GiB in flight, about 47 MiB per worker, which is less than one 64 MiB block. Idle workers make read-ahead bounded by the budget alone (#318), so the reader drew later blocks' packed bytes into the budget until it was full. The blocks that had already started then had no room to hand out their output, and the run fell back to decoding about one block at a time.

This only shows on archives whose packed bytes plus in-flight output pass the budget at full width: many blocks, data that compresses poorly. Mixed data that packs well (1.2 GB to 405 MB) and a 17-block set ran the same at every width.

Fix

plan now also limits the workers so that each one keeps 1.5 7-Zip blocks of the budget in flight (a block is four dictionary windows, and never less than MIN_PIECE). At least one worker still starts whenever its cost fits. A block's packed bytes leave the budget as its decoder consumes them, so a piece's waiting input plus its output so far stays near one block, and the extra half block is headroom. With the default 2 GiB this means at most 18 workers at -mx5 and 4 at -mx9. The limit applies to explicit -mtN too, which made -mt32 fast again. Fewer threads than the limit behave as before.

I tried a limit of two blocks per worker first (14 workers). It was measurably slower on the 2 GB set (7.2 s against 5.8–6.1 s at 16–24 workers), because 33 pieces then take three waves instead of two.

Tests:

  • New unit test plan_leaves_each_worker_room_for_its_block. It checks 32 cores with 2 GiB at -mx5 (18 workers) and at -mx9 (4), the threads-asked case, and the small-budget cases. It also sweeps threads, budgets and windows to check that each started worker has at least 1.5 blocks in flight and that the workers' cost stays within a third of the budget.
  • The existing tests pass: tests/sevenzip_lzma2_threads.rs, sevenzip_extract, sevenzip_print, sevenzip_method_names and the lzma2_par unit tests.

Updated docs/7z-extract.md and the SevenZipLimits::lzma2_parallel_bytes doc.

Bench

Wall time in seconds for zipfast x -y -q [-mtN] to local disk, median of 3. Before and after runs were interleaved. The 7z column is 7-Zip 23.01 7z x at its default threads.

x86, rustyget-x86-c (Xeon Platinum 8581C, 32 vCPU, 62 GB, so a 2 GiB budget). Another user kept the load average at 5–10 during the runs. Archives made with 7z a -m0=lzma2 -mmt:

  • mixed: 1.21 GB → 405 MB, -mx5 (text, binaries, 220 MB random)
  • hard: 1.15 GB base64 of random bytes → 920 MB, -mx5, 17 blocks
  • hard2: 2.10 GB base64 of random bytes → 1.69 GB, -mx5, 33 blocks (the regression)
  • mixed9: the mixed input at -mx9
set threads before after 7z
hard2 default 9.04 6.02 7.42
hard2 8 9.69 9.73
hard2 16 5.98 5.97
hard2 32 8.58 5.75
hard default 3.64 3.62 5.33
hard 8 6.63 6.59
hard 16 4.42 4.44
hard 32 3.67 3.64
mixed default 2.03 2.02 3.06
mixed 1 9.20 9.13
mixed 2 4.43 4.39
mixed 4 2.66 2.68
mixed 8 2.00 2.00
mixed 16 2.01 2.00
mixed 32 2.00 2.00
mixed9 default 5.59 5.68 5.64
mixed9 4 5.63 5.63
mixed9 8 5.70 5.66
mixed9 16 5.68 5.68
mixed9 32 5.60 5.62

On hard2 the default went from 1.57× the best explicit setting to within 5% of it (5.75 s at -mt32), and it now beats 7z. Nothing else moved.

Before the fix, the hard2 sweep on the same host was: 8 → 9.7, 12 → 7.4, 14 → 7.2, 16 → 5.8–6.2, 20 → 5.9–6.1, 24 → 5.9–6.0, 32 → 8.6–10.4 s.

I could not reproduce the slowdown on the mixed set: on quiet rustyget-x86-b the default took 1.67 s against 1.65 s at -mt8. It also did not appear under 16 or 32 CPU-hog processes on x86-c.

Apple M4 (10 cores, 32 GB, so a 2 GiB budget). The mixed-style set here is 1.06 GB → 368 MB, made with 7zz a -m0=lzma2 -mx5 -mmt. Median of 5 interleaved runs; other sessions' test runs kept the load average at 6–17, so single runs vary by up to 2×. At 10 threads plan picks the same workers before and after (the limit is 18), so the code path is unchanged and the differences are noise.

threads before after 7zz 26
default 1.78 1.67 1.36
1 4.33 5.45
2 2.17 2.04
4 1.63 1.64
8 1.76 1.88
10 1.80 1.72

🤖 Generated with Claude Code

https://claude.ai/code/session_01LG44gZ63jJDJGeNcamsGrj

…alf of room each

At the default thread count (one per core) a 32-core host started 32 LZMA2
block workers, leaving each 47 MiB of the 2 GiB budget in flight, less than
one 64 MiB -mx5 block. Idle workers drew later blocks' packed bytes into the
budget until the started blocks could not hand out their output, and the run
decoded about one block at a time: slower than -mt16. plan() now also caps
the workers so each keeps 1.5 blocks (six windows) in flight: 18 at -mx5 and
4 at -mx9 with 2 GiB.

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-27T03:41:54.015363Z cef973d 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. Delightful!

Reviewed commit: 048f1913bb

ℹ️ 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".

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

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

Unknown error
ℹ️ 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. Delightful!

Reviewed commit: cef973d8c6

ℹ️ 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".

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