From: David Laight <David.Laight@ACULAB.COM>
To: 'Andy Shevchenko' <andy.shevchenko@gmail.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Kees Cook <keescook@chromium.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
Andy Shevchenko <andy@kernel.org>,
"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
Andrew Morton <akpm@linux-foundation.org>
Subject: RE: [PATCH v1 1/1] lib/string: Use strchr() in strpbrk()
Date: Sat, 28 Jan 2023 22:35:19 +0000 [thread overview]
Message-ID: <d36a45b689e84ae7b9c1079221e054da@AcuMS.aculab.com> (raw)
In-Reply-To: <CAHp75VcZgkuOA3pSrg7cN36EAB4Y0jV2xFNfj-sGp_C17J2yAQ@mail.gmail.com>
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)
prev parent reply other threads:[~2023-01-28 22:35 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d36a45b689e84ae7b9c1079221e054da@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=andy.shevchenko@gmail.com \
--cc=andy@kernel.org \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox