This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Use strlen when searching for a nul char
- From: Wilco Dijkstra <Wilco dot Dijkstra at arm dot com>
- To: Mike Frysinger <vapier at gentoo dot org>
- Cc: 'GNU C Library' <libc-alpha at sourceware dot org>, nd <nd at arm dot com>
- Date: Tue, 19 Apr 2016 22:03:07 +0000
- Subject: Re: [PATCH] Use strlen when searching for a nul char
- Authentication-results: sourceware.org; auth=none
- Nodisclaimer: True
- References: <DB3PR08MB008978DA72CE0E1B93BB477483A60 at DB3PR08MB0089 dot eurprd08 dot prod dot outlook dot com>,<20160419175706 dot GI5369 at vapier dot lan>
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:23
Mike Frysinger wrote:
> On 25 Feb 2016 13:04, Wilco Dijkstra wrote:
>> Remove the strchr (s, '\0') to rawmemchr optimization as using rawmemchr is
>> a bad idea - I have a patch to add strchr (s, '\0') -> strlen to GCC7.
>> Like strchr (s, '\0'), rawmemchr (s, '\0') appears a common idiom for finding
>> the end of a string, however it is not the most efficient way of doing so.
>> Strlen is a simpler operation which is significantly faster on larger inputs
>> (eg. on x86 strlen is 50% faster than rawmemchr on strings of 1KB).
>
> will there be a change in GCC to also detect rawmemchr(s,'\0') ?
I wasn't planning to add it - GCC doesn't currently have it as a builtin so support
would need to be added first. It's unclear how often rawmemchr is used
elsewhere and what percentage is for finding the end of a string.
> even then, since this optimization isn't showing up until GCC7, shouldn't
> we keep some logic here ? i.e. transform strchr/rawmemchr(s, '\0') into
> strlen before falling back ?
If it is common to use an old GCC to build a new GLIBC for a distro then adding
strchr->strlen with a PREREQ would be useful. But if one typically first builds GCC7
and then GLIBC with that then it would not matter.
An alternative would be fixup uses in GLIBC so it doesn't rely on header or GCC
optimizations to do the obvious.
Wilco