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

Re: {RFC/PATCH] micro-optimize get_sha1_hex()

From
JGJeff Garzik <jeff@garzik.org>
Date
Sep 9, 2006, 22:33 UTC
Message-ID
<45034143.4050308@garzik.org>
In-Reply-To
<7vzmd8vh6q.fsf@assigned-by-dhcp.cox.net>
Junio C Hamano wrote:
Show 18 quoted lines
>  int get_sha1_hex(const char *hex, unsigned char *sha1)
>  {
>  	int i;
>  	for (i = 0; i < 20; i++) {
> -		unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
> -		if (val & ~0xff)
> +		unsigned int v, w, val;
> +		v = *hex++;
> +		if ((v < '0') || ('f' < v) ||
> +		    ((v = hexval[v-'0']) == 255))
> +			return -1;
> +		w = *hex++;
> +		if ((w < '0') || ('f' < w) ||
> +		    ((w = hexval[w-'0']) == 255))
>  			return -1;
> -		*sha1++ = val;
> -		hex += 2;
> +		*sha1++ = (v << 4) | w;

Why not just make the table include the range of all possible characters? That would eliminate some comparisons and subtractions against '0'.

The end result would look more like the current (unpatched) form, except with a function replaced by the table.

	Jeff
Previous: Junio C HamanoNext: Linus Torvalds
Message 2 of 4 in “{RFC/PATCH] micro-optimize get_sha1_hex()”
  1. Junio C HamanoSep 9, 2006
  2. Jeff GarzikSep 9, 2006
  3. Linus TorvaldsSep 10, 2006
  4. Linus TorvaldsSep 10, 2006

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.