public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Improve performance of strncpy
@ 2014-08-20 12:44 Wilco Dijkstra
  2014-08-22 12:02 ` Adhemerval Zanella
  0 siblings, 1 reply; 14+ messages in thread
From: Wilco Dijkstra @ 2014-08-20 12:44 UTC (permalink / raw)
  To: libc-alpha

[-- Attachment #1: Type: text/plain, Size: 395 bytes --]

Hi,

This patch improves strncpy performance by using memset to clear memory after the string when the
buffer is much larger than the copied string. This is better as memset is significantly faster than
a simple byte-loop. On bench-strncpy it is ~25% faster.

ChangeLog:
2014-08-20  Wilco Dijkstra  <wdijkstr@arm.com>

	* string/strncpy.c (strncpy): Improve performance by using memset.

[-- Attachment #2: Improve-performance-of-strncpy.txt --]
[-- Type: text/plain, Size: 523 bytes --]

---
 string/strncpy.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/string/strncpy.c b/string/strncpy.c
index 0915e03..604417c 100644
--- a/string/strncpy.c
+++ b/string/strncpy.c
@@ -78,9 +78,12 @@ STRNCPY (char *s1, const char *s2, size_t n)
   while (c != '\0');
 
  zero_fill:
-  do
-    *++s1 = '\0';
-  while (--n > 0);
+  if (n >= 8)
+    memset (s1 + 1, '\0', n);
+  else
+    do
+      *++s1 = '\0';
+    while (--n > 0);
 
   return s;
 }
-- 
1.7.9.5


^ permalink raw reply	[flat|nested] 14+ messages in thread
* RE: [PATCH] Improve performance of strncpy
@ 2014-09-10 15:21 Wilco Dijkstra
  2014-09-10 17:34 ` Florian Weimer
  0 siblings, 1 reply; 14+ messages in thread
From: Wilco Dijkstra @ 2014-09-10 15:21 UTC (permalink / raw)
  To: azanella; +Cc: libc-alpha

Adhemerval Zanella wrote:
> Hi, the patch looks ok. I also pushed a similar modification for powerpc based on same idea. 

>   zero_fill:
> -  do
> -    *++s1 = '\0';
> -  while (--n > 0);
> +  if (n >= 8)
> +    memset (s1 + 1, '\0', n);
> +  else
> +    do
> +      *++s1 = '\0';
> +    while (--n > 0);

> I wonder if this test is really worth, my opinion is just to keep it simple
> and just call memset on both 'goto' in loop and after 'last_chars'.

Yes, you're right, I timed it and there is actually little difference, while
the code is now even simpler. New version below (not attaching results in bad
characters due to various mail servers changing line endings).

OK for commit?

---
 string/strncpy.c |   14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/string/strncpy.c b/string/strncpy.c
index 0915e03..d5fa5be 100644
--- a/string/strncpy.c
+++ b/string/strncpy.c
@@ -57,10 +57,10 @@ STRNCPY (char *s1, const char *s2, size_t n)
 	  if (--n4 == 0)
 	    goto last_chars;
 	}
-      n = n - (s1 - s) - 1;
-      if (n == 0)
-	return s;
-      goto zero_fill;
+      s1++;
+      n = n - (s1 - s);
+      memset (s1, '\0', n);
+      return s;
     }
 
  last_chars:
@@ -77,11 +77,7 @@ STRNCPY (char *s1, const char *s2, size_t n)
     }
   while (c != '\0');
 
- zero_fill:
-  do
-    *++s1 = '\0';
-  while (--n > 0);
-
+  memset (s1 + 1, '\0', n);
   return s;
 }
 libc_hidden_builtin_def (strncpy)
-- 
1.7.9.5


^ permalink raw reply	[flat|nested] 14+ messages in thread
* RE: [PATCH] Improve performance of strncpy
@ 2014-10-24 15:56 Wilco Dijkstra
  2014-11-23 16:52 ` Ondřej Bílka
  0 siblings, 1 reply; 14+ messages in thread
From: Wilco Dijkstra @ 2014-10-24 15:56 UTC (permalink / raw)
  To: azanella; +Cc: libc-alpha

Ping (there was some further discussion but I don't see an OK for this patch)

> -----Original Message-----
> From: Wilco Dijkstra [mailto:wdijkstr@arm.com]
> Sent: 10 September 2014 16:22
> To: 'azanella@linux.vnet.ibm.com'
> Cc: 'libc-alpha@sourceware.org'
> Subject: RE: [PATCH] Improve performance of strncpy
> 
> Adhemerval Zanella wrote:
> > Hi, the patch looks ok. I also pushed a similar modification for powerpc based on same idea.
> 
> >   zero_fill:
> > -  do
> > -    *++s1 = '\0';
> > -  while (--n > 0);
> > +  if (n >= 8)
> > +    memset (s1 + 1, '\0', n);
> > +  else
> > +    do
> > +      *++s1 = '\0';
> > +    while (--n > 0);
> 
> > I wonder if this test is really worth, my opinion is just to keep it simple
> > and just call memset on both 'goto' in loop and after 'last_chars'.
> 
> Yes, you're right, I timed it and there is actually little difference, while
> the code is now even simpler. New version below (not attaching results in bad
> characters due to various mail servers changing line endings).
> 
> OK for commit?
> 
> ---
>  string/strncpy.c |   14 +++++---------
>  1 file changed, 5 insertions(+), 9 deletions(-)
> 
> diff --git a/string/strncpy.c b/string/strncpy.c
> index 0915e03..d5fa5be 100644
> --- a/string/strncpy.c
> +++ b/string/strncpy.c
> @@ -57,10 +57,10 @@ STRNCPY (char *s1, const char *s2, size_t n)
>  	  if (--n4 == 0)
>  	    goto last_chars;
>  	}
> -      n = n - (s1 - s) - 1;
> -      if (n == 0)
> -	return s;
> -      goto zero_fill;
> +      s1++;
> +      n = n - (s1 - s);
> +      memset (s1, '\0', n);
> +      return s;
>      }
> 
>   last_chars:
> @@ -77,11 +77,7 @@ STRNCPY (char *s1, const char *s2, size_t n)
>      }
>    while (c != '\0');
> 
> - zero_fill:
> -  do
> -    *++s1 = '\0';
> -  while (--n > 0);
> -
> +  memset (s1 + 1, '\0', n);
>    return s;
>  }
>  libc_hidden_builtin_def (strncpy)
> --
> 1.7.9.5


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2014-11-27 20:51 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-20 12:44 [PATCH] Improve performance of strncpy Wilco Dijkstra
2014-08-22 12:02 ` Adhemerval Zanella
2014-09-10 15:21 Wilco Dijkstra
2014-09-10 17:34 ` Florian Weimer
2014-09-10 18:02   ` Rich Felker
2014-09-10 18:25     ` Wilco Dijkstra
2014-09-11 19:37     ` Wilco Dijkstra
2014-09-12  6:22       ` Ondřej Bílka
2014-09-12 11:04         ` Wilco Dijkstra
2014-09-10 18:09   ` Wilco Dijkstra
2014-10-24 15:56 Wilco Dijkstra
2014-11-23 16:52 ` Ondřej Bílka
2014-11-27 19:20   ` Wilco Dijkstra
2014-11-27 20:51     ` Ondřej Bílka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).