RE: [PATCH v1 1/1] lib/string: Use strchr() in strpbrk()

From: David Laight
Date: Sat Jan 28 2023 - 17:35:32 EST


From: Andy Shevchenko
> Sent: 28 January 2023 19:55
>
> On Sat, Jan 28, 2023 at 4:51 PM David Laight <David.Laight@xxxxxxxxxx> 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)