Skip to contentSkip to conversation

small update for strlen, strnlen, use less cpu instructions

PATCH In review Orestes Leal Rodriguez 2 participants 2 messages Jun 16, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 small update for strlen, strnlen, use less cpu instructions

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.

string.c renamed from string.c.orig +13 -8
@@ -17,6 +17,10 @@
  * * Sat Feb 09 2002, Jason Thomas <[email protected]>,
  *                    Matthew Hawkins <[email protected]>
  * -  Kissed strtok() goodbye
+ *
+ * * Tuesday June 16 2015, Orestes Leal Rodriguez <[email protected]>
+ * - 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
 
#1 lore raw
Quoted 3 lines · 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

Signature · 1 line
Mihai Donțu