public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] smaller strlen()
@ 2016-08-26 20:01 Alexey Dobriyan
  2016-08-26 22:23 ` Joe Perches
  0 siblings, 1 reply; 2+ messages in thread
From: Alexey Dobriyan @ 2016-08-26 20:01 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel

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

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

* Re: [PATCH] smaller strlen()
  2016-08-26 20:01 [PATCH] smaller strlen() Alexey Dobriyan
@ 2016-08-26 22:23 ` Joe Perches
  0 siblings, 0 replies; 2+ messages in thread
From: Joe Perches @ 2016-08-26 22:23 UTC (permalink / raw)
  To: Alexey Dobriyan, akpm; +Cc: linux-kernel, Andi Kleen

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 :^)

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

end of thread, other threads:[~2016-08-26 22:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-26 20:01 [PATCH] smaller strlen() Alexey Dobriyan
2016-08-26 22:23 ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox