git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Correct dir.c to compile on Solaris 9

From
JWJosef Weidendorfer <josef.weidendorfer@gmx.de>
Date
Apr 15, 2007, 21:03 UTC
Message-ID
<200704152303.12435.Josef.Weidendorfer@gmx.de>
In-Reply-To
<Pine.LNX.4.64.0704151824240.18846@racer.site>
On Sunday 15 April 2007, Johannes Schindelin wrote:
Show 25 quoted lines
> On Sun, 15 Apr 2007, Shawn O. Pearce wrote:
> >  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])
> > +		switch (c) {
> > +		case 0: case '?':
> > +		case '\\': case '*':
> > +		case '[':
> >  			return len;
> > +		}
> >  	}
> >  }
> 
> You are replacing a table-based check with a switch based, which might be 
> substantially slower (depends on how often cmp_name() is called).

Or faster. When the table gives a cache miss and has to be loaded from main memory, I am quite sure that 5 compares in a row are faster than the cache miss.

Actually, with the switch, the compiler is free to implement it with a table (and gcc usually does this, probably even using a substantially smaller table). The table-based check in contrast looks like some kind of micro-optimization which makes the code IMHO more difficult to read, and which only would be justified with meassured improvements.

Josef
Previous: Johannes SchindelinNext: Robin Rosenberg
Message 3 of 5 in “Correct dir.c to compile on Solaris 9”
  1. Correct dir.c to compile on Solaris 9Shawn O. Pearce, Apr 15, 2007
  2. Johannes SchindelinApr 15, 2007
  3. Josef WeidendorferApr 15, 2007
  4. Robin RosenbergApr 15, 2007
  5. Junio C HamanoApr 15, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.