This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PING][PATCH] More effective generic strrchr.
- From: Richard Earnshaw <rearnsha at arm dot com>
- To: OndÅej BÃlka <neleai at seznam dot cz>, "libc-alpha at sourceware dot org" <libc-alpha at sourceware dot org>
- Date: Wed, 07 Jan 2015 12:06:23 +0000
- Subject: Re: [PING][PATCH] More effective generic strrchr.
- Authentication-results: sourceware.org; auth=none
- References: <20131005071542 dot GA13382 at domone> <20150107115537 dot GA31981 at domone>
On 07/01/15 11:55, OndÅej BÃlka wrote:
> [PING]
Looks sensible to me.
It would be useful if some of the bench tests were based on degenerate
examples.
R.
> On Sat, Oct 05, 2013 at 09:15:42AM +0200, OndÅej BÃlka wrote:
>>
>> A generic implementation of strrchr could be improved by following
>> patch. As it is likely that last occurrence of c will be near we
>> could quickly find it. This is also much faster for degenerate inputs
>> like:
>>
>> strrchr("xyxyxyxyxyxyxyxyxyxyxyxyx...",'x');
>>
>> where a loop slows us down.
>>
>> * string/strrchr.c: Improve implementation.
>>
>> ---
>> string/strrchr.c | 19 ++-----------------
>> 1 file changed, 2 insertions(+), 17 deletions(-)
>>
>> diff --git a/string/strrchr.c b/string/strrchr.c
>> index bdec841..ad242b6 100644
>> --- a/string/strrchr.c
>> +++ b/string/strrchr.c
>> @@ -23,23 +23,8 @@
>> char *
>> strrchr (const char *s, int c)
>> {
>> - const char *found, *p;
>> -
>> - c = (unsigned char) c;
>> -
>> - /* Since strchr is fast, we use it rather than the obvious loop. */
>> -
>> - if (c == '\0')
>> - return strchr (s, '\0');
>> -
>> - found = NULL;
>> - while ((p = strchr (s, c)) != NULL)
>> - {
>> - found = p;
>> - s = p + 1;
>> - }
>> -
>> - return (char *) found;
>> + /* We need to include terminating zero for case c == 0. */
>> + return __memrchr (s, c, strlen (s) + 1);
>> }
>>
>> #ifdef weak_alias
>> --
>> 1.8.4.rc3
>
>