Shlemiel the Painter Is Back: How Zig’s libc Rewrite Made SQLite Slower

94 points by ohrv 5 days ago on lobsters | 18 comments

squeek502 | 4 days ago

Student | 5 days ago

That’s a lovely way of explaining quadratic time operations. I took a quick look at the linked zig code, and presumably it’s like this because it’s mapping c strings into nice safe zig arrays rather than treating them as unsafe streams of memory locations containing chars.

refi64 | 5 days ago

From one build and deploy script on my Mac I can target all of my platforms without containers or build machines. Zig’s elegant libzigc re-implements the standard library so you can static link the necessary functionality.

Zig uses musl (the standard library rewrite), but they have been rewriting it piece by piece in pure Zig (via libzigc), removing the C dependencies one at a time.

Hmm, was this using the musl triple when invoking zigbuild? I'd be a bit surprised if that didn't use musl, but I'm not sure what other libc values can be used in the triple other than glibc.

jdpage | 4 days ago

Zig treats triples as requesting an ABI, rather than an implementation. A member of the core team discussed the rationale behind it in this Lobsters comment.

squeek502 | 4 days ago

The quoted text is correct, you can follow the link to the commit and see that musl sources are deleted as libzigc symbols are added. This is also done for symbols provided by compiler_rt.

Relevant issues: https://github.com/ziglang/zig/issues/2879 -> https://codeberg.org/ziglang/zig/issues/30978

tobin_baker | 4 days ago

Hate to say it, but LLM code review certainly would have caught such an obvious "accidentally quadratic" bug.

wofo | 4 days ago

And we would have missed the community showing up, and having fun working together on a fix.

I think this is acceptable in a pre-1.0 language.

andrewrk | 4 days ago

Most of the people working on zig libc project has been young, new contributors. It's been a fantastic way to grow the community, to introduce people to systems programming, and to give them a sense of empowerment - the idea that you actually can implement a libc yourself.

Sometimes, a crucial part of the lesson is seeing the consequences of one's mistakes in the wild.

I made the judgement call to let this slide in the review. I'm still not sure whether it was the right or wrong call.

zaphar | 4 days ago

Giving people the space to make a mistake and then fix it with a community that encourages them in that is I think more valuable than a work that is clearly in progress have a performance regression but otherwise be safe.

Other's might disagree with me but I for one think it was fine.

wofo | 4 days ago

Thanks for this! I got into systems programming thanks to people like you. Back when the Rust project was small and had a similarly welcoming attitude, one of the maintainers taught me basic git fu instead of telling me to RTFM. Fast-forward 12 years and now I'm paid to work on Firefox's networking stack.

Keep going :)

proctrap | 3 days ago

to give them a sense of empowerment - the idea that you actually can implement a libc yourself

I think this is worth a lot. I still have this mental barrier regarding such things. Something something high bar to pass and why reinvent the wheel. Even though it could be fun. If no one writes crypto then "don't write cryptography code" will become "no one knows how to write it".

pmarreck | 4 days ago

You don't even need that. You just need a ratio gate.

  1. Measure the time it takes for a function to process 1x an input (which is basically "setup time"), 10x an input, 100x an input, and 1000x an input. If you want to get fancy, warm it up first with 10000x input to get to steady-state performance.

  2. Measure the ratios between those times. (This should be somewhat cpu-details-orthogonal and somewhat immune to load effects.) Is it linear? n log n? n squared? Derive a complexity score from that (within a bit of tolerance). Force it to be under a certain score.

  3. Done.

mitsuhiko | 4 days ago

But that is a lot more work than having an AI code reviewer run. And it only solves for a very narrow thing.

david_chisnall | 4 days ago

This is the kind of thing that an LLM is very bad at.

For very small strings, any vectorisation is unlikely to give a win because all of the futzing with the unaligned starts and ends will cause problems. For large strings, it's a big win but reading past the end of the buffer may cause problems (most architectures won't trap, but some will even for aligned reads). If the string fits nicely in the cache then the strlen is warming the cache for you and the vectorised match will be incredibly fast. For very large strings, the cost of the strlen will dominate.

The LLM will tell you that your code looks like an antipattern. But did you actually want to optimise for those very long strings? Most of the data I've seen says that strings passed to standard-library string functions tend to be between 0 bytes and 1 KiB. 0 bytes is much more common than I'd expect, things above about 128 bytes are increasingly infrequent. So what should you be optimising for? The LLM advice that I've seen for this kind of thing will let you avoid a pathological case at the expense of making the common case slower. Is that the tradeoff that you wanted to make?

mitsuhiko | 4 days ago

This is the kind of thing that an LLM is very bad at. […] The LLM will tell you that your code looks like an antipattern. […] The LLM advice that I've seen for this kind of thing will let you avoid a pathological case at the expense of making the common case slower.

I feel like rather than guessing what the LLM will say, we can just ask it. Here is what Opus 5.5 in Pi reports when you put it to the file and ask the following question: "Please review string.zig against the libc implementation": review.md

Beyond the actual bugs if found, here is what it says on speed:

  • Every call measures the whole string first. strchr, strspn, strcspn, strpbrk, strstr and strcasestr call std.mem.span/len on the full haystack, so even a match at index 0 costs O(strlen). musl stops at the first match. A common loop like while ((p = strchr(p, ','))) becomes quadratic.
  • strtok_r and strsep are quadratic. They call span on the rest of the string on every call, so splitting a large buffer costs O(n²).

I think those are completely accurate findings. But worse, it immediately found three behavior bugs in memccpy, strncmp and strchr. memccpy(dst, "abc", 'b', 4) for instance gives the wrong results in the new zig implementation and it noticed that strncmp reads one extra byte beyond n.

(And it also confirmed that all three bugs are still in the latest version on git)

david_chisnall | 4 days ago

I feel like rather than guessing what the LLM will say, we can just ask it

And then you did, and it does exactly what I said it would. It tells you about the worst case, but it doesn't tell you what this does to the average case. What should this function be optimising for? There's no discussion of that and it sends you down a path of changing it in a way that may make it worse for most users.

mitsuhiko | 4 days ago

And then you did, and it does exactly what I said it would. It tells you about the worst case, but it doesn't tell you what this does to the average case. What should this function be optimising for?

It does. It gives you an example of how that function is typically called and why that behavior is bad for that case.

mplant | 4 days ago

but it doesn't tell you what this does to the average case.

It kind of does:

A common loop like while ((p = strchr(p, ','))) becomes quadratic.