Shlemiel the Painter Is Back: How Zig's Libc Rewrite Made SQLite 12,000× Slower ...

@best

Shlemiel the Painter Is Back: How Zig's Libc Rewrite Made SQLite 12,000× Slower dennisforbes.ca · 18 points · 0 comments · HN thread

TL;DR Dennis Forbes describes how a chess analysis service built in Rust slowed dramatically when inserting about 40,000 JSON records into SQLite. The cause was Zig's pure-Zig rewrite of libc string functions (strspn, strcspn, strpbrk, strstr), compiled in via zig cc, which re-measures the full string length on every call, making tokenizing quadratic and roughly 12,000x slower on a 1.2MB file. The post frames it as a modern instance of the Shlemiel the Painter pattern.

  • The chess analysis service is Rust with SQLite; runners return large JSON bundles that get stored, and the insert of about 40,000 records took minutes of CPU time on Linux.
  • SQLite was compiled for Linux targets with zig cc via cargo-zigbuild, which uses Zig's pure-Zig libc rewrites instead of musl's C versions.
  • In the rewritten strspn, strcspn, strpbrk and strstr, the first step measures the entire string length, which is costly when a tokenizer repeatedly calls them on the remaining text.
  • Tokenizing a 1.2MB file was about 12,000x slower with this pattern, and the slowdown grows quadratically with file size; the Mac build, which did not use zig cc, was unaffected.
  • A partial fix was pre-pruning whitespace from JSON; the author moved Linux builds into containers to avoid the problem.
  • The post recalls Spolsky's 2001 'Back to Basics' essay and a Firefox string-concatenation fix that helped it win SunSpider.

Discussion The thread has no comments yet, so there is no discussion to summarize at the time of this digest. The story has 18 points.

#Zig #SQLite #performance #libc #algorithmicComplexity