I have been building Athena, an offline Python tool that scans Git history to find broken callers across a codebase before you merge a pull request. On small repositories it felt instant. Once I pointed it at 1,000+ commits and thousands of files, a single cold scan took 30 to 45 seconds.
My first instinct was the usual one: rewrite the whole thing in Rust. After profiling the actual bottlenecks, I realized a full rewrite would throw away Python's rock-solid ast module and make contributing much harder. So I kept Python as the core and tried fixing only what the profiler flagged.
1. The biggest early win had nothing to do with Rust
Before writing any Rust code, I looked at where time was actually going. Our Git inspector was spawning a new git cat-file -s and git show subprocess for every changed file. Across hundreds of commits, OS process startup overhead dominated the whole runtime.
Switching to a single batched binary git cat-file --batch stream fixed it immediately:
Before: 1 subprocess per file (git show / cat-file -s) -> 12.74s
After: 1 batched git cat-file --batch stream -> 3.17s (4.02x faster)
No language rewrite needed. Just fewer processes.
2. When porting Python to Rust actually makes it slower
Next, I ported our unified diff parser (parse_unified_diff_raw) to Rust via PyO3. Every test passed and all 229 recorded inputs matched Python identically. Then I ran the benchmark:
- Pure Python:
1.6 ms - Rust (PyO3):
4.8 ms(3.1x slower)
Why did Rust lose? When Python calls a Rust function through PyO3, strings and lists have to cross the Foreign Function Interface (FFI) boundary. Parsing a unified diff is mostly lightweight string slicing, so converting and allocating those strings across Python and Rust cost more than the parsing work itself. I deleted that Rust port and kept diff parsing in Python.
3. Where Rust actually crushed Python
Rust paid off big on tight CPU loops where compute far outweighed FFI copying. Four hot kernels passed strict equivalence checks and stayed enabled (with FAST_ANYTHING_DISABLE=1 available anytime to toggle back to pure Python):
| Kernel | Speedup | Parity Checks |
|---|---|---|
redact_secrets |
41x (237.3us -> 5.8us) |
776/776 identical |
score_graph_trigrams |
8.8x (37.3ms -> 4.2ms) |
330/330 identical |
compute_community_assignments |
3.5x (9.6ms -> 2.7ms) |
206/206 identical |
extract_structural_symbols_raw |
1.9x (16.2us -> 8.6us) |
800/800 identical |
4. Verifying zero quality loss across 1,000+ commits
Speed means nothing if native ports silently change results, so I audited pure Python against the hybrid Rust build across three large public repositories over 1,000+ commits each:
| Repository | Commits | Graph Build (Python -> Rust) | Parity |
|---|---|---|---|
google/flatbuffers |
1,000 | 43.39s -> 1.62s (26.73x) | 14/14 SHA-256 checks identical |
NVIDIA/cuvs |
1,472 | 29.89s -> 1.33s (22.48x) | 14/14 SHA-256 checks identical |
microsoft/playwright-python |
1,000 | 45.78s -> 12.22s (3.75x) | 14/14 SHA-256 checks identical |
playwright-python saw a smaller jump because it is mostly Python files (where ast.parse stays in Python) and still iterates 100k+ edges in Python during queries.
Takeaway
If you have a slow Python CLI or developer tool, resist rewriting the whole codebase on day one. Batch your subprocess calls first, measure FFI copy costs carefully and only move the hot loops that actually benchmark faster.
All code, tests and benchmark scripts are open source under Apache-2.0 here: https://github.com/aaryanrwt/athena
Top comments (0)