Skip to content

riscv: use the prefetch decoder for large frames on small-L2 cores - #5078

Open
ihb2032 wants to merge 1 commit into
meta:devfrom
ihb2032:riscv-prefetch-gate
Open

ihb2032 wants to merge 1 commit into
meta:devfrom
ihb2032:riscv-prefetch-gate

Conversation

@ihb2032

@ihb2032 ihb2032 commented Dec 1, 2026 •

Copy link
Copy Markdown

What and why

This PR adds an RISC-V-specific frame-size gate for zstd's existing long (prefetch) decoder, and deepens its sequence ring when the build targets Zicbop.

Upstream only selects ZSTD_decompressSequencesLong for cold dictionaries or when a large share of the offsets is long. On RV64 cores with a small shared L2, a large frame's output stream and match source compete for the cache, so the declared frame content size is a useful additional signal. The gate uses the frame header's content size: a known size of at least ZSTD_RISCV_PREFETCH_MIN_FRAME (default 4 MiB, overrideable at build time) selects the long decoder; unknown sizes keep the upstream heuristics. The build-time override is documented in lib/README.md. With Zicbop, PREFETCH_* emits real prefetch instructions and the sequence ring grows from 8 to 256 entries; without Zicbop the code compiles to the upstream behavior. Only decoder selection changes; compressed output is unchanged.

Related work: #4557/#5053/#4733 already generalize the prevOffset hoisting (the RISC-V numbers can be added there; this PR does not duplicate it); #4643 extends PREFETCH_L1(match) in the decoder; #5067 tunes the decode copy/prefetch; #1411 introduced the cold-dictionary path of the long decoder; #3828 prefetches op/litPtr in the sequence loop. No open PR covers a frame-size gate/deep ring for small-L2 RISC-V + Zicbop.

Performance (decoder-only; frames produced by unmodified upstream dev at e50cdb0d4; SG2044, 64x RV64GCV, 2 MB L2 per 4 cores; GCC 15.1; flags -O3 -march=rv64gcv_zbb_zba_zicbop; single core taskset -c 8; 5 interleaved rounds, medians; no checksum; real Chinese public-domain text):

L19:

corpus upstream+Zicbop this PR ratio
3.59 MB 212.1 201.3 0.95x
6.52 MB 198.9 227.7 1.14x
36 MB 140.6 207.9 1.48x
100 MB 124.5 202.3 1.62x

L9:

corpus upstream+Zicbop this PR ratio
3.59 MB 224.6 223.6 1.00x
6.52 MB 213.9 240.6 1.12x
36 MB 154.1 218.9 1.42x
100 MB 143.0 207.3 1.45x

L1/L3 are 0.94-1.01x and 1.03-1.23x respectively. Frames below the 4 MB gate keep the short decoder; on the 3.59 MB corpus lowering the gate to 2 MB gives +9.8% (L9) and +12.1% (L19) at -4.6% (L1), which is the main review question for the default threshold.

The patch also adds a unit test that decodes frames above and below the gate, plus a frame with unknown content size.

Checklist

  • I searched open pull requests and issues: all 174 open PRs were
    reviewed; related work is listed above, and no PR covers this frame-size
    gate.
  • Bug fix: N/A, this is a performance change.
  • Performance change: numbers above come from repeated interleaved runs,
    with CPU, compiler, flags and data stated.
  • I stated below whether AI or other automated tools were used.

Tools used

AI-assisted tools were used for analysis, implementation and benchmark scripting. All measurements, code and conclusions were reviewed by the author.

@meta-cla meta-cla Bot added the CLA Signed label Dec 1, 2026
@ihb2032
ihb2032 force-pushed the riscv-prefetch-gate branch 2 times, most recently from 47ae715 to 06abc24 Compare December 3, 2026 01:50
Co-authored-by: lyd1992 <liuyudong@iscas.ac.cn>
Co-authored-by: YuanSheng <yuansheng@isrc.iscas.ac.cn>
@ihb2032
ihb2032 force-pushed the riscv-prefetch-gate branch from 06abc24 to 4ae304f Compare December 4, 2026 03:46

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant