Skip to content

Optimize scalar LZ4 compression and decompression on C906 - #505

Merged
dormancygrace merged 3 commits into
mainfrom
codex/lz4-c906-scalar
Oct 27, 2026
Merged

dormancygrace merged 3 commits into
mainfrom
codex/lz4-c906-scalar

Conversation

@dormancygrace

Copy link
Copy Markdown
Owner

C906 spends avoidable instructions in LZ4 byte counting and match copies. Use byte reversal plus CLZ in the compressor, initialize the decoder overlap buffer only for offsets below eight, and remove the old aligned-copy dispatch and its kernel option. Public APIs, bounds checks and compressed output are preserved.

Measured with the actual kernel code on C906, using 1,413 identical 4 KiB pages and matched code placement:

Change Median cycles Six comparison range Control drift
Compressor -1.33% -1.47% to -1.23% at most 0.21%
Decoder -1.93% -2.00% to -1.89% at most 0.26%

These are codec measurements, not end-to-end zram gains. Decoder inputs were produced by the exact compressor in #503. Each comparison used bracketing baselines, three rounds and two layouts; decoder candidate order was rotated. All device runs passed, with unchanged output.

Validation: normal and ASan/UBSan compressor, small-table and decoder contracts; guarded buffers and malformed input; overlap distances, partial decoding, dictionaries, safe/fast streaming and in-place decompression. A negative control confirms that the decoder test detects missing zero-offset initialization. Final C906 machine code matches the measured prototypes; patch application, platform tests and kernel configuration checks pass. The decoder contract now runs against the actual patched kernel source in firmware CI.

Stacked on #503; compressor update #501 and table-size change #503 remain separate.

@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Build outputs

Compared with the build of main at 2571baa; codex/lz4-zram-table4k is at ccdfe3e, so outputs changed since then are listed too.

17 changed, 0 added, 0 removed of 570 outputs.

directory files
changed . Image
changed boot boot/alpha.sha256
boot/beta.sha256
boot/detect.sha256
boot/fit-template.sd
boot/fit.layout
boot/lite.sha256
boot/pcie.sha256
changed boot-fit boot-fit/alpha.sd
boot-fit/beta.sd
boot-fit/detect.sd
boot-fit/lite.sd
boot-fit/pcie.sd
changed lib lib/modules/7.3.0-nanokvm-os-r3/kernel/drivers/block/zram/zram.ko
lib/modules/7.3.0-nanokvm-os-r3/kernel/lib/lz4/lz4_compress.ko
lib/modules/7.3.0-nanokvm-os-r3/modules.symbols
lib/modules/7.3.0-nanokvm-os-r3/modules.symbols.bin
Hashes: outputs-sha256 artifact of this run.

@dormancygrace dormancygrace left a comment •

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The new code is correct.

  • Differential: I compared the old and new decoders on 309,540 constructed cases (offsets 0–16, match lengths 4–70, 0–20 trailing literals, with the safe, fast and partial APIs) and on 300,000 bit-flipped or truncated streams under ASan. The results were identical.
  • bswap+clz: it is only reached with a non-zero diff, and the kernel build always enables XTheadBb.

Removing 0039 is fine with the kernel built with -mno-strict-align (see the corrected inline comment); only the bench docs still point at it.

Comment thread platform/kernel/config
Comment thread platform/kernel/0056-lz4-c906-scalar-paths.patch
Comment thread tests/lz4-compressor/decoder-contract.c Outdated

@dormancygrace dormancygrace left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Review completed at 5f35ef5. No new production-code defect found in the scalar byte-count or overlap-copy changes. Expanded near-end and malformed-block tests pass with the actual patched Linux 7.3 decoder, normally and under ASan/UBSan; compressor and small-table contracts pass in both modes. Full LZ4 patch application, platform configuration and C906 flag checks pass. Historical benchmark documentation and the full/partial decode comment are corrected. This branch includes the substantive #501/#503 workspace corrections and remains open with its dependencies.

Base automatically changed from codex/lz4-zram-table4k to main October 27, 2026 00:22

@dormancygrace dormancygrace left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Final review at 1046bfc: scalar LZ4 paths are now patch 0056, following compressor 0054 and small-table 0055. All test paths and the current benchmark README reference are updated; the patch contents are unchanged. Both decoder and C906 memory/usercopy documentation sections are preserved when integrating current main. Clean Linux 7.3 patch/Kconfig, compressor and small-table contracts, and the actual patched decoder in normal and ASan/UBSan modes pass. The prepared main merge tree exactly matches the validated head. No unresolved findings.

@dormancygrace
dormancygrace merged commit fe2e25d into main Oct 27, 2026
15 checks passed
@dormancygrace
dormancygrace deleted the codex/lz4-c906-scalar branch October 27, 2026 00:23
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