feat(index): suffix-array construction via libsais (byte-identical to caps-sa) - #109
Open
BenjaminDEMAILLE wants to merge 5 commits into
Open
feat(index): suffix-array construction via libsais (byte-identical to caps-sa)#109BenjaminDEMAILLE wants to merge 5 commits into
BenjaminDEMAILLE wants to merge 5 commits into
Conversation
CI's clippy advanced to 1.97 (new lints fail the `-D warnings` gate) and rustsec flagged several advisories in the dependency tree. Both block all PRs. Clippy: - src/index/suffix_array.rs: assert!(x == 0) -> assert_eq! (manual_assert_eq) - src/io/sam.rs, tests/alignment_features.rs: byte-slice literals -> byte strings, e.g. [b'S', b'M'] -> *b"SM" (byte_char_slices) - src/quant/transcriptome.rs: if-let/else-return-None -> `?` (question_mark) Security audit (Cargo.lock only, no manifest change): - memmap2 0.9.10 -> 0.9.11 (RUSTSEC-2026-0186, out-of-bounds pointer offset) - crossbeam-epoch 0.9.18 -> 0.9.20 (RUSTSEC-2026-0204, invalid pointer deref) - anyhow 1.0.102 -> 1.0.104 (RUSTSEC-2026-0190, unsound downcast_mut) No behavioural change. Unblocks CI for open and future PRs. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add `sa_build::build_libsais`, an alternative suffix-array constructor using libsais (fast SA-IS) instead of caps-sa. It sorts the same per-segment sentinel-transformed text the caps-sa sentinel arm uses; because a suffix array is unique for a given text, the packed output is byte-for-byte identical to `build()` (and hence to STAR 2.7.11b). Validated by the `build_libsais_matches_caps_sa_*` tests over single-chr, multi-chr-with-N, and repetitive fixtures. - libsais is depended on with `default-features = false` (no OpenMP), so libsais-sys builds the C core via `cc` without an OpenMP toolchain (keeps the 5-platform CI portable). - Covers genomes whose per-segment sentinel alphabet fits u8/u16; larger segment counts fall back to `build()` until the libsais large-alphabet (i32) path is wired. Not yet swapped in as the default builder. Roadmap PR-15 (index-construction speedup). Next: large-alphabet path + byte- identical validation on the yeast and human genomes, then switch `build()` over and benchmark. See docs-old/dev/porting-from-star-rs.md. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Extend build_libsais with the i32 large-alphabet arm so libsais covers genomes whose per-segment sentinel alphabet exceeds u16 (many chromosomes + sjdb), removing the previous caps-sa fallback for alphabet width. The width is auto-selected (u8/u16/i32); an internal forceable width lets tests exercise the i32 arm on small fixtures without a >64K-segment genome. Byte-identical to build() (build_libsais_i32_matches_caps_sa_* tests). libsais large-alphabet uses an i32 SA buffer, which bounds this arm to texts shorter than 2^31; huge sequences (e.g. human) still use caps-sa's external- memory path. Selecting libsais vs caps-sa by available memory, and switching build()/build_streaming over, remain roadmap follow-ups. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
genomeGenerate builds the suffix array with libsais when RUSTAR_USE_LIBSAIS is set (default stays caps-sa). The libsais SA is written to the SA file through the same PackedStreamWriter, so the on-disk format is unchanged. A new test streams build_libsais's entries exactly as GenomeIndex::build does and asserts the byte stream is identical to the caps-sa streaming build, pinning the end-to-end genomeGenerate SA output byte-for-byte (on top of the unit-level build_libsais == build equality for both the u8/u16 and i32 arms). libsais builds in memory, so the opt-in path targets genomes that fit in RAM; huge genomes keep the caps-sa external-memory streaming path. Making libsais the default for RAM-sized genomes awaits a construction-time benchmark (output is already proven byte-identical, so enabling it is output-safe). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
BenjaminDEMAILLE
marked this pull request as ready for review
July 23, 2026 18:38
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The index-construction speedup from the roadmap (Rob's "we already have one for index construction"), implemented with
libsais(fast SA-IS) as an alternative tocaps-sa.Benchmark (real genomeGenerate, 48 MB / 24 chromosomes, release, 8 threads)
RUSTAR_USE_LIBSAIS=1)~3.0× faster,
SAandSAindexbyte-for-byte identical — at ~2.5× the peak RAM. libsais builds the SA in memory (the i64 SA buffer is2N·8bytes ≈ 768 MB here + text + packed output); caps-sa's default path spills to disk (external-memory), so its RSS stays bounded. Extrapolated to human (2N ≈ 6.2 B), libsais would need ~50 GB while caps-sa ext-mem stays at a few GB — hence libsais is opt-in and best defaulted only for RAM-sized genomes.Byte-identical, by construction
A suffix array is unique for a given text.
build_libsaissorts the same per-segment sentinel-transformed text the existing caps-sa sentinel arm uses, so its packed output is byte-for-byte identical tobuild()— and hence to STAR 2.7.11b. No tie-break/ordering risk; correctness reduces to "feed libsais the same transformed text."Validated at four levels (all green):
build_libsais== caps-sabuild()on single-chr, multi-chr-with-N, repetitive fixtures;i32arm forced on small fixtures;build_libsaisexactly asGenomeIndex::builddoes matches the caps-sa streaming byte stream;SA+SAindex.What's included
sa_build::build_libsaiswith automatic alphabet-width selection:u8/u16for few segments,i32large-alphabet for many-segment genomes (many chromosomes + sjdb).genomeGeneratebehindRUSTAR_USE_LIBSAIS=1(default stays caps-sa); on-disk format unchanged.libsaispinned withdefault-features = false(no OpenMP);libsais-sysbuilds its C core viacc. CI green on all 5 platforms, includingwindows-x86_64.On making it the default
Output is proven byte-identical and it's ~3× faster, so flipping the default is output-safe. The trade is RAM (above). Recommended follow-up: enable libsais by default for RAM-sized genomes with a size guard that falls back to caps-sa ext-mem above a threshold. Happy to do that here or in a follow-up once we agree the threshold.
Roadmap item PR-15. Based on #107 (CI health); independent of the WASP stack.
🤖 Generated with Claude Code