[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <b0747531-c988-55f8-7be9-0d9863d3d91a@prevas.dk>
Date: Sat, 5 Dec 2020 21:28:54 +0100
From: Rasmus Villemoes <rasmus.villemoes@...vas.dk>
To: Ard Biesheuvel <ardb@...nel.org>,
James Bottomley <James.Bottomley@...senpartnership.com>
Cc: laniel_francis@...vacyrequired.com,
linux-efi <linux-efi@...r.kernel.org>,
Linux Kernel Mailing List <linux-kernel@...r.kernel.org>
Subject: Re: [RFC PATCH v1 07/12] efi: Replace strstarts() by
str_has_prefix().
On 05/12/2020 20.36, Ard Biesheuvel wrote:
> On Fri, 4 Dec 2020 at 19:02, James Bottomley
> <James.Bottomley@...senpartnership.com> wrote:
>>
>> On Fri, 2020-12-04 at 18:07 +0100, Ard Biesheuvel wrote:
>>> On Fri, 4 Dec 2020 at 18:06, <laniel_francis@...vacyrequired.com>
>>> wrote:
>>>> From: Francis Laniel <laniel_francis@...vacyrequired.com>
>>>>
>>>> The two functions indicates if a string begins with a given prefix.
>>>> The only difference is that strstarts() returns a bool while
>>>> str_has_prefix()
>>>> returns the length of the prefix if the string begins with it or 0
>>>> otherwise.
>>>>
>>>
>>> Why?
>>
>> I think I can answer that. If the conversion were done properly (which
>> it's not) you could get rid of the double strings in the code which are
>> error prone if you update one and forget another. This gives a good
>> example: 3d739c1f6156 ("tracing: Use the return of str_has_prefix() to
>> remove open coded numbers"). so in your code you'd replace things like
>>
>> if (strstarts(option, "rgb")) {
>> option += strlen("rgb");
>> ...
>>
>> with
>>
>> len = str_has_prefix(option, "rgb");
>> if (len) {
>> option += len
>> ...
>>
>> Obviously you also have cases where strstart is used as a boolean with
>> no need to know the length ... I think there's no value to converting
>> those.
>>
>
> This will lead to worse code being generated. strlen() is evaluated at
> build time by the compiler if the argument is a string literal, so
> your 'before' version gets turned into 'option += 3', whereas the
> latter needs to use a runtime variable.
Well, both functions are static inlines
static inline bool strstarts(const char *str, const char *prefix)
{
return strncmp(str, prefix, strlen(prefix)) == 0;
}
static __always_inline size_t str_has_prefix(const char *str, const char
*prefix)
{
size_t len = strlen(prefix);
return strncmp(str, prefix, len) == 0 ? len : 0;
}
So
len = str_has_prefix()
if (len) { use len }
is essentially
if (somecondition ? some-non-zero-constant : 0) { use
some-non-zero-constant }
which I'm fairly certain the compiler has no problem turning into
if (somecondition) { ... }
which is exactly the existing strstarts() code. So I wouldn't expect a
huge difference in generated code.
Rasmus
Powered by blists - more mailing lists