Earlier quoted context omitted.
I'm confused. What are you getting at? The code linked is from OpenBSD, which is under a BSD-style license which is considered GPL compatible.
Possibly the original non-free version was similar to the OpenBSD, or perhaps the GlibC developers took applied this rule where not strictly necessary. Don't know; just offering a potential clue as to why the GNU version is so different.
Glibc's strlen implementation: Probably not what you'd guess
61–70 of 74 posts
Re: Glibc's strlen implementation: Probably not what you'd guess
#62Re: Glibc's strlen implementation: Probably not what you'd guess
#63Earlier quoted context omitted.
I have no idea what points you're trying to make here. You can trade per-byte cycle counts for lower cost to invoke the routine, and for not evicting cache and BTB entries. On your second point, I assumed it was the "rep" part of the instruction that you were railing against. Apparently it's the "not knowing the difference between a byte and a dword" part. That's awesome. You can have the last word, if you'd like.
rep movsb/rep movsd works well for moving data. However, you obviously can't use that approach for searching for a 0. That's why the code is optimized as it was. My point is that using rep scasb is suboptimal. Don't know what you're talking about "lower cost to invoke the routine", and the cache/BTB entries would be negligible on a small routine like this. You seem kinda angry and bitter whenever you reply to me :/ C…
Re: Glibc's strlen implementation: Probably not what you'd guess
#64Earlier quoted context omitted.
Point taken, and you're right. The speed-up I was referring to howeverwasn't "strlen speedup" but "your entire app running with naive strlen implementation, vs your entire app with clever strlen." I also wasn't saying this has no place in it. I was trying to add that these hacks aren't usually what makes your app execute twice as fast or feel more 'snappy', unless the bread and butter of your app is string processin…
Of course, that's Knuth's warning that "Premature optimization is the root of all evil." I assumed we've all heard that before, and we're only thinking about applying optimizations after measurement.
Re: Glibc's strlen implementation: Probably not what you'd guess
#65Earlier quoted context omitted.
rep movsb/rep movsd works well for moving data. However, you obviously can't use that approach for searching for a 0. That's why the code is optimized as it was. My point is that using rep scasb is suboptimal. Don't know what you're talking about "lower cost to invoke the routine", and the cache/BTB entries would be negligible on a small routine like this. You seem kinda angry and bitter whenever you reply to me :/ C…
It costs cycles to call a C function. I seem angry and bitter all the time. But my point is just, there's an argument in favor of scasb.
Sure, it would bloat the code a little to inline the optimized version, but it could be done in tight inner loops if required.
Re: Glibc's strlen implementation: Probably not what you'd guess
#66Earlier quoted context omitted.
It costs cycles to call a C function. I seem angry and bitter all the time. But my point is just, there's an argument in favor of scasb.
So you're comparing inlined rep scasb, with non-inlined alternative. Interesting comparison I guess. Sure, it would bloat the code a little to inline the optimized version, but it could be done in tight inner loops if required.
Re: Glibc's strlen implementation: Probably not what you'd guess
#67for comparison: http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/string/st...
The glibc version is written in such a way to minimize the jumps in the assembly and therefore produce faster code. Besides reading 4 bytes at a time, it does some clever (and very nontrivial) "magic." But again, all to reduce the number of jumps.
Re: Glibc's strlen implementation: Probably not what you'd guess
#68It's faster if you just store the length in a separate memory location. This is very fast: getLength() { return length; }
I'm worried nobody's going to mod this up because it doesn't talk about bit twiddling hacks or assembly cycle counts, but it is in fact that real answer to this problem. Don't use ASCIIZ when string processing is a bottleneck.
Re: Glibc's strlen implementation: Probably not what you'd guess
#69Earlier quoted context omitted.
I'm worried nobody's going to mod this up because it doesn't talk about bit twiddling hacks or assembly cycle counts, but it is in fact that real answer to this problem. Don't use ASCIIZ when string processing is a bottleneck.
Probably because it's not a real answer to _this_ problem (counting the number of bytes in a null-terminated string)?
Re: Glibc's strlen implementation: Probably not what you'd guess
#70The lesson here is use library functions . Someone smarter than you has probably optimized the hell out of them.
The "right" answer was just to use strlen()...