logoalt Hacker News

GalaxyNovatoday at 12:02 AM3 repliesview on HN

Yes I have seen it happen a few times with `strlen` being called in a loop silently causing O(N) to turn to O(N^2)


Replies

sgerensertoday at 2:13 AM

Joel Spolsky coined the term “Shlemiel the Painter’s Algorithm” for this type of thing back in 2001: https://www.joelonsoftware.com/2001/12/11/back-to-basics/

jkrejchatoday at 1:23 AM

Reminds me of an article[1] that described how he cut GTA Online loading times by 70% because strlen was getting called for effectively every character in a string

[1]: https://nee.lv/2021/02/28/How-I-cut-GTA-Online-loading-times...

show 1 reply
senfiajtoday at 12:16 AM

Exactly, you can't write clean concise code when working with c strings. Almost every c string manipulation requires cognitive load: "Is the buffer size enough (including null terminator), should I reallocate it?", "I need to have the offset from the last concat, to make next concats performant", "Umm, shold I put null terminator at i or i + 1?"... It really sucks, it's akin to death by thousands of cuts.