public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Timothy Miller <miller@techsource.com>
To: Peter Kjellerstedt <peter.kjellerstedt@axis.com>
Cc: "'Willy Tarreau'" <willy@w.ods.org>,
	linux-kernel mailing list <linux-kernel@vger.kernel.org>
Subject: Re: generic strncpy - off-by-one error
Date: Mon, 18 Aug 2003 12:06:39 -0400	[thread overview]
Message-ID: <3F40F98F.8060103@techsource.com> (raw)
In-Reply-To: D069C7355C6E314B85CF36761C40F9A42E20BB@mailse02.se.axis.com



Peter Kjellerstedt wrote:

> 
> For loops       2.867568    5.620561    8.128734   28.286289  
> Multi byte fill 2.868031    5.670782    6.312027   11.336015  
> 
> And here are the numbers for my P4:
> 
> For loops       3.060262    5.927378    8.796814   30.659818  
> Multi byte fill 3.126607    5.898459    7.096685   13.135379  
> 
> So there is no doubt that the multi byte version is a clear
> winner (which was expected, I suppose).

Cool!  Hey, is this just an exercise, or are we actually going to use 
this?  I would be very happy to have something I contributed to put into 
the kernel.  :)

> 
> Here is the code that I used:
> 
> char *strncpy(char *dest, const char *src, size_t count)
> {
> 	char *tmp = dest;
> 
> 	while (count && *src) {
> 		*tmp++ = *src++;
> 		count--;
> 	}
> 
> 	if (count) {

Good idea... bad to do so many checks if count is zero.  On the other 
hand, if count is rarely zero, then it's a loss.  Maybe benchmark with 
and without?

> 		size_t count2;
> 
> 		while (count & (sizeof(long) - 1)) {
> 			*tmp++ = '\0';
> 			count--;
> 		}
> 
> 		count2 = count / sizeof(long);

I know that a good compiler should migrate code to help the CPU 
pipeline, but how about moving this "count2 = " line up to before the 
first fill loop.  See if that helps any.  Always good to precompute well 
in advance.

> 		while (count2) {
> 			*((long *)tmp)++ = '\0';
> 			count2--;
> 		}
> 
> 		count &= (sizeof(long) - 1);

And move this to before the middle fill loop.

> 		while (count) {
> 			*tmp++ = '\0';
> 			count--;
> 		}
> 	}
> 
> 	return dest;
> }
> 
> //Peter
> 
> 



  parent reply	other threads:[~2003-08-18 15:52 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-08-16  8:15 generic strncpy - off-by-one error Peter Kjellerstedt
2003-08-16  8:41 ` Daniel Forrest
2003-08-18 16:17   ` Timothy Miller
2003-08-18 16:06 ` Timothy Miller [this message]
  -- strict thread matches above, loose matches on Subject: below --
2003-08-20  7:43 Peter Kjellerstedt
2003-08-16 21:10 Peter Kjellerstedt
2003-08-18 18:41 ` Timothy Miller
2003-08-16 20:08 Peter Kjellerstedt
2003-08-16  9:19 Peter Kjellerstedt
2003-08-16 10:04 ` Daniel Forrest
2003-08-18 16:40   ` Timothy Miller
2003-08-15  9:54 Peter Kjellerstedt
2003-08-15 17:52 ` Timothy Miller
2003-08-15  9:53 Peter Kjellerstedt
2003-08-15 17:47 ` Timothy Miller
2003-08-14  9:34 Peter Kjellerstedt
2003-08-14 19:45 ` Willy Tarreau
2003-08-14 20:24 ` Timothy Miller
2003-08-13  3:09 Anthony Truong
2003-08-13  2:18 Albert Cahalan
2003-08-13  2:47 ` Erik Andersen
2003-08-13  3:38   ` Albert Cahalan
2003-08-13  3:56     ` Nick Piggin
2003-08-13  5:18     ` Willy Tarreau
2003-08-13 19:03   ` Timothy Miller
2003-08-12 14:07 Yoshinori Sato
2003-08-12 14:39 ` Willy Tarreau
2003-08-12 14:50 ` Yoshinori Sato
2003-08-12 15:03   ` Valdis.Kletnieks
2003-08-12 15:54     ` William Gallafent
2003-08-12 16:19       ` Valdis.Kletnieks
2003-08-12 16:09     ` Andreas Schwab
2003-08-12  1:56 Anthony Truong
2003-08-12 17:14 ` Alan Cox
2003-08-12 21:53   ` Bernd Petrovitsch
2003-08-12  1:28 Anthony Truong
2003-08-12 16:24 ` 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=3F40F98F.8060103@techsource.com \
    --to=miller@techsource.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peter.kjellerstedt@axis.com \
    --cc=willy@w.ods.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