Skip to content

perf(rust): add nsieve 3.rs (Wheel 8-of-30 Sieve) - #505

Open
bonnhatnguyen wants to merge 1 commit into
hanabi1224:mainfrom
bonnhatnguyen:rust-nsieve-wheel-3
Open

bonnhatnguyen wants to merge 1 commit into
hanabi1224:mainfrom
bonnhatnguyen:rust-nsieve-wheel-3

Conversation

@bonnhatnguyen

Copy link
Copy Markdown

Description

Introduces a high-performance Rust implementation (3.rs) for the nsieve benchmark utilizing a 1-bit Wheel 8-of-30 Sieve of Eratosthenes.

Key Optimizations

  • Wheel 8-of-30 Factorization: Skips all multiples of 2, 3, and 5 (8 candidate coprimes per cycle of 30: 1, 7, 11, 13, 17, 19, 23, 29). This eliminates ~73.3% of composite markings before sieving starts.
  • Bit-Packed Candidate Buffer: Uses 1 bit per odd integer (u64 words). For $M = 40,960,000$, total memory footprint is only ~2.44 MB, fitting entirely inside CPU L2/L3 cache and avoiding DRAM bandwidth bottlenecks.
  • Register-Unrolled Composite Marking: 8-stride pre-computed step cycle unrolled with unchecked pointer offsets to maximize instruction-level parallelism (ILP) and branch prediction throughput.
  • Unrolled Branchless Prime Counting: Traverses remaining prime candidates across wheel residues with branchless bit extractions.
  • Zero External Dependencies: Implemented entirely with Rust std, requiring no external crates.

Verification & Benchmark

  • Verified against unit tests (4_out and 5_out), output matches byte-for-byte.
  • Benchmark comparison on input 12 ($M = 40,960,000$):
    • 1.rs (baseline): ~778 ms
    • 3.rs (this PR): ~38 ms (~20.4x faster)

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