4 ms·
The accepted answer fails to understand that the standard library is exempt from following the C standard and makes a number of false or overly prescriptive ass
by saagarjha 7y ago
The accepted answer fails to understand that the standard library is exempt from following the C standard and makes a number of false or overly prescriptive assertions. (It also doesn't answer the question, but that's par for the course on Stack Overflow…)
- marmaduke 7y agoThe first comment on that answer mentions this. I would never take an answer on SO w/o a grain of salt w/o reading comments.
- seisvelas 7y ago>that's par for the course on Stack Overflow This is tangential to the meat of your comment but I feel compelled to nitpick based entirely on personal anecdote: I have been helped hundreds of times on StackOverflow by people who had nothing to gain from it and yet provided in depth, insightful answers. In my experience, the farther you get away from the big, overwhelmed tags, the better the quality is. But you don't want to get so obscure that you are just ignored. The Racket community on SO is particularly fantastic, and (in my experience) the more 'web' related your question is, the worse answers you get.
- saagarjha 7y agoYes, I'm not saying that Stack Overflow doesn't have "birdies". For example, I really enjoy this user's consistently high quality answers: https://stackoverflow.com/users/224132/peter-cordes https://stackoverflow.com/users/224132/peter-cordes
- seisvelas 7y agoHe once answered a question of mine about recursion with x86!
- sytelus 7y agoAgreed. Parent commentor is statistically wrong. A large number of SO answers are helpful but he found an anecdote to wrongfully preach generalization. Also, OP's question violates SO guideline in the sense that answer to this question would be highly opinionated.
- kelnos 7y ago> The accepted answer fails to understand that the standard library is exempt from following the C standard I didn't read it that way at all. Consider the audience: the questioner admits that they are new to C. Because of that, the author of the accepted answer wanted to make it crystal clear that code like this should never be written "especially if you're not a C compiler / standard library vendor". The answerer explicitly acknowledges that the code depends on implementation-defined behavior. IMO, the answer is great. It starts with a strong warning to never write code like that in your own programs (which you shouldn't), and then explains how it works, and why it works in the context of where it's written.
- saagarjha 7y agos/especially/unless/: there is a good reason why the strlen is written that way; it's because it's fast and the standard library is allowed to make assumptions about the platform it's running on. But the answer instead dilutes itself and keeps saying things like "supposedly" and calling the code "bad", along with a long tangent by using the sanitizers to "prove" that the code is "wrong". For a beginner, I feel that this answer is much better than the one that's accepted: https://stackoverflow.com/a/57655203/5230900 https://stackoverflow.com/a/57655203/5230900. It's not as long, so it would be nice to have more details about why the code does what it does, but it's not pushing an agenda.
- xenadu02 7y ago> it's because it's fast Is it? We have to ask a few questions: 1. Is it actually faster on today's CPUs? (Any linear memory access pattern is going to be detected by the prefetcher) 2. Would the compiler do the same or better? eg, could it be auto-vectorizing the loop if we didn't play tricks? 3. If we really care about trading complexity for speed, wouldn't a hand-written SSE/SSE2/AVX implementation be even faster? Why not spend our complexity budget there?
- saagarjha 7y agoHonestly, I don't know: I trust that the glibc people know what they are doing. However, in an attempt answer some of your questions: there is an SSE2 implementation available that might be used on computers that support it: https://github.com/bminor/glibc/blob/master/sysdeps/x86_64/strlen.S https://github.com/bminor/glibc/blob/master/sysdeps/x86_64/s... . Perhaps this version is for platforms that don't have a hand-optimized implementation? I'm not sure if the compiler is smart enough to auto-vectorize an arbitrary strlen implementation (or delegate it to a library call).
- gonzo 7y agoThe accepted answer fails to note that the subject code is a high-performance variant of strlen(), and has basically taken the ideas from many highly-optimized memcpy() routines. There is nothing wrong with the simpler version, but it’s not as fast (for longer strings).