Live data from Hacker News

It Can Happen to You

mattkeeter.com

91–100 of 419 posts

Re: It Can Happen to You

#92
post #15

The moral of the story, as far as I'm concerned: do NOT parse strings in C! Use a library, prefferably in a higher-level language. C string handling is a mess of viciously surprising APIs, juggling those particular footguns is almost certainly not your least bad option.

Funny, the ergonomics of the Swift string API are so bad that I've started learning a lower level language for parsing, etc. Here's my favorite WTF: https://christiantietze.de/posts/2020/01/string-index-offset...

You can fault the docs but what's the problem with the API? Why should you be surprised that accessing a collection with 'nil' index is a runtime error? What else could it be?

A simple fix in the doc seems to solve the confusion:

"Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index [in which case it returns nil]".

It does say "returns an index ... unless ..". So yeah, a bit too terse. But with is the issue with the API?

    //  /!\  Warning! Do not use in production!   /!\
    let s = "Swift"
    if let i = s.index(s.startIndex, offsetBy: 5, limitedBy: s.endIndex) {
       print(s[i])
    }
"What does it print? Nothing, you hope?"

Seriously? 'print(s[nil])' should print nothing? How about 'let c = s[nil]'. Should that just silently pass? That is the runtime error, btw. It won't even get to print(). (Valid to question the entirely non-informative error, however.)

Re: It Can Happen to You

#93
post #15

The moral of the story, as far as I'm concerned: do NOT parse strings in C! Use a library, prefferably in a higher-level language. C string handling is a mess of viciously surprising APIs, juggling those particular footguns is almost certainly not your least bad option.

I've taken it a step further (or three). I'm building a whole computer on the principle[1] (one possible way of looking at it) of avoiding parsing as far as possible.

https://github.com/akkartik/mu

Mu takes the dwm[2] principle of avoid-config-files/modify-sources-to-reconfigure to the limit. The idea is that there are at any given time only 3 languages in the computer:

1. A self-hosted notation for a subset of 32-bit x86 machine code

2. A memory-safe statement-oriented language where most statements map 1:1 to machine-code instructions. Written in level 1 above.

3. A high-level interpreted language.

The vision is to fix 1 and 2 but allow 3 to fork wantonly. If you want a Lisp for your HLL, make a fork. If you want a Python-like syntax, make a fork. But, and this is the important part, in any given computer/repo there is only ever one language. Only one non-trivial parser. (Levels 1 and 2 have extremely uniform syntax.)

As best I can tell, the #1 way to avoid the need to run a fuzzer is to avoid writing parsers. Just say no.

[1] https://lobste.rs/s/to8wpr/configuration_files_are_canary_wa...

[2] https://dwm.suckless.org

Re: It Can Happen to You

#94
post #32

Loving the progression here. Tomorrow, someone’s going to reduce the boot times of macOS by 90% by the same principle. A week from now, someone will prove P=NP because all the problems we thought were NP were just running strlen() on the whole input.

Amazing

Re: It Can Happen to You

#95
post #27

Blog author here! Thanks to HN for warning me about sscanf at exactly the right time – within a day of me trying to load some ASCII STLs and noticing it was slow... Linked deep in the Twitter replies [1], there's an open glibc issue about this, dating back to 2014: https://sourceware.org/bugzilla/show_bug.cgi?id=17577 C doesn't have any requirements on the complexity of sscanf, so it might not be a bug per se, but it…

I love the format of this blog post. Perfect length. Perfect detail. You don't waste words. A good number of images.

And it works without javascript!

Re: It Can Happen to You

#96

So many times, in higher level code, it's seeing a foreach loop in a foreach loop, and the nested loop is calling an API or re-rerunning the same database call 5000 times. Move things around or just use a cache... and instant 1000%+ speedup. I've seen this too many times to count, often in apps and on sites that are in fairly heavy use. The answer is often to scale up and pay for 10x more server capacity to handle th…

My fav optimization story:

A few years back, I was doing some geographic calculations. Basically building a box of lat/lngs and getting all the points within that box.

It was slow. Weirdly slow. I made sure the lat and lng of the records were in the index, but it was still slow.

More testing revealed that the way I was passing the lat/lng into the query was causing those values to be converted to strings, which were converted back to numbers, but caused the index to not be used. This meant the database had to do a lot more work.

Converting the parameters to numbers made sure the index could be used, which lead to a nice speed up.

Edit: I thought I wrote this up, but I didn't. But it turns out there was some interesting sorting shenanigans that I did: https://www.mooreds.com/wordpress/archives/547

Sometimes it's worth peering down through those layers of abstraction!

Re: It Can Happen to You

#97
Still, folks in the comments section generally agreed: they wouldn't write anything that silly.

Well, if you've never accidentally crashed a system running an unexpectedly (and unnecessarily) "non-performant" piece of code then you're either an absolute genius of a coder, or relatively inexperienced.

Re: It Can Happen to You

#99

Earlier quoted context omitted.

And maybe, in a decade or so, the man page for these functions will list their algorithmic complexity! That was the most interesting takeaway from this article, for me at least. I have only seen a one or two libraries that actually list this in their documentation.

All of the C++ algorithms list complexity guarantees, I believe. This saga stunned me to learn that C doesn’t seem to do this.

Are there complexity guarantees for std::istream::operator>>?

Re: It Can Happen to You

#100
Strange. I've written some simple parsers over the course of my life. Latest one was to parse some subset of SQL for home grown in memory database.

I do not remember ever using anything even remotely resembling scanf or related stuff. It was always read next char from some abstracted stream and execute state machine on it.

Post reply on HN