memmem small optimalisation

Bernard van Gastel bvgastel at bitpowder.com
Wed Feb 15 10:33:28 UTC 2012


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?

Regards,
  Bernard

[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;


---
Bernard van Gastel
Bit Powder
RSS Junkie? Download the app with push notifications for Android: https://market.android.com/details?id=com.bitpowder.rssjunkie


More information about the freebsd-hackers mailing list