From owner-freebsd-hackers@FreeBSD.ORG Wed Feb 15 10:33:27 2012 Return-Path: Delivered-To: hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id F2D681065675 for ; Wed, 15 Feb 2012 10:33:27 +0000 (UTC) (envelope-from bvgastel@bitpowder.com) Received: from smtp10.mail.sp.isp-net.nl (smtp10.mail.sp.isp-net.nl [217.149.192.65]) by mx1.freebsd.org (Postfix) with ESMTP id 8B85C8FC0C for ; Wed, 15 Feb 2012 10:33:27 +0000 (UTC) Received: from bitpowder.com by smtp10.mail.sp.isp-net.nl via 28-53-223.ftth.xms.internl.net [85.223.53.28] with ESMTP for id q1EIPGvS022686 (8.13.2/2.04); Tue, 14 Feb 2012 19:25:16 +0100 (MET) Received: from [10.0.42.11] (localhost [127.0.0.1]) by bitpowder.com (Postfix) with ESMTPA id AEE0A18E8FD for ; Tue, 14 Feb 2012 18:25:15 +0000 (UTC) From: Bernard van Gastel Content-Type: multipart/signed; boundary="Apple-Mail=_A1EFFEDA-2AAF-4165-9F13-C572310F6928"; protocol="application/pkcs7-signature"; micalg=sha1 Date: Tue, 14 Feb 2012 19:25:14 +0100 Message-Id: <1BDAF8C6-66A5-4F4F-A2CD-3928A0B10D48@bitpowder.com> To: hackers@freebsd.org Mime-Version: 1.0 (Apple Message framework v1257) X-Mailer: Apple Mail (2.1257) X-Language-Detected: en X-Spam-Scanned: InterNLnet Mail Scan System V2.03 X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: Subject: memmem small optimalisation X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Feb 2012 10:33:28 -0000 --Apple-Mail=_A1EFFEDA-2AAF-4165-9F13-C572310F6928 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii 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=3D1= .2 , excerpt below: for (cur =3D (char *)cl; cur <=3D last; cur++) if (cur[0] =3D=3D cs[0] && memcmp(cur, cs, s_len) =3D=3D 0) return cur; --- Bernard van Gastel Bit Powder RSS Junkie? Download the app with push notifications for Android: = https://market.android.com/details?id=3Dcom.bitpowder.rssjunkie= --Apple-Mail=_A1EFFEDA-2AAF-4165-9F13-C572310F6928--