i386: Remove string functions that use 'rep scasb'
PATCH 1/1 i386: Remove string functions that use 'rep scasb'
From: David Laight <[email protected]>
The fixed overhead of all the 'rep xxx' instructions is rather more that might expect.
While 'rep movs' is getting better on more recent CPU, the same is not true for 'rep scasb'.
On my Zen-5 it has a fixed overhead of 150 clocks and then takes 3 clocks for each byte.
I've not measured any Intel CPU, but the cost might be 'only' 40 + 2n.
Remove the asm versions of strcat() strncat() strlen() memchr() and memscan(), the generic C versions will be faster.
It is quite likely that all these functions are slower than the generic code on pretty much all CPU since the 486.
--- arch/x86/include/asm/string_32.h | 18 ------- arch/x86/lib/string_32.c | 89 -------------------------------- 2 files changed, 107 deletions(-)
arch/x86/include/asm/string_32.h +0 -18
@@ -12,12 +12,6 @@ extern char *strcpy(char *dest, const char *src);#define __HAVE_ARCH_STRNCPYextern char *strncpy (char *dest,const char *src,size_t count); -#define __HAVE_ARCH_STRCAT -extern char *strcat (char *dest,const char *src); - -#define __HAVE_ARCH_STRNCAT -extern char *strncat (char *dest,const char *src,size_t count); -#define __HAVE_ARCH_STRCMPextern int strcmp (const char *cs,const char *ct);
@@ -27,9 +21,6 @@ extern int strncmp(const char *cs, const char *ct, size_t count);#define __HAVE_ARCH_STRCHRextern char *strchr (const char *s,int c); -#define __HAVE_ARCH_STRLEN -extern size_t strlen (const char *s); - static __always_inline void *__memcpy(void *to, const void *from, size_t n) {int d0, d1, d2;
@@ -159,9 +150,6 @@ extern int memcmp(const void *, const void *, size_t);#define memcmp __builtin_memcmp#endif -#define __HAVE_ARCH_MEMCHR -extern void *memchr (const void *cs,int c,size_t count); -static inline void *__memset_generic (void *s,char c,size_t count) {int d0, d1;
@@ -216,12 +204,6 @@ static inline void *memset32(uint32_t *s, uint32_t v, size_t n)return s; } -/* -* find the first occurrence of byte 'c', or 1 past the area if none -*/ -#define __HAVE_ARCH_MEMSCAN -extern void *memscan (void *addr,int c,size_t size); -#endif /* __KERNEL__ */ #endif /* _ASM_X86_STRING_32_H */
arch/x86/lib/string_32.c +0 -89
101 lines not on this page · raw message
Signature · 1 line
2.39.5
Quoted 6 lines · On 3/27/26 12:57, [email protected] wrote:
The fixed overhead of all the 'rep xxx' instructions is rather more that might expect. While 'rep movs' is getting better on more recent CPU, the same is not true for 'rep scasb'. On my Zen-5 it has a fixed overhead of 150 clocks and then takes 3 clocks for each byte. I've not measured any Intel CPU, but the cost might be 'only' 40 + 2n.
One measurement on a modern 64-bit CPU isn't super convincing to me.
Quoted 5 lines
Remove the asm versions of
strcat()strncat()strlen()memchr()andmemscan(), the generic C versions will be faster.It is quite likely that all these functions are slower than the generic code on pretty much all CPU since the 486.
This is rather handwavy for my taste.
There seem to be two valid paths here:
1. We continue the "nobody cares about 32-bit" refrain. This removes a bunch of 32-bit-only code and complexity. If it causes a performance regression, we do not care much.
2. Someone makes _some_ kind of effort to test this on at least *one* 32-bit-only CPU to see if it does any harm.
In other words, I'm not opposed to the patch, but the justification doesn't really work for me as written.
Quoted 21 lines · Dave Hansen
Quoted 6 lines · On 3/27/26 12:57, [email protected] wrote:
The fixed overhead of all the 'rep xxx' instructions is rather more that might expect. While 'rep movs' is getting better on more recent CPU, the same is not true for 'rep scasb'. On my Zen-5 it has a fixed overhead of 150 clocks and then takes 3 clocks for each byte. I've not measured any Intel CPU, but the cost might be 'only' 40 + 2n.
One measurement on a modern 64-bit CPU isn't super convincing to me.
Quoted 5 lines
Remove the asm versions of
strcat()strncat()strlen()memchr()andmemscan(), the generic C versions will be faster.It is quite likely that all these functions are slower than the generic code on pretty much all CPU since the 486.
This is rather handwavy for my taste.
There seem to be two valid paths here:
Quoted 8 lines
1. We continue the "nobody cares about 32-bit" refrain. This removes a bunch of 32-bit-only code and complexity. If it causes a performance regression, we do not care much.
2. Someone makes _some_ kind of effort to test this on at least *one* 32-bit-only CPU to see if it does any harm.In other words, I'm not opposed to the patch, but the justification doesn't really work for me as written.
I have Intel Quark at hand to test. But I need to know the step-by-step instructions on what to do.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 28 lines · On Mon, 30 Mar 2026 20:21:41 +0300 Andy Shevchenko <[email protected]>…
Quoted 27 lines · Dave Hansen
Quoted 6 lines · On 3/27/26 12:57, [email protected] wrote:
The fixed overhead of all the 'rep xxx' instructions is rather more that might expect. While 'rep movs' is getting better on more recent CPU, the same is not true for 'rep scasb'. On my Zen-5 it has a fixed overhead of 150 clocks and then takes 3 clocks for each byte. I've not measured any Intel CPU, but the cost might be 'only' 40 + 2n.
One measurement on a modern 64-bit CPU isn't super convincing to me.
Quoted 5 lines
Remove the asm versions of
strcat()strncat()strlen()memchr()andmemscan(), the generic C versions will be faster.It is quite likely that all these functions are slower than the generic code on pretty much all CPU since the 486.
This is rather handwavy for my taste.
There seem to be two valid paths here:
1. We continue the "nobody cares about 32-bit" refrain. This removes a bunch of 32-bit-only code and complexity. If it causes a performance regression, we do not care much.
2. Someone makes _some_ kind of effort to test this on at least *one* 32-bit-only CPU to see if it does any harm.
Quoted 6 lines
Quoted 2 lines
In other words, I'm not opposed to the patch, but the justification doesn't really work for me as written.
I have Intel Quark at hand to test. But I need to know the step-by-step instructions on what to do.
I can run my test on a few 'older' systems, but I don't have anything Intel before Sandy bridge and only an AMD 'Excavator' (or similar).
I do remember (a long time ago) getting my Athlon 700 to run a copy loop as fast as 'rep movl' - but the setup time was a lot worse. So I suspect that generation of cpu didn't have a large overhead. If I've read Agner's tables he gives a 40 clock setup to P-II onwards.
I can give you the source of the test I've been using.
David
Quoted 2 lines · David Laight
I have Intel Quark at hand to test. But I need to know the step-by-step instructions on what to do.
I'll take it if it's all that we have, but Quark is really weird. It's probably Intel's last sold 32-bit-only CPU, but it wasn't used for anything remotely performance sensitive, it's more like a 1995 CPU than a 2010 CPU, and Intel probably sold like twenty of them. ;)
But, seriously, we don't need to go digging in the junk heap for performance numbers. If nobody has one handy, it's just extra justification for "we don't care".
But let's just say *THAT* instead of doing some kind of performance theater where we pretend that like every cycle on CPUs from 2003 matters on a 2026 kernel, and that we even cared enough to measure it.
Quoted 8 lines · Dave Hansen
Quoted 2 lines
I have Intel Quark at hand to test. But I need to know the step-by-step instructions on what to do.
I'll take it if it's all that we have, but Quark is really weird. It's probably Intel's last sold 32-bit-only CPU, but it wasn't used for anything remotely performance sensitive, it's more like a 1995 CPU than a 2010 CPU, and Intel probably sold like twenty of them. ;)
Quoted 7 lines
But, seriously, we don't need to go digging in the junk heap for performance numbers. If nobody has one handy, it's just extra justification for "we don't care".
But let's just say *THAT* instead of doing some kind of performance theater where we pretend that like every cycle on CPUs from 2003 matters on a 2026 kernel, and that we even cared enough to measure it.
FWIW I can benchmark on a genuine i486 or Pentium MMX system right away,
but I'm more concerned about support being dropped altogether rather than squeezing out any extra cycles from these boxes at this point. If anyone runs such equipment for performance nowadays, they must clearly be mad or have missed something.
Maciej
Quoted 18 lines · Maciej W. Rozycki
Quoted 8 lines · Dave Hansen
Quoted 2 lines
I have Intel Quark at hand to test. But I need to know the step-by-step instructions on what to do.
I'll take it if it's all that we have, but Quark is really weird. It's probably Intel's last sold 32-bit-only CPU, but it wasn't used for anything remotely performance sensitive, it's more like a 1995 CPU than a 2010 CPU, and Intel probably sold like twenty of them. ;)
Quoted 7 lines
But, seriously, we don't need to go digging in the junk heap for performance numbers. If nobody has one handy, it's just extra justification for "we don't care".
But let's just say *THAT* instead of doing some kind of performance theater where we pretend that like every cycle on CPUs from 2003 matters on a 2026 kernel, and that we even cared enough to measure it.
Quoted 5 lines
FWIW I can benchmark on a genuine i486 or Pentium MMX system right away,
but I'm more concerned about support being dropped altogether rather than squeezing out any extra cycles from these boxes at this point. If anyone runs such equipment for performance nowadays, they must clearly be mad or have missed something.
It makes sense for people who want a tiny x86 core running something as fast as they can with all the benefits from that small core. Intel Quark was designed for power and efficiency for the embedded world, having slightly better performance is not a bad idea.
Signature · 2 lines
With Best Regards, Andy Shevchenko