linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 1/1] lib/string: Use strchr() in strpbrk()
@ 2023-01-27 15:51 Andy Shevchenko
  2023-01-27 18:55 ` Kees Cook
  2023-01-28 14:51 ` David Laight
  0 siblings, 2 replies; 5+ messages in thread
From: Andy Shevchenko @ 2023-01-27 15:51 UTC (permalink / raw)
  To: Kees Cook, linux-kernel
  Cc: Andy Shevchenko, Rasmus Villemoes, Andrew Morton, Andy Shevchenko

Use strchr() instead of open coding it as it's done elsewhere in
the same file. Either we will have similar to what it was or possibly
better performance in case architecture implements its own strchr().

Memory wise on x86_64 bloat-o-meter shows the following

  Function           old     new   delta
  strsep             111     102      -9
  Total: Before=2763, After=2754, chg -0.33%

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 lib/string.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/lib/string.c b/lib/string.c
index 4fb566ea610f..3d55ef890106 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -480,13 +480,11 @@ EXPORT_SYMBOL(strcspn);
  */
 char *strpbrk(const char *cs, const char *ct)
 {
-	const char *sc1, *sc2;
+	const char *sc;
 
-	for (sc1 = cs; *sc1 != '\0'; ++sc1) {
-		for (sc2 = ct; *sc2 != '\0'; ++sc2) {
-			if (*sc1 == *sc2)
-				return (char *)sc1;
-		}
+	for (sc = cs; *sc != '\0'; ++sc) {
+		if (strchr(ct, *sc))
+			return (char *)sc;
 	}
 	return NULL;
 }
-- 
2.39.0


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

* Re: [PATCH v1 1/1] lib/string: Use strchr() in strpbrk()
  2023-01-27 15:51 [PATCH v1 1/1] lib/string: Use strchr() in strpbrk() Andy Shevchenko
@ 2023-01-27 18:55 ` Kees Cook
  2023-01-28 14:51 ` David Laight
  1 sibling, 0 replies; 5+ messages in thread
From: Kees Cook @ 2023-01-27 18:55 UTC (permalink / raw)
  To: andriy.shevchenko, linux-kernel; +Cc: Kees Cook, andy, linux, Andrew Morton

On Fri, 27 Jan 2023 17:51:35 +0200, Andy Shevchenko wrote:
> Use strchr() instead of open coding it as it's done elsewhere in
> the same file. Either we will have similar to what it was or possibly
> better performance in case architecture implements its own strchr().
> 
> Memory wise on x86_64 bloat-o-meter shows the following
> 
>   Function           old     new   delta
>   strsep             111     102      -9
>   Total: Before=2763, After=2754, chg -0.33%
> 
> [...]

Applied to for-next/hardening, thanks!

[1/1] lib/string: Use strchr() in strpbrk()
      https://git.kernel.org/kees/c/a5f5ee7c49da

-- 
Kees Cook


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

* RE: [PATCH v1 1/1] lib/string: Use strchr() in strpbrk()
  2023-01-27 15:51 [PATCH v1 1/1] lib/string: Use strchr() in strpbrk() Andy Shevchenko
  2023-01-27 18:55 ` Kees Cook
@ 2023-01-28 14:51 ` David Laight
  2023-01-28 19:54   ` Andy Shevchenko
  1 sibling, 1 reply; 5+ messages in thread
From: David Laight @ 2023-01-28 14:51 UTC (permalink / raw)
  To: 'Andy Shevchenko', Kees Cook, linux-kernel
  Cc: Andy Shevchenko, Rasmus Villemoes, Andrew Morton

From: Andy Shevchenko
> Sent: 27 January 2023 15:52
> 
> Use strchr() instead of open coding it as it's done elsewhere in
> the same file. Either we will have similar to what it was or possibly
> better performance in case architecture implements its own strchr().

Except that you get a whole load of calls to strchr() for (typically)
very few characters.
So the cost of the calls dominates, anything that tries to speed up
strchr() for long strings will also slow things down.

Plausibly this version (untested) is faster!

char *strbprk(const char *str, const char *seps)
{
	const char *found, *try;

	do {
		if (*!seps)
			return NULL;
		found = strchr(str, *seps++);
	} while (!found);

	while (*seps) {
		try = memchr(str, *seps++, found - str);
		if (try)
			found = try;
	}

	return (char *)found;
}

Although I very much doubt strpbrk() is used anywhere where
performance matters.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


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

* Re: [PATCH v1 1/1] lib/string: Use strchr() in strpbrk()
  2023-01-28 14:51 ` David Laight
@ 2023-01-28 19:54   ` Andy Shevchenko
  2023-01-28 22:35     ` David Laight
  0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2023-01-28 19:54 UTC (permalink / raw)
  To: David Laight
  Cc: Andy Shevchenko, Kees Cook, linux-kernel, Andy Shevchenko,
	Rasmus Villemoes, Andrew Morton

On Sat, Jan 28, 2023 at 4:51 PM David Laight <David.Laight@aculab.com> wrote:
>
> From: Andy Shevchenko
> > Sent: 27 January 2023 15:52
> >
> > Use strchr() instead of open coding it as it's done elsewhere in
> > the same file. Either we will have similar to what it was or possibly
> > better performance in case architecture implements its own strchr().
>
> Except that you get a whole load of calls to strchr() for (typically)
> very few characters.
> So the cost of the calls dominates, anything that tries to speed up
> strchr() for long strings will also slow things down.

Hmm... I haven't seen the calls, I assume gcc simply inlined a copy of that.

...

> Although I very much doubt strpbrk() is used anywhere where
> performance matters.

strsep()/strspn() are the users.

-- 
With Best Regards,
Andy Shevchenko

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

* RE: [PATCH v1 1/1] lib/string: Use strchr() in strpbrk()
  2023-01-28 19:54   ` Andy Shevchenko
@ 2023-01-28 22:35     ` David Laight
  0 siblings, 0 replies; 5+ messages in thread
From: David Laight @ 2023-01-28 22:35 UTC (permalink / raw)
  To: 'Andy Shevchenko'
  Cc: Andy Shevchenko, Kees Cook, linux-kernel, Andy Shevchenko,
	Rasmus Villemoes, Andrew Morton

From: Andy Shevchenko
> Sent: 28 January 2023 19:55
> 
> On Sat, Jan 28, 2023 at 4:51 PM David Laight <David.Laight@aculab.com> wrote:
> >
> > From: Andy Shevchenko
> > > Sent: 27 January 2023 15:52
> > >
> > > Use strchr() instead of open coding it as it's done elsewhere in
> > > the same file. Either we will have similar to what it was or possibly
> > > better performance in case architecture implements its own strchr().
> >
> > Except that you get a whole load of calls to strchr() for (typically)
> > very few characters.
> > So the cost of the calls dominates, anything that tries to speed up
> > strchr() for long strings will also slow things down.
> 
> Hmm... I haven't seen the calls, I assume gcc simply inlined a copy of that.

Anything gcc itself inlines is likely to be optimised for long strings
(where inlining probably makes less difference).
In any case that will bloat the function - and you saw a size reduction.

About the worst thing that can happen here is that gcc realises the open
coded loop is strchr() and then inlines its own 'fast' copy.
Which is the last thing you want if the string is only a few characters
long.

> 
> ...
> 
> > Although I very much doubt strpbrk() is used anywhere where
> > performance matters.
> 
> strsep()/strspn() are the users.

I bet they aren't called anywhere that matters.
There is also a significant different.
strsep() is probably looking for a very small number of characters.
strspn() could easily have all alphabetics.

For long strings you can actually use a bitmap of the characters.
On 64bit this can, with care, be held in 4 registers.
32bit is more problematic.
But is you are just looking for " \t" the overheads are massive.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

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

end of thread, other threads:[~2023-01-28 22:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-27 15:51 [PATCH v1 1/1] lib/string: Use strchr() in strpbrk() Andy Shevchenko
2023-01-27 18:55 ` Kees Cook
2023-01-28 14:51 ` David Laight
2023-01-28 19:54   ` Andy Shevchenko
2023-01-28 22:35     ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).