linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] small update for strlen, strnlen, use less cpu instructions
@ 2015-06-16  9:28 Orestes Leal Rodriguez
  2015-06-16 10:37 ` Richard Weinberger
  0 siblings, 1 reply; 10+ messages in thread
From: Orestes Leal Rodriguez @ 2015-06-16  9:28 UTC (permalink / raw)
  To: linux-kernel

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

very small update to strlen and strnlen that now use less cpu 
instructions by using a counter to avoid the memory addresses 
substraction to find the length of the string.

Orestes Leal Rodriguez.


[-- Attachment #2: string.c.patch --]
[-- Type: text/plain, Size: 1140 bytes --]

--- lib/string.c.orig	2015-06-15 23:59:32.768346193 -0400
+++ lib/string.c	2015-06-16 00:11:45.791381539 -0400
@@ -17,6 +17,10 @@
  * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
  *                    Matthew Hawkins <matt@mh.dropbear.id.au>
  * -  Kissed strtok() goodbye
+ *
+ * * Tuesday June 16 2015, Orestes Leal Rodriguez <lukes357@gmail.com>
+ * - strlen, strnlen: by using a single counter we use less cpu instructions
+ *   by avoiding substracting the memory addresses before return
  */
 
 #include <linux/types.h>
@@ -401,11 +405,11 @@ EXPORT_SYMBOL(strim);
  */
 size_t strlen(const char *s)
 {
-	const char *sc;
+	size_t sz = 0;
 
-	for (sc = s; *sc != '\0'; ++sc)
-		/* nothing */;
-	return sc - s;
+	for (; *s++ != '\0'; sz++)
+		/*empty */;
+	return sz;
 }
 EXPORT_SYMBOL(strlen);
 #endif
@@ -418,12 +422,13 @@ EXPORT_SYMBOL(strlen);
  */
 size_t strnlen(const char *s, size_t count)
 {
-	const char *sc;
+	size_t sz = 0;
 
-	for (sc = s; count-- && *sc != '\0'; ++sc)
-		/* nothing */;
-	return sc - s;
+	for (; count-- && *s++ != '\0'; sz++)
+		/* empty */;
+	return sz;
 }
+
 EXPORT_SYMBOL(strnlen);
 #endif
 

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

* Re: [PATCH] small update for strlen, strnlen, use less cpu instructions
  2015-06-16  9:28 [PATCH] small update for strlen, strnlen, use less cpu instructions Orestes Leal Rodriguez
@ 2015-06-16 10:37 ` Richard Weinberger
  2015-06-16 18:56   ` Luke Skywalker
  0 siblings, 1 reply; 10+ messages in thread
From: Richard Weinberger @ 2015-06-16 10:37 UTC (permalink / raw)
  To: Orestes Leal Rodriguez; +Cc: LKML

On Tue, Jun 16, 2015 at 11:28 AM, Orestes Leal Rodriguez
<lukes357@gmail.com> wrote:
> very small update to strlen and strnlen that now use less cpu instructions
> by using a counter to avoid the memory addresses substraction to find the
> length of the string.

This is not a valid patch submission. Please see
Documentation/SubmittingPatches.

Anyway, why do you think your patch makes strlen() better?
Now strlen() increments a variable for each character it faces, hence
it will consume more cycles.

-- 
Thanks,
//richard

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

* Re: [PATCH] small update for strlen, strnlen, use less cpu instructions
  2015-06-16 10:37 ` Richard Weinberger
@ 2015-06-16 18:56   ` Luke Skywalker
  2015-06-16 19:06     ` Darth Vader
  0 siblings, 1 reply; 10+ messages in thread
From: Luke Skywalker @ 2015-06-16 18:56 UTC (permalink / raw)
  To: Richard Weinberger; +Cc: LKML

> This is not a valid patch submission. Please see
> Documentation/SubmittingPatches.

I resent the patch an hour ago based on that document, thanks for
pointing this out.
I need to know if the patch is in good shape now, thanks

>
> Anyway, why do you think your patch makes strlen() better?
> Now strlen() increments a variable for each character it faces, hence
> it will consume more cycles.

you can look at the output of objdump -d to see why, also the assembly
snippet send by another person make this clear.

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

* Re: [PATCH] small update for strlen, strnlen, use less cpu instructions
  2015-06-16 18:56   ` Luke Skywalker
@ 2015-06-16 19:06     ` Darth Vader
  0 siblings, 0 replies; 10+ messages in thread
From: Darth Vader @ 2015-06-16 19:06 UTC (permalink / raw)
  To: Luke Skywalker; +Cc: Richard Weinberger, LKML

Luke,

On Tue, Jun 16, 2015 at 01:56:52PM -0500, Luke Skywalker wrote:
> > This is not a valid patch submission. Please see
> > Documentation/SubmittingPatches.
> 
> I resent the patch an hour ago based on that document, thanks for
> pointing this out.
> I need to know if the patch is in good shape now, thanks

It is not yet.

Use the force^Wcheckpatch.pl.

> you can look at the output of objdump -d to see why, also the assembly
> snippet send by another person make this clear.

You need to explain that in the commit message, my young padawan.

Then you can join me. Together we can rule the galaxy like father and
son.

Your father,
Darth.

---
Sent from the Death Star.

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

* Re: [PATCH] small update for strlen, strnlen, use less cpu instructions
  2015-06-16 11:41 Alexey Dobriyan
@ 2015-06-16 11:43 ` Richard Weinberger
  0 siblings, 0 replies; 10+ messages in thread
From: Richard Weinberger @ 2015-06-16 11:43 UTC (permalink / raw)
  To: Alexey Dobriyan; +Cc: lukes357, Linux Kernel

Am 16.06.2015 um 13:41 schrieb Alexey Dobriyan:
>> Now strlen() increments a variable for each character it faces,
>> hence it will consume more cycles.
> 
> It doesn't matter if there is a dependency in a loop:
> 
> Before:
>  520:   48 83 c0 01             add    $0x1,%rax
>  524:   80 38 00                cmpb   $0x0,(%rax)
>  527:   75 f7                   jne    520
> 
> After:
>  500:   48 83 c0 01             add    $0x1,%rax
>  504:   80 3c 07 00             cmpb   $0x0,(%rdi,%rax,1)
>  508:   75 f6                   jne    500
> 

Hmmm.
Thanks for pointing this out!

Thanks,
//richard

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

* Re: [PATCH] small update for strlen, strnlen, use less cpu instructions
@ 2015-06-16 11:41 Alexey Dobriyan
  2015-06-16 11:43 ` Richard Weinberger
  0 siblings, 1 reply; 10+ messages in thread
From: Alexey Dobriyan @ 2015-06-16 11:41 UTC (permalink / raw)
  To: richard.weinberger; +Cc: lukes357, Linux Kernel

> Now strlen() increments a variable for each character it faces,
> hence it will consume more cycles.

It doesn't matter if there is a dependency in a loop:

Before:
 520:   48 83 c0 01             add    $0x1,%rax
 524:   80 38 00                cmpb   $0x0,(%rax)
 527:   75 f7                   jne    520

After:
 500:   48 83 c0 01             add    $0x1,%rax
 504:   80 3c 07 00             cmpb   $0x0,(%rdi,%rax,1)
 508:   75 f6                   jne    500

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

* Re: [PATCH] small update for strlen, strnlen, use less cpu instructions
  2015-06-16  9:40 Orestes Leal Rodriguez
@ 2015-06-16 10:03 ` Mihai Donțu
  0 siblings, 0 replies; 10+ messages in thread
From: Mihai Donțu @ 2015-06-16 10:03 UTC (permalink / raw)
  To: Orestes Leal Rodriguez; +Cc: linux-kernel

On Tue, 16 Jun 2015 04:40:55 -0500 Orestes Leal Rodriguez wrote:
> very small update to strlen and strnlen that now use less cpu 
> instructions by using a counter to avoid the memory addresses 
> substraction to find the length of the string.

Nice. :-)

 Performance counter stats for './strnlen-new':

          3.602591      task-clock (msec)         #    0.836 CPUs utilized          
                 4      context-switches          #    0.001 M/sec                  
                 2      cpu-migrations            #    0.555 K/sec                  
                48      page-faults               #    0.013 M/sec                  
         9,607,307      cycles                    #    2.667 GHz                    
   <not supported>      stalled-cycles-frontend  
   <not supported>      stalled-cycles-backend   
        23,612,424      instructions              #    2.46  insns per cycle        
         9,121,754      branches                  # 2531.998 M/sec                  
             5,409      branch-misses             #    0.06% of all branches        

       0.004311477 seconds time elapsed


 Performance counter stats for './strnlen-old':

          3.181354      task-clock (msec)         #    0.930 CPUs utilized          
                 1      context-switches          #    0.314 K/sec                  
                 1      cpu-migrations            #    0.314 K/sec                  
                47      page-faults               #    0.015 M/sec                  
         8,558,129      cycles                    #    2.690 GHz                    
   <not supported>      stalled-cycles-frontend  
   <not supported>      stalled-cycles-backend   
        23,577,237      instructions              #    2.75  insns per cycle        
         9,114,760      branches                  # 2865.057 M/sec                  
             4,298      branch-misses             #    0.05% of all branches        

       0.003419085 seconds time elapsed

However, you will need to create a proper patch:
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/plain/Documentation/SubmittingPatches

-- 
Mihai Donțu

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

* [PATCH] small update for strlen, strnlen, use less cpu instructions
@ 2015-06-16  9:40 Orestes Leal Rodriguez
  2015-06-16 10:03 ` Mihai Donțu
  0 siblings, 1 reply; 10+ messages in thread
From: Orestes Leal Rodriguez @ 2015-06-16  9:40 UTC (permalink / raw)
  To: linux-kernel

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

very small update to strlen and strnlen that now use less cpu 
instructions by using a counter to avoid the memory addresses 
substraction to find the length of the string.

Orestes Leal Rodriguez.




[-- Attachment #2: string.c.patch --]
[-- Type: text/plain, Size: 1142 bytes --]

--- lib/string.c.orig	2015-06-15 23:59:32.768346193 -0400
+++ lib/string.c	2015-06-16 00:11:45.791381539 -0400
@@ -17,6 +17,10 @@
  * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
  *                    Matthew Hawkins <matt@mh.dropbear.id.au>
  * -  Kissed strtok() goodbye
+ *
+ * * Tuesday June 16 2015, Orestes Leal Rodriguez <lukes357@gmail.com>
+ * - strlen, strnlen: by using a single counter we use less cpu instructions
+ *   by avoiding substracting the memory addresses before return
  */
 
 #include <linux/types.h>
@@ -401,11 +405,11 @@ EXPORT_SYMBOL(strim);
  */
 size_t strlen(const char *s)
 {
-	const char *sc;
+	size_t sz = 0;
 
-	for (sc = s; *sc != '\0'; ++sc)
-		/* nothing */;
-	return sc - s;
+	for (; *s++ != '\0'; sz++)
+		/*empty */;
+	return sz;
 }
 EXPORT_SYMBOL(strlen);
 #endif
@@ -418,12 +422,13 @@ EXPORT_SYMBOL(strlen);
  */
 size_t strnlen(const char *s, size_t count)
 {
-	const char *sc;
+	size_t sz = 0;
 
-	for (sc = s; count-- && *sc != '\0'; ++sc)
-		/* nothing */;
-	return sc - s;
+	for (; count-- && *s++ != '\0'; sz++)
+		/* empty */;
+	return sz;
 }
+
 EXPORT_SYMBOL(strnlen);
 #endif
 



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

* [PATCH] small update for strlen, strnlen, use less cpu instructions
@ 2015-06-16  9:36 Orestes Leal Rodriguez
  0 siblings, 0 replies; 10+ messages in thread
From: Orestes Leal Rodriguez @ 2015-06-16  9:36 UTC (permalink / raw)
  To: linux-kernel

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

very small update to strlen and strnlen that now use less cpu 
instructions by using a counter to avoid the memory addresses 
substraction to find the length of the string.

Orestes Leal Rodriguez.



[-- Attachment #2: string.c.patch --]
[-- Type: text/plain, Size: 1141 bytes --]

--- lib/string.c.orig	2015-06-15 23:59:32.768346193 -0400
+++ lib/string.c	2015-06-16 00:11:45.791381539 -0400
@@ -17,6 +17,10 @@
  * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
  *                    Matthew Hawkins <matt@mh.dropbear.id.au>
  * -  Kissed strtok() goodbye
+ *
+ * * Tuesday June 16 2015, Orestes Leal Rodriguez <lukes357@gmail.com>
+ * - strlen, strnlen: by using a single counter we use less cpu instructions
+ *   by avoiding substracting the memory addresses before return
  */
 
 #include <linux/types.h>
@@ -401,11 +405,11 @@ EXPORT_SYMBOL(strim);
  */
 size_t strlen(const char *s)
 {
-	const char *sc;
+	size_t sz = 0;
 
-	for (sc = s; *sc != '\0'; ++sc)
-		/* nothing */;
-	return sc - s;
+	for (; *s++ != '\0'; sz++)
+		/*empty */;
+	return sz;
 }
 EXPORT_SYMBOL(strlen);
 #endif
@@ -418,12 +422,13 @@ EXPORT_SYMBOL(strlen);
  */
 size_t strnlen(const char *s, size_t count)
 {
-	const char *sc;
+	size_t sz = 0;
 
-	for (sc = s; count-- && *sc != '\0'; ++sc)
-		/* nothing */;
-	return sc - s;
+	for (; count-- && *s++ != '\0'; sz++)
+		/* empty */;
+	return sz;
 }
+
 EXPORT_SYMBOL(strnlen);
 #endif
 


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

* [PATCH] small update for strlen, strnlen, use less cpu instructions
@ 2015-06-16  9:26 Luke Skywalker
  0 siblings, 0 replies; 10+ messages in thread
From: Luke Skywalker @ 2015-06-16  9:26 UTC (permalink / raw)
  To: linux-kernel

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

very small update to strlen and strnlen that now use less cpu 
instructions by
using a counter  avoiding the memory addresses substraction to find the 
length
of the string,

Orestes Leal Rodriguez.

[-- Attachment #2: string.c.patch --]
[-- Type: text/plain, Size: 1140 bytes --]

--- lib/string.c.orig	2015-06-15 23:59:32.768346193 -0400
+++ lib/string.c	2015-06-16 00:11:45.791381539 -0400
@@ -17,6 +17,10 @@
  * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
  *                    Matthew Hawkins <matt@mh.dropbear.id.au>
  * -  Kissed strtok() goodbye
+ *
+ * * Tuesday June 16 2015, Orestes Leal Rodriguez <lukes357@gmail.com>
+ * - strlen, strnlen: by using a single counter we use less cpu instructions
+ *   by avoiding substracting the memory addresses before return
  */
 
 #include <linux/types.h>
@@ -401,11 +405,11 @@ EXPORT_SYMBOL(strim);
  */
 size_t strlen(const char *s)
 {
-	const char *sc;
+	size_t sz = 0;
 
-	for (sc = s; *sc != '\0'; ++sc)
-		/* nothing */;
-	return sc - s;
+	for (; *s++ != '\0'; sz++)
+		/*empty */;
+	return sz;
 }
 EXPORT_SYMBOL(strlen);
 #endif
@@ -418,12 +422,13 @@ EXPORT_SYMBOL(strlen);
  */
 size_t strnlen(const char *s, size_t count)
 {
-	const char *sc;
+	size_t sz = 0;
 
-	for (sc = s; count-- && *sc != '\0'; ++sc)
-		/* nothing */;
-	return sc - s;
+	for (; count-- && *s++ != '\0'; sz++)
+		/* empty */;
+	return sz;
 }
+
 EXPORT_SYMBOL(strnlen);
 #endif
 

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

end of thread, other threads:[~2015-06-16 19:06 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-16  9:28 [PATCH] small update for strlen, strnlen, use less cpu instructions Orestes Leal Rodriguez
2015-06-16 10:37 ` Richard Weinberger
2015-06-16 18:56   ` Luke Skywalker
2015-06-16 19:06     ` Darth Vader
  -- strict thread matches above, loose matches on Subject: below --
2015-06-16 11:41 Alexey Dobriyan
2015-06-16 11:43 ` Richard Weinberger
2015-06-16  9:40 Orestes Leal Rodriguez
2015-06-16 10:03 ` Mihai Donțu
2015-06-16  9:36 Orestes Leal Rodriguez
2015-06-16  9:26 Luke Skywalker

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 NNTP newsgroup(s).