interesting fact about StrCaseCmp

Ludolf Holzheid lholzheid at bihl-wiedemann.de
Wed Feb 19 16:11:31 GMT 2003


On 19 Feb 2003 at 11:36, Martin Pool wrote:

> On 18 Feb 2003, Ludolf Holzheid <lholzheid at bihl-wiedemann.de> wrote:
> 
> > In the embedded/real time world, malloc() and friends are strongly
> > deprecated as you can't predict how long they will take.  They have
> > to go through a linked list of unknown length and may even start a
> > garbage collection.
> 
> Well that's fine, but Samba is obviously not a hard real time program,
> so it's hardly relevant.  Bear in mind that it generally runs in
> virtual memory and so *any* memory access can take an unboundedly long
> time.

You can't avoid accessing the strings to compare, but maybe you can 
avoid calling functions that may be slow.

(This is going to get nit-picking.)

> > If StrCaseCmp() is really that sensitive w.r.t. processor cycles, you 
> > better keep the malloc()ed buffers between the calls and increase 
> > their size (by calling free() and malloc(), not realloc()) if the 
> > strings to be compared do not fit.
> 
> With all due respect, this is a really silly argument.  Are you not
> familiar with the saying that premature optimization is the root of
> all evil?  And anyhow, if it doesn't need to be correct, it can be as
> fast as you like.  It can just return true...

I don't see why it is premature optimization if you keep a buffer 
allocated until you need a larger one.

> If this function needs to be fast, then it should not be *copying* the
> strings *four* times to compare them, when comparing the first *byte*
> would often be enough.  
> 
> A minimal change to make StrCaseCmp at least not truncate strings
> would involve adding a malloc(), which is harmless since it's very
> slow already.  A good implementation of StrCaseCmp shouldn't do any
> copying or allocation.  When I get a chance I'll write that and post
> numbers.

You are right.

If you are going to change StrCaseCmp() you should do it in a way 
that avoids copying altogether.

Ludolf

---------------------------------------------------------------
Ludolf Holzheid             Tel:    +49 621 339960
Bihl+Wiedemann GmbH         Fax:    +49 621 3392239
Flosswoerthstrasse 41       e-mail: lholzheid at bihl-wiedemann.de
D-68199 Mannheim, Germany
---------------------------------------------------------------



More information about the samba-technical mailing list