From: Zijun Hu <zijun_hu@icloud.com>
To: Kees Cook <kees@kernel.org>
Cc: Andy Shevchenko <andy@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
Zijun Hu <quic_zijuhu@quicinc.com>,
"Rob Herring (Arm)" <robh@kernel.org>
Subject: Re: [PATCH 1/2] lib/string: Improve strstarts() performance
Date: Tue, 8 Apr 2025 19:34:41 +0800 [thread overview]
Message-ID: <1d2e9f40-708d-49b2-b0ee-a1d775f6fd23@icloud.com> (raw)
In-Reply-To: <202504070912.8BB9AD0AE@keescook>
On 2025/4/8 00:17, Kees Cook wrote:
>> strstarts() is frequently invoked to test if a string has another string
>> as prefix, but its performance is degraded by the strlen() loop contained.
>>
>> Improve its performance by eliminating the strlen() loop.
> So, as Andy already said: no, this is very unlikely to be a performance
> improvement, and if it is, you'll need to show the numbers (and likely
> the reason _why_, in the form of assembly output, etc).
>
agree.
> The reason this isn't going to be an improvement is because
> strlen($string_constant) is optimized by the compiler into a integral
> constant value. So you'd be replacing a potentially inline constant with
will confirm if strlen() within strstarts() is compile-time constant.
> an explicit function call. That will be much more expensive.
strstarts() has a strncmp() function call as well even if it is inline
>
> With almost 300 users:
> $ git grep 'strstarts' | wc -l
> 198
>
> Only 38 are _not_ using a string constant:
> $ git grep 'strstarts' | grep -v '"' | wc -l
> 38
>
> Additionally, there is no "loop". strlen() of a runtime string would be
> evaluated once.
>
strlen() contains a loop which has strlen() char comparisons
> -Kees
next prev parent reply other threads:[~2025-04-08 11:34 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-07 13:15 [PATCH 0/2] lib/string: Improve performance for both strstarts() and str_has_prefix() Zijun Hu
2025-04-07 13:15 ` [PATCH 1/2] lib/string: Improve strstarts() performance Zijun Hu
2025-04-07 13:51 ` Andy Shevchenko
2025-04-07 14:33 ` Zijun Hu
2025-04-07 14:39 ` Andy Shevchenko
2025-04-07 14:53 ` Zijun Hu
2025-04-07 16:17 ` Kees Cook
2025-04-08 11:34 ` Zijun Hu [this message]
2025-04-08 13:13 ` kernel test robot
2025-04-07 13:15 ` [PATCH 2/2] lib/string: Improve str_has_prefix() performance Zijun Hu
2025-04-07 13:52 ` Andy Shevchenko
2025-04-07 13:49 ` [PATCH 0/2] lib/string: Improve performance for both strstarts() and str_has_prefix() Andy Shevchenko
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=1d2e9f40-708d-49b2-b0ee-a1d775f6fd23@icloud.com \
--to=zijun_hu@icloud.com \
--cc=akpm@linux-foundation.org \
--cc=andy@kernel.org \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=quic_zijuhu@quicinc.com \
--cc=robh@kernel.org \
/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
all inboxes | Powered by JetHome®