8000 perf: right-size radix-sort key widths (coordinate & chain-score sorts) by nh13 · Pull Request #46 · nh13/minibwa · GitHub
[go: up one dir, main page]

Skip to content

perf: right-size radix-sort key widths (coordinate & chain-score sorts) - #46

Draft
nh13 wants to merge 1 commit into
masterfrom
perf/radix-key-width
Draft

nh13 wants to merge 1 commit into
masterfrom
perf/radix-key-width

Conversation

@nh13
@nh13 nh13 commented Sep 7, 2026
Copy link
Copy Markdown
Owner

Downstream identical-class perf feature. Not for upstream submission.

What

Every KRADIX_SORT_INIT in the tree declares an 8-byte key, but klib radix does one 8-bit pass per key byte, so narrow keys pay for passes over always-zero high bytes. This declares the true width where the bound is structural (not workload-dependent):

  • Genome coordinates → 5-byte key: seed SA/BWT positions (key_sai0), anchor target positions (key_anchor), and the lchain/pe target-coordinate sorts. Bounded by index length (< 2^40 ≈ references up to ~1 Tbp).
  • Chain score → 4-byte key: mb_chain_backtrack sorts on f[i], a non-negative int32 in a uint64 → top 32 bits structurally zero.

The score<<32 sorts (mb_gen_hit, mb_hit_sort, mate rescue) are left at 8 bytes on purpose — their high-byte occupancy depends on read length, not a structural bound.

Correctness

Byte-identical SAM vs stock f0e1174 across PE default, -u, -b MD, -b cs, single-end, and Hi-C (-5P) on HG002 WGS-1M + HiFi-10k against hg38. Narrowing only removes provably-zero high bytes, so ordering (including the unstable radix tie-break) is unchanged.

Performance

  • Isolated microbench: the narrowed sorts are 1.5–4.9× faster (4.9× for the 4-byte chain-score sort at small n).
  • End-to-end: small — radix sorting is under ~1% of map CPU time (dominated by the ksw2 DP kernels). Measured sort volume concentrates in anchor-tpos, chain-score, gen-hit, and compact_a. Shipped because it is free and byte-identical.

Not a ksw2 kernel change, so the SW-kernel bench does not apply; validated by the byte-identity map gate above.

… sorts

The klib radix sort does one 8-bit pass per key byte, but every
KRADIX_SORT_INIT in the tree declared an 8-byte key while the actual
keys are narrower. Declaring the true width skips the passes over the
always-zero high bytes.

Two structural (not workload-dependent) bounds are exploited:

- Genome coordinates -- seed SA/BWT positions (key_sai0), anchor target
  positions (key_anchor), and the chain/pair target-coordinate sorts in
  lchain and pe -- are bounded by the index length (< 2^40, i.e.
  references up to ~1 Tbp), so a 5-byte key suffices.
- The chain-backtrack sort keys on f[i], a non-negative int32 score
  stored in a uint64, whose top 32 bits are structurally zero, so a
  4-byte key suffices.

The score<<32 sorts (mb_gen_hit, mb_hit_sort, mate rescue) are left at
8 bytes: their high-byte occupancy depends on read length rather than a
structural bound.

Output is unchanged: byte-identical SAM verified against stock across
PE default, -u, -b MD, -b cs, single-end, and Hi-C (-5P). In isolation
the narrowed sorts are 1.5-4.9x faster; end-to-end the effect is small
because radix sorting is well under ~1% of map CPU time (dominated by
the ksw2 DP kernels).
@nh13 nh13 added feature review vehicle for a manifest feature; never a merge vehicle uninstalled No features.toml block yet; not installed into the distribution (dist). Remove when installed. labels Sep 7, 2026
@coderabbitai
coderabbitai Bot commented Sep 7, 2026 •
Copy link
Copy Markdown

Note

Reviews paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nh13
nh13 commented Sep 7, 2026
Copy link
Copy Markdown
Owner Author

@coderabbitai pause

@coderabbitai
coderabbitai Bot commented Sep 7, 2026
Copy link
Copy Markdown
✅ Action performed

Reviews paused.

nh13 added a commit that referenced this pull request Sep 7, 2026
Right-size klib radix key widths (5-byte genome coordinates, 4-byte
int32 chain score). output = identical; byte-identity verified across
PE default, -u, -b MD, -b cs, single-end and Hi-C. Kept downstream-only
(upstreamable = false); fork PR #46.
@nh13 nh13 removed the uninstalled No features.toml block yet; not installed into the distribution (dist). Remove when installed. label Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature review vehicle for a manifest feature; never a merge vehicle

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0