Re: While we're at it

Colm MacCárthaigh <[email protected]> Wed, 31 Mar 2010 16:04:00 -0700
Newsgroups gmane.network.djbdns
Message-ID <[email protected]>
On Wed, Mar 31, 2010 at 3:40 PM, Hugo Monteiro <[email protected]> wrote:
> Why 4, and not more then? Is 4 the an average of something? Bare in mind
> we're talking about strings and not only words.

I think it's essentially arbitrary. Originally I thought that some
compilers might be intelligent enough to compress it into a neat
word-wize test, but have found no evidence of this in machine code
emitted by gcc or icc.

In a dynamic or semi-interpretted language (such as Java) it's
possible to do run-time statistical profiling and change the balance
between memory consumption Vs jumps based on observed average string
length and memory availability. (at some point the amount of memory
consumed by the unrolled code will cross a caching boundary and become
a source of serious latency). But in statically compiled languages
such as C, fewer such tricks are available - one could choose a value
based on a more-widely measured average - but just picking 4 seems
like an ok thing to do too - it hardly matters for the type of usage
patterns DJB's daemons have.

Security and portability are more important goals than performance for
str_len.c, so it doesn't avail of any of the modern tricks that are
available (such as the SSE instruction set). Though I think any modern
compiler would do a better job of unrolling than putting it explicitly
in the code. Also, some compilers do allow you to feed them profiling
hints, which they use to make decisions such as to what degree a loop
should be unrolled.

-- 
Colm