All of lore.kernel.org
 help / color / mirror / Atom feed
From: Willy Tarreau <w@1wt.eu>
To: Folkert van Heusden <folkert@vanheusden.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: strncpy optimalisation? (lib/string.c)
Date: Sun, 10 Dec 2006 22:06:14 +0100	[thread overview]
Message-ID: <20061210210614.GD24090@1wt.eu> (raw)
In-Reply-To: <20061210205230.GB30197@vanheusden.com>

On Sun, Dec 10, 2006 at 09:52:30PM +0100, Folkert van Heusden wrote:
> Hi,
> 
> In lib/string.c we have:
> 
> char *strncpy(char *dest, const char *src, size_t count)
> {
>         char *tmp = dest;
> 
>         while (count) {
>                 if ((*tmp = *src) != 0)
>                         src++;
>                 tmp++;
>                 count--;
>         }
>         return dest;
> }
> 
> now I wonder isn't this ineffecient when strlen(src) < count? It would
> then, if I'm correct, iterate count-strlen(src) times doing useless
> increment/decrement. And since there are aprox. 580 instances in the
> 2.6.18.2 source, maybe some efficency can be won here.
> Wouldn't it be better to do:
>                 if ((*tmp = *src) == 0x00)
>                         break;
> 
> So that would be:
> --- lib/string.c	2006-11-04 02:33:58.000000000 +0100
> +++ string-new.c	2006-12-10 21:50:05.000000000 +0100
> @@ -97,8 +97,8 @@
>  	char *tmp = dest;
>  
>  	while (count) {
> -		if ((*tmp = *src) != 0)
> -			src++;
> +		if ((*tmp = *src) == 0x00)
> +			break;
>  		tmp++;
>  		count--;
>  	}

While your code is faster, it does not do exactly the same.
Original code completely pads the destination with zeroes,
while yours only adds the last zero. Your code does what
strncpy() is said to do, but maybe there's a particular
reason for it to behave differently in the kernel (helping
during debugging, or filling specific structs).

Just out of curiosity, have you tried to do a general
benchmark to check if original code eats much CPU ?

Regards,
Willy


  reply	other threads:[~2006-12-10 21:06 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-10 20:52 strncpy optimalisation? (lib/string.c) Folkert van Heusden
2006-12-10 21:06 ` Willy Tarreau [this message]
2006-12-10 21:35   ` Folkert van Heusden
2006-12-10 22:05     ` Mitchell Blank Jr
2006-12-10 21:49       ` Folkert van Heusden
2006-12-10 21:49   ` Mitchell Blank Jr
2006-12-10 21:39     ` [KJ] " Folkert van Heusden
2006-12-10 21:39       ` Folkert van Heusden
2006-12-10 23:28       ` [KJ] " Bernd Petrovitsch
2006-12-10 23:28         ` Bernd Petrovitsch

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=20061210210614.GD24090@1wt.eu \
    --to=w@1wt.eu \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.