Re: [PATCH] dir.c: avoid c99 array initialization
- From
Johannes Schindelin <johannes.schindelin@gmx.de>
- Date
- Aug 29, 2008, 11:08 UTC
- Message-ID
- <alpine.DEB.1.00.0808291306420.24820@pacific.mpi-cbg.de.mpi-cbg.de>
- In-Reply-To
- <20080828201657.GH21072@spearce.org>
Hi,
On Thu, 28 Aug 2008, Shawn O. Pearce wrote:
Show 33 quoted lines
> Alex Riesen <raa.lkml@gmail.com> wrote:
> >
> > For just these 5 values it is likely more effective to just use
> > a conditional statement (less stack requested, less likely
> > some stupid compiler tries to optimize it wrongly).
> > And just as readable.
> >
> > diff --git a/dir.c b/dir.c
> > index 92452eb..1cf5985 100644
> > --- a/dir.c
> > +++ b/dir.c
> > @@ -680,17 +680,12 @@ static int cmp_name(const void *p1, const void *p2)
> > */
> > static int simple_length(const char *match)
> > {
> > - const char special[256] = {
> > - [0] = 1, ['?'] = 1,
> > - ['\\'] = 1, ['*'] = 1,
> > - ['['] = 1
> > - };
> > int len = -1;
> >
> > for (;;) {
> > unsigned char c = *match++;
> > len++;
> > - if (special[c])
> > + if (!c || '?' == c || '\\' == c || '*' == c || '[' == c)
>
> I am reminded of a year old thread with my patch to this:
>
> http://kerneltrap.org/mailarchive/git/2007/4/15/243541
>
> The patch never applied. I wonder why. Was it just Dscho's comment?If it was, I am very sorry.
But I still think that a lookup for something that is called potentially a million times per second is better than a switch statement (except when there are less than, say, 4 cases).
Ciao, Dscho