Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Date: Tue, 9 Apr 2013 01:54:36 -0400
From: Rich Felker <dalias@...ifal.cx>
To: musl@...ts.openwall.com
Subject: Re: multibyte performance findings

On Sat, Apr 06, 2013 at 02:08:52AM -0400, Rich Felker wrote:
> On Sat, Apr 06, 2013 at 01:21:21AM -0400, Rich Felker wrote:
> > Hi all,
> > 
> > I've been examining performance in the multibyte conversion functions
> > (as part of the POSIX locale controversy), and have some interesting
> > findings so far:
> > [...]
> 
> And here's a diff of the proposed changes so far..
> 
> Rich

> diff --git a/src/multibyte/mbrtowc.c b/src/multibyte/mbrtowc.c
> index cc49781..d552652 100644
> --- a/src/multibyte/mbrtowc.c
> +++ b/src/multibyte/mbrtowc.c
> @@ -18,6 +18,7 @@ size_t mbrtowc(wchar_t *restrict wc, const char *restrict src, size_t n, mbstate
>  	const unsigned char *s = (const void *)src;
>  	const unsigned N = n;
>  
> +	if (!n) return -2;
>  	if (!st) st = (void *)&internal_state;
>  	c = *(unsigned *)st;
>  	
> @@ -27,9 +28,9 @@ size_t mbrtowc(wchar_t *restrict wc, const char *restrict src, size_t n, mbstate
>  		n = 1;
>  	} else if (!wc) wc = (void *)&wc;
>  
> -	if (!n) return -2;

This change turned out to be wrong (it's an invalid transformation
when s is null) and I found a better improvement anyway, which I've
committed. The commit log message is actually rather interesting:

http://git.musl-libc.org/cgit/musl/commit/?id=a49e038bab7b3927b6a9c7d0c52f9e1a9cb82629

and I think this finding serves as a warning about writing 'clever'
code for special cases that "falls through" to the general code,
rather than just writing the special case code explicitly.

> +	/* This condition can only be true if *s<0x80 and c==0 */
> +	if (*s + c < 0x80) return !!(*wc = *s);
>  	if (!c) {
> -		if (*s < 0x80) return !!(*wc = *s);
>  		if (*s-SA > SB-SA) goto ilseq;
>  		c = bittab[*s++-SA]; n--;
>  	}

I omitted this for now too since the improvement seems difficult to
measure. In principle it should be better, so I may revisit this
later.

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.