parse_number() as a SQLite extension: C vs Rust
研究:将parse_number()作为SQLite扩展:C语言与Rust 当我带着狗散步时,我开始好奇编写一个SQLite C扩展来添加一个parse_number()函数会有多容易/困难,这个函数可以将类似-$1,234.56或'12.5%'这样的字符串转换为可以求和和排序的数值。我打开了常规的Claude聊天(Opus 5.5),并输入了以下内容: 我正在考虑一个SQLite自定义函数'parse_number(value)',它会删除','、'$'、'£'和'%',处理前导的'-',并返回'NULL'如果无法解析任何内容。 探索以C语言还是Rust语言构建这个SQLite扩展之间的差异——我想比较性能以及最终生成的.so(或其他)文件的代码复杂性和大小。 这给了我一个良好的起点——Rust扩展使用Rusqlite时是321 KB,使用sqlite-loadable-rs时是298 KB,而C扩展是14 KB。但Rust扩展更快!原来是因为Rust中C的strtod的等效实现“快两倍”。因此我继续提问: 尝试编写你自己的C函数,将文本解析为十进制数字,以赶上或超越Rust实现 它做到了,使用了“Clinger的快速路径”:每行的净成本超过2百万
Research: parse_number() as a SQLite extension: C vs Rust 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, using "Clinger's fast path": Net cost per row over 2 mill