Re: [PATCH 1/2] lib/string: Improve strstarts() performance
From: Kees Cook
Date: Mon Apr 07 2025 - 12:29:06 EST
On Mon, Apr 07, 2025 at 09:15:04PM +0800, Zijun Hu wrote:
> From: Zijun Hu <quic_zijuhu@xxxxxxxxxxx>
>
> 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).
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
an explicit function call. That will be much more expensive.
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.
-Kees
--
Kees Cook