Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1470977 > unrolled thread

[PATCH] smaller strlen()

Started byAlexey Dobriyan <adobriyan@gmail.com>
First post2016-08-26 22:10 +0200
Last post2016-08-27 00:30 +0200
Articles 2 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] smaller strlen() Alexey Dobriyan <adobriyan@gmail.com> - 2016-08-26 22:10 +0200
    Re: [PATCH] smaller strlen() Joe Perches <joe@perches.com> - 2016-08-27 00:30 +0200

#1470977 — [PATCH] smaller strlen()

FromAlexey Dobriyan <adobriyan@gmail.com>
Date2016-08-26 22:10 +0200
Subject[PATCH] smaller strlen()
Message-ID<savBU-3hs-9@gated-at.bofh.it>
gcc prefers "*s++" style code for some reason, doesn't unroll loop
condition check once. Kernel strings are small but they aren't of 0
length, so that additional branch was almost never taken.

	$ ./scripts/bloat-o-meter ../vmlinux-000 ../obj/vmlinux
	strlen         30      26      -4
	strlcpy        71      64      -7
	strlcat       120      99     -21

strlcpy() and strlcat() are collateral damage :^)

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

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

--- a/lib/string.c
+++ b/lib/string.c
@@ -476,11 +476,11 @@ EXPORT_SYMBOL(strim);
  */
 size_t strlen(const char *s)
 {
-	const char *sc;
+	const char *s0 = s;
 
-	for (sc = s; *sc != '\0'; ++sc)
+	while (*s++)
 		/* nothing */;
-	return sc - s;
+	return s - s0 - 1;
 }
 EXPORT_SYMBOL(strlen);
 #endif

[toc] | [next] | [standalone]


#1471020

FromJoe Perches <joe@perches.com>
Date2016-08-27 00:30 +0200
Message-ID<saxNn-4Bu-9@gated-at.bofh.it>
In reply to#1470977
On Fri, 2016-08-26 at 23:01 +0300, Alexey Dobriyan wrote:
> gcc prefers "*s++" style code for some reason, doesn't unroll loop
> condition check once. Kernel strings are small but they aren't of 0
> length, so that additional branch was almost never taken.

Hey Alexey.

Is this gcc version specific?

And I'm confused why there isn't an asm __HAVE_ARCH_STRLEN
version x86_64 strlen like x86_32 or if __builtin_strlen()
is or isn't used.  Maybe Andi Kleen knows/remembers (cc'd).

> 	$ ./scripts/bloat-o-meter ../vmlinux-000 ../obj/vmlinux
> 	strlen         30      26      -4
> 	strlcpy        71      64      -7
> 	strlcat       120      99     -21
> 
> strlcpy() and strlcat() are collateral damage :^)

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web