public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Adrian Bunk <bunk@stusta.de>
To: Folkert van Heusden <folkert@vanheusden.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: optimalisation for strlcpy (lib/string.c)
Date: Mon, 11 Dec 2006 00:13:05 +0100	[thread overview]
Message-ID: <20061210231305.GG10351@stusta.de> (raw)
In-Reply-To: <20061210212350.GC30197@vanheusden.com>

On Sun, Dec 10, 2006 at 10:23:51PM +0100, Folkert van Heusden wrote:
> Hi,
> 
> Like the other patch (by that other person), I think it is faster to not
> do a strlen first.
>...
> --- lib/string.c        2006-11-04 02:33:58.000000000 +0100
> +++ string-new.c        2006-12-10 22:22:08.000000000 +0100
> @@ -121,14 +121,24 @@
>   */
>  size_t strlcpy(char *dest, const char *src, size_t size)
>  {
> -       size_t ret = strlen(src);
> +        char *tmp = dest;
> 
> -       if (size) {
> -               size_t len = (ret >= size) ? size - 1 : ret;
> -               memcpy(dest, src, len);
> -               dest[len] = '\0';
> +        for(;;)
> +        {
> +                *dest = *src;
> +                if (!*src)
> +                        break;
> +
> +                if (--size == 0)
> +                        break;
> +
> +                dest++;
> +                src++;
>         }
> -       return ret;
> +
> +        *dest = 0x00;
> +
> +        return dest - tmp;
>...

Two bugs in your code:
- you copy a maximum of size bytes _plus_ \0
- size == 0 is no longer handled correctly

> I've tested the speed difference with this:
> http://www.vanheusden.com/misc/kernel-strlcpy-opt-test.c
> and the speed difference is quite a bit on a P4: 28% faster.
>...

My Athlon says:
org: 2.400000
new: 6.710000

IOW, your version is much slower.

But the main question is actually:
Does the performance of this function matter anywhere inside the kernel?
Is strlcpy() used in any fast path?
If not, there's no point in trying to optimize it.

> Folkert van Heusden

cu
Adrian

-- 

       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed


  parent reply	other threads:[~2006-12-10 23:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-10 21:23 optimalisation for strlcpy (lib/string.c) Folkert van Heusden
2006-12-10 21:49 ` Eyal Lebedinsky
2006-12-10 22:03   ` Folkert van Heusden
2006-12-10 21:55 ` Mitchell Blank Jr
2006-12-10 21:41   ` Folkert van Heusden
2006-12-10 23:13 ` Adrian Bunk [this message]
2006-12-11  5:26   ` Clemens Koller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20061210231305.GG10351@stusta.de \
    --to=bunk@stusta.de \
    --cc=folkert@vanheusden.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox