From owner-freebsd-hackers@FreeBSD.ORG Fri Feb 24 01:34:04 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 57FF71065674 for ; Fri, 24 Feb 2012 01:34:04 +0000 (UTC) (envelope-from delphij@delphij.net) Received: from anubis.delphij.net (anubis.delphij.net [IPv6:2001:470:1:117::25]) by mx1.freebsd.org (Postfix) with ESMTP id 280B68FC1A for ; Fri, 24 Feb 2012 01:34:04 +0000 (UTC) Received: from delta.delphij.net (drawbridge.ixsystems.com [206.40.55.65]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by anubis.delphij.net (Postfix) with ESMTPSA id D0BC4FD54; Thu, 23 Feb 2012 17:34:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=delphij.net; s=anubis; t=1330047243; bh=axC1BKHb4pvUL8FuM6TryvB6rG8sp0EHTE1zd5uodiY=; h=Message-ID:Date:From:Reply-To:MIME-Version:To:CC:Subject: References:In-Reply-To:Content-Type:Content-Transfer-Encoding; b=AWVn3aXRd8oeJR5mfpaXdcurAkFFLH4FAUjEVULLDq3UmNwfnhnP35xfu9k00zdMd wpImwm2qMK+1aZeswxNTbV+khyUXYMoHypMOJ6z2Qxu0MX/nVlF8/a6vTJEThrdr5O AF43B2nikqQThbjeNTnhTcOH9AOUhMd9XEkzxLOs= Message-ID: <4F46E90B.2050407@delphij.net> Date: Thu, 23 Feb 2012 17:34:03 -0800 From: Xin Li Organization: The FreeBSD Project MIME-Version: 1.0 To: Bernard van Gastel References: <1BDAF8C6-66A5-4F4F-A2CD-3928A0B10D48@bitpowder.com> In-Reply-To: <1BDAF8C6-66A5-4F4F-A2CD-3928A0B10D48@bitpowder.com> X-Enigmail-Version: 1.3.5 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Cc: hackers@freebsd.org, d@delphij.net Subject: Re: memmem small optimalisation X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: d@delphij.net List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 24 Feb 2012 01:34:04 -0000 -----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 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-----