SQLite Extensions: C vs Rust Performance in parse_number()
A comparison of C and Rust implementations of SQLite's parse_number() shows C achieving 16–19 ns per row, outperforming Rust by about twice, while both passed 8 million fuzz checks.
Implementation and Performance Comparison
Simon Willison’s Weblog compares four SQLite extensions implementing the same `parse_number()` functionality in C and Rust. The optimized C implementation achieved speeds of 16–19 ns per row, outperforming Rust versions by about twice and significantly surpassing SQL `replace()` in efficiency. However, plain C faced slower performance on decimal parsing due to reliance on `strtod()`, though a compact fast path helped close the gap. All versions passed 8 million fuzz checks, ensuring consistent behavior across test cases. The research highlights trade-offs between performance and precision, with C offering speed advantages but requiring careful optimization for decimal handling. Rust implementations, while slightly slower, provide memory safety and modern language features. The findings underscore the importance of language-specific optimizations when extending SQLite for numeric parsing tasks.
Motivation and Initial Exploration
The motivation for exploring a SQLite extension like parse_number() stems from the need to handle real-world numeric formatting in databases. This function aims to clean and convert strings such as -$1,234.56 or '12.5%' into numeric values for analysis. Initial exploration involved comparing the development process and outcomes of implementing this function in C versus Rust. The C extension proved significantly smaller, at just 14 KB, compared to the Rust implementations, which ranged from 298 KB to 321 KB depending on the library used (sqlite-loadable-rs or Rusqlite). These differences in file size suggest potential trade-offs between performance, code complexity, and portability when choosing between the two languages for SQLite extensions. The exploration was guided by a prompt to Claude chat (Opus 5.5), which provided a solid starting point for evaluating both approaches.
Performance Insights and Tooling
When evaluating the performance of SQLite extensions written in C versus Rust, one notable insight emerges from recent benchmarks. According to Simon Willison, the Rust equivalent of C's strtod function is reported to be "twice as fast." This suggests that Rust may offer performance advantages in certain numerical parsing tasks relevant to SQLite extensions. However, these findings should be interpreted with caution, as they are specific to the implementation tested and may not generalize across all use cases. The performance of SQLite extensions can also depend on factors such as memory management, compiler optimizations, and the specific functions being extended. While Rust's safety guarantees and modern tooling may contribute to performance improvements, further research is needed to validate these claims across a broader range of scenarios.
Sources and reporting notes
Based on published source material. No independent hands-on testing is claimed.