Skip to contentSkip to conversation

small update for strlen, strnlen, use less cpu instructions

PATCH In review Orestes Leal Rodriguez 3 participants 4 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 · Orestes Leal Rodriguez

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.

Signature · 2 lines
Thanks,
//richard
#2 lore raw
Quoted 2 lines

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

Quoted 4 lines

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.

#3 lore raw

Luke,

Quoted 6 lines · Luke Skywalker
Quoted 2 lines

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.

Quoted 2 lines

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.