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

Re: [PATCH] type_from_string_gently: make sure length matches

From
Jeff King <peff@peff.net>
Date
Apr 17, 2015, 21:07 UTC
Message-ID
<20150417210713.GA7403@peff.net>
In-Reply-To
<xmqqd232pjb0.fsf@gitster.dls.corp.google.com>
On Fri, Apr 17, 2015 at 01:54:27PM -0700, Junio C Hamano wrote:
Show 13 quoted lines
> > Since the strings we are matching are literals, we could also record
> > their sizes in the object_type_strings array and check the length first
> > before even calling strncmp. I doubt this is a performance hot-spot,
> > though.
> >
> > You could also potentially just use strlen(object_type_strings[i]), but
> > I'm not sure if compilers will optimize out the strlen in this case,
> > since it is in a loop.
> 
> That thought crossed my mind while reading your patch.  It could
> even make it go faster if we made object_type_strings into an array
> of counted strings (i.e. "struct { const char *str; int len; }")
> and then took advantage of the fact that we have lengths of both.
Right, that was what I meant.

I'd be surprised if it appreciably speeds things up, but I guess it is not too complicated to do.

Show 10 quoted lines
> +static struct {
> +	const char *str;
> +	int len;
> +} object_type_name[] = {
> +	{ NULL, 0 },      /* OBJ_NONE = 0 */
> +	{ "commit", 6 },  /* OBJ_COMMIT = 1 */
> +	{ "tree", 4 },	  /* OBJ_TREE = 2 */
> +	{ "blob", 4 },    /* OBJ_BLOB = 3 */
> +	{ "tag", 3 },     /* OBJ_TAG = 4 */
>  };
I had envisioned a macro like:
  #define SIZED_STRING(x) { (x), (sizeof(x) - 1) }

though perhaps that is overkill for such a short list (that we don't even expect to change).

-Peff
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 4 in “type_from_string_gently: make sure length matches”
  1. type_from_string_gently: make sure length matchesJeff King, Apr 17, 2015
  2. Junio C HamanoApr 17, 2015
  3. Jeff KingApr 17, 2015
  4. Junio C HamanoApr 17, 2015

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.