Skip to content

Use 16-bit symbol entries in the rANS 4x8 order-1 decoder - #163

Merged
jkbonfield merged 1 commit into
samtools:masterfrom
BenjaminDEMAILLE:perf/rans4x8-o1-decode
Dec 1, 2026
Merged

jkbonfield merged 1 commit into
samtools:masterfrom
BenjaminDEMAILLE:perf/rans4x8-o1-decode

Conversation

@BenjaminDEMAILLE

@BenjaminDEMAILLE BenjaminDEMAILLE commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

I noticed that rans_uncompress_O1 stores start/freq as two uint32_t per (context, symbol), but both always fit in 16 bits there (freq <= 4096 after the totals check, start < 4096). I switched it to the existing RansDecSymbol. That halves the table, and the scratch buffer goes from 1.5 MB to 1.25 MB. It's a 5-line change.

I compared full decodes with master, and ran a differential fuzz against master (27k mutated order-1 streams, under ASan): same results, no reports. make check passes.

Order-1 decode, one thread on my M4 Max: Illumina qualities went from 584 to 615 MB/s and HiFi from 339 to 377 MB/s; q4 was flat. I haven't measured it on x86-64.

It was prompted by pasta (https://github.com/DylanCosto/pasta, MIT).

I've listed this with my other performance PRs in samtools/htslib#2175.

Assisted-by: Claude:claude-opus-5-5

@jkbonfield

Copy link
Copy Markdown
Collaborator

I tried it on x86-64 with gcc (11.4, so not current) and a test of Illumina NovaSeq quals (4 binning) showed master at 220MB/s decode and this PR at 198MB/s decode. So it's a 10% performance hit.

With gcc16 on the same system, I see 302MB/s and 281MB/s
With clang21 it's 311MB/s and 283MB/s

So it all 3 cases master outperforms this PR. /proc/cpu claims "Intel Core Processor (Broadwell, IBRS)". (It's virtual)

On a bare-metal Intel(R) Xeon(R) Platinum 8468 with gcc16 and clang21 I see:
gcc16: master 546MB/s, PR 533MB/s
clang21: master 545MB/s, PR 541MB/s

On AMD EPYC 7713 64-Core
gcc16: master 304MB/s, PR 300MB/s
clang21: master 350MB/s, PR 344MB/s

I'm guessing this is an x86 vs ARM difference, perhaps related to L1 cache sizes?
I don't really see why it would be slower on x86, but haven't time currently to look into the assembly produced.

@BenjaminDEMAILLE

BenjaminDEMAILLE commented Nov 26, 2026 via email

Copy link
Copy Markdown
Contributor Author

@jkbonfield

Copy link
Copy Markdown
Collaborator

I tried my q40b (old Illumina 40 distinct quals) file and it is marginally faster there. So once we start using more of the arrays then the smaller sizes matter, slightly. That was on the original VM machine that showed the biggest performance hit on the q4b file, but it was only from 258MB/s (master) to 260MB/s (PR) for clang21, and 235MB/s (master) to 240MB/s (PR) for gcc16.

PS. Please also rebase as the MinGW CI config has been fixed so the tests pass

start and freq always fit in 16 bits here.  On AArch64, using
RansDecSymbol instead of RansDecSymbol32 halves the per-context table
and makes order-1 decode 5-11% faster on Illumina/HiFi qualities.

On x86 the 16-bit entries are slower (up to 10% on 4-binned qualities):
imul/sub can no longer take the table entry as a memory operand, so each
field needs a separate movzwl.  Other platforms keep RansDecSymbol32, and
their generated code is unchanged.

Idea from pasta (https://github.com/DylanCosto/pasta, MIT).

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Benjamin Demaille <benjamin.demaille@icloud.com>
@BenjaminDEMAILLE

Copy link
Copy Markdown
Contributor Author

Thanks for testing on so many machines. I looked at the x86-64 code (clang -O2) for the order-1 inner loop, and it explains what you're seeing.

With RansDecSymbol32, x86 folds the table loads straight into the arithmetic: imull (%r12,%r9,8), %r10d and subl 4(%r12,%r9,8), %r8d. There's no 16-bit memory form that zero-extends, so with RansDecSymbol each field needs its own movzwl first. That's 8 extra loads per iteration, and the higher register pressure adds 3 more spills/reloads: the loop goes from 57 to 70 instructions. That's presumably also why the comment above RansDecSymbol says the 32-bit version was faster. On AArch64 the loads are separate instructions either way, so the only effect there is the smaller table, which helps once many symbols are in use.

So I've made the 16-bit entries AArch64-only (#if defined(__aarch64__)), and every other platform keeps RansDecSymbol32. The x86-64 assembly is now identical to master, and the arm64 assembly is identical to the previous version of this PR. It's rebased onto master for the MinGW fix, and make check passes.

If you'd rather not carry a platform-specific path for ~5–10% on ARM with many-symbol data, I'm also fine with closing this.

@jkbonfield

Copy link
Copy Markdown
Collaborator

Thanks. That's pretty much how I hoped you'd resolve it, with a typedef.

@jkbonfield
jkbonfield merged commit 19f83c8 into samtools:master Dec 1, 2026
3 checks passed
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.

2 participants