parse_number() for SQLite in C and Rust: Rust lost on size before it lost on speed
Translated by a model; the original is in Russian.
Simon Willison has written up a small research project that started on a dog walk. He wondered how hard it would be to write a SQLite C extension adding a parse_number() function. The function would turn strings like -$1,234.56 or 12.5% into numeric values that can be summed and sorted.
The brief
Willison opened a regular Claude chat (Opus 5.5) and described the function. It strips ,, $, £ and %, handles a leading minus sign, and returns NULL for anything it can't parse. He asked for a comparison of C and Rust implementations covering performance, code complexity and the size of the resulting .so file.
The size gap was dramatic. The Rust extension came to 321 KB using Rusqlite and 298 KB using sqlite-loadable-rs. The C version fit in 14 KB. Yet Rust was the faster one.
Where the speed went
The cause was in the standard library: Rust's equivalent of C's strtod() is roughly twice as fast. Willison then asked the model to write its own C function for parsing decimal numbers that would catch up with or beat Rust. The model managed it by using "Clinger's fast path".
Net cost per row over 2 million rows (lower is better):
| Data | first C | new C | rusqlite | sqlite-loadable |
|---|---|---|---|---|
| Mixed | 67 ns | 19 ns | 46 ns | 41 ns |
| Integers only | 25 ns | 16 ns | 36 ns | 32 ns |
| Decimals only | 106 ns | 18 ns | 55 ns | 47 ns |
The original C version lagged most on decimals, where it relied on strtod(). The compact fast path closed that gap.
The final report
Finally, Willison asked Claude to clone his simonw/research repository from GitHub and write up the investigation as a report in the same format, delivered as a zip file. The report covers four extensions with identical parse_number() behavior. Their tests check type preservation, parsing rules and floating-point accuracy.
The optimized C implementation came in at 16–19 ns per row. That is about twice as fast as the Rust versions and far faster than doing the job with SQL's replace(). All four versions produced matching results across 8 million fuzz checks.
Source: Simon Willison
Comments
Sign in, to comment.