返回全部资讯
Simon Willison 博客T2·开发与工程

用 C 与 Rust 编写 SQLite parse_number 扩展对比

原标题:parse_number() as a SQLite extension: C vs Rust
42AI 研判分

Simon Willison 借助 Claude 对比了用 C 与 Rust 实现 SQLite parse_number() 自定义函数的差异:C 版扩展仅 14 KB,Rust 版约 300 KB 但运行更快,原因是 Rust 的浮点解析实现比 C 的 strtod 快约一倍。作者随后尝试手写更快的 C 解析函数,并将整个过程整理为可复现的研究报告。

报道正文

I was out walking the dog when I got curious about how easy/difficult it would be to write a SQLite C extension that adds a parse_number() function for turning strings like -$1,234.56 or '12.5%' into numeric values that could be summed and sorted.

I fired up regular Claude chat (Opus 5.5) and prompted:

I'm thinking about a SQLite custom function 'parse_number(value)' that strips ',', '$', '£' and '%', handles a leading '-', and returns 'NULL' for anything it can’t parse Explore the difference between building this as a SQLite extension in C v.s. in Rust - I want to compare performance and code complexity and size of the resulting.so(or whatever) file

This gave me a solid starting point - the Rust extension was 321 KB (using Rusqlite) and 298 KB (using sqlite-loadable-rs), while the C extension was 14 KB. But the Rust extension was faster!

Turns out that's because the Rust equivalent of C's strtod is "twice as fast". So I prompted:

Try writing your own C function for parsing text into a decimal number that catches up to or beats the Rust implementation

And it did:

Net cost per row over 2 million rows, lower is better:

first Cnew Crusqlitesqlite-loadable
Mixed data67 ns19 ns46 ns41 ns
Integers only25 ns16 ns36 ns32 ns
Decimals only106 ns18 ns55 ns47 ns

Finally, I had it rewrite the prototype as a research report to match the others in simonw/research by prompting this:

Clone simonw/research from GitHub and turn your above investigation into a research report as seen in that repository - you can give me a zip file to download that contains your new folder