Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 23 Feb 2012 17:34:03 -0800
From:      Xin Li <delphij@delphij.net>
To:        Bernard van Gastel <bvgastel@bitpowder.com>
Cc:        hackers@freebsd.org, d@delphij.net
Subject:   Re: memmem small optimalisation
Message-ID:  <4F46E90B.2050407@delphij.net>
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
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Hi,

On 02/14/12 10:25, 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)
> 
> 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?

Did you benchmarked the change?  Changes like this has to be done very
carefully since it's possible that the extra time spent on addition
and subtractions, when multiple by the length of the "long" string,
may actually defeat the benefit of one less byte worth memory compare
because the effect of CPU's data cache.

I think it would be worthy to explore if we could use a more advanced
algorithm here by the way.

Cheers,
- -- 
Xin LI <delphij@delphij.net>	https://www.delphij.net/
FreeBSD - The Power to Serve!		Live free or die
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.18 (FreeBSD)

iQEcBAEBCAAGBQJPRukLAAoJEG80Jeu8UPuzKlcIAJP+1tL4HfHfAOou39azwBwC
DvTOw6XJziv3Pau0GmemE27cWRYHJDGiUzUI95IPFuoCfF/UuHwA8FV5x+wo7dId
3sYyNsqc2Hk9HGw0O5SifaJ5182/2jdJGbMVLl7z1tRlw29HRjubYnvAEizvrMpa
IE0O2kvm9LhRlpy8Grnd4sa8WxJ4AGUpCDcAGcDrqM+GcDC5jvd+qO5HgD/yRNqd
sXzNfDXtY6vIPND4hyLMJpsTTReTfsWJGL8pYI8T9IqK+/vy2+AzalL9FKoBgOAE
z1qkl565J6S4au6v60smNNb0+rIvrPoQrYs8ot4Dl2g8vMKDetj7rGTH3zorKvM=
=2Nw3
-----END PGP SIGNATURE-----



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