Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 15 Feb 2012 14:22:25 +0200
From:      Andrey Simonenko <simon@comsys.ntu-kpi.kiev.ua>
To:        Bernard van Gastel <bvgastel@bitpowder.com>
Cc:        hackers@freebsd.org
Subject:   Re: memmem small optimalisation
Message-ID:  <20120215122225.GA34935@pm513-1.comsys.ntu-kpi.kiev.ua>
In-Reply-To: <1BDAF8C6-66A5-4F4F-A2CD-3928A0B10D48@bitpowder.com>
References:  <1BDAF8C6-66A5-4F4F-A2CD-3928A0B10D48@bitpowder.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, Feb 14, 2012 at 07:25:14PM +0100, Bernard van Gastel wrote:
> Hi all,
> 
> I was looking through the sources of memmove at [1], and saw a (very) 
> small optimization opportunity. The 'memcmp' also compares the current
> character, but the current character is already checked (first part of
> the condition). As we already know the size of the string is greater as
> 1 (it is checked before the loop), it is possible to replace the memcmp
> with (possible doing the decrease of s_len once outside the loop):
> 	memcpy(&cur[1], &cs[1], s_len-1)

You are right, also second argument for memcpy() also can be calculated
outside of the loop (remembering first character from this buffer is
required).  One can find similar optimization in other simple memmem()
implementations.  (search memmem on http://code.google.com/ for example).

> Am I missing something? E.g. is readability more important as speed?
> This made me wonder what generally the tradeoff is that has to be taken
> into account in these cases?

Looks like it will give a little improvement.  If one want to use
such function for huge data, then more advanced string searching algorithms
are required.

> [1] http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libc/string/memmem.c?rev=1.2 , excerpt below:
> for (cur = (char *)cl; cur <= last; cur++)
>     if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
>        return cur;




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20120215122225.GA34935>