Glibc's strlen implementation: Probably not what you'd guess
sources.redhat.com
Glibc's strlen implementation: Probably not what you'd guess
1–10 of 74 posts
Re: Glibc's strlen implementation: Probably not what you'd guess
#2Author covers really clever techniques to count bits, count non zero bytes like this etc etc. Bit manipulation at its best.
Re: Glibc's strlen implementation: Probably not what you'd guess
#3Will I employ similar technique in my own code? Absolutely not. It hinders readability and byte comparison instructions in consecutive memory addresses are so stupidly fast that I'll probably save less CPU time combined in all executions of my program than the total amount of time it took me to think this hack up.
If you're a maintainer of glibc though (which is known for its exceptionally clear and straight-forward code,) then I might consider accepting a patch from someone who thought it up.
edit: I thought similarly of DJB's loop unrolling when I saw it in qmail. It's cute, but will it make any noticeable difference today? Probably not.
Re: Glibc's strlen implementation: Probably not what you'd guess
#4Re: Glibc's strlen implementation: Probably not what you'd guess
#5for comparison: http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/string/st...
I used to use and love FreeBSD (Since switched to OS X as desktop os, still run FreeBSD servers), but OpenBSD source code just looked more approachable.
McKusicks' wonderful book and video course helped.
Re: Glibc's strlen implementation: Probably not what you'd guess
#6Re: Glibc's strlen implementation: Probably not what you'd guess
#7It's cute. Will I employ similar technique in my own code? Absolutely not. It hinders readability and byte comparison instructions in consecutive memory addresses are so stupidly fast that I'll probably save less CPU time combined in all executions of my program than the total amount of time it took me to think this hack up. If you're a maintainer of glibc though (which is known for its exceptionally clear and straig…
Re: Glibc's strlen implementation: Probably not what you'd guess
#8It's cute. Will I employ similar technique in my own code? Absolutely not. It hinders readability and byte comparison instructions in consecutive memory addresses are so stupidly fast that I'll probably save less CPU time combined in all executions of my program than the total amount of time it took me to think this hack up. If you're a maintainer of glibc though (which is known for its exceptionally clear and straig…
Sure, but... you're not writing glibc. How many computers run that code? At some point, if it's faster and correct, it's worth it.
I'm just curious if the speed-up of this hack was ever benchmarked. Or if anyone spent a while tracking down a source of bottlenecks, and found it to be an inefficient strlen implementation.
Re: Glibc's strlen implementation: Probably not what you'd guess
#9Re: Glibc's strlen implementation: Probably not what you'd guess
#10 1 million strlens on the same random 100 byte string:
Atom 330, 1.6GHz, gcc 4.3.2 (Debian Lenny)
glibc: 1.3ns/char
easy: 3.4ns/char
obsd: 3.4ns/char
Core2 Duo, 2.8GHz, gcc version 4.0.1 (Apple Inc. build 5484)
libc: 0.12ns/char
glibc: 0.39ns/char
easy: 0.58ns/char
obsd: 0.60ns/char
easy: while ( *p++) c++;
obsd: openbsd, for (s = str; *s; ++s); return s-str;
The preliminary conclusions are:The glibc strlen is something like twice as fast as the naive implementation, but there is something else out there that knocks its socks off.
Secondary conclusion would be: remember not to compare GHz across different processors.