This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Fix undefined behaviour inconsistent for strtok
- From: Wilco Dijkstra <Wilco dot Dijkstra at arm dot com>
- To: "adhemerval dot zanella at linaro dot org" <adhemerval dot zanella at linaro dot org>, "Andreas Schwab" <schwab at suse dot de>
- Cc: "libc-alpha at sourceware dot org" <libc-alpha at sourceware dot org>, nd <nd at arm dot com>
- Date: Tue, 25 Oct 2016 16:45:59 +0000
- Subject: Re: [PATCH] Fix undefined behaviour inconsistent for strtok
- Authentication-results: sourceware.org; auth=none
- Nodisclaimer: True
- References: <AM5PR0802MB2610A3388EDD439E3EDED65083A80@AM5PR0802MB2610.eurprd08.prod.outlook.com>
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:99
Hi,
>+ if ((s == NULL) && ((s = olds) == NULL))
>+ return NULL;
>
> What is the benefit of this given:
>
> if (s == NULL)
> /* This token finishes the string. */
> olds = __rawmemchr (token, '\0');
Right, looking at this a bit more, if we call strcspn rather than strpbrk,
we get the end of the string for free and avoid 2 calls.
It looks like the reason behind the (s = olds) == NULL check is purely
performance. Checking *s == '\0' at the start is slightly faster still (and
would seem better as it catches incorrect use of strtok).
With these changes you get > 2x speedup for most cases. I'll post a patch.
Wilco