git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org,
	Johannes Schindelin <johannes.schindelin@gmx.de>,
	Karthik Nayak <karthik.188@gmail.com>
Subject: Re: [PATCH] type_from_string_gently: make sure length matches
Date: Fri, 17 Apr 2015 17:07:13 -0400	[thread overview]
Message-ID: <20150417210713.GA7403@peff.net> (raw)
In-Reply-To: <xmqqd232pjb0.fsf@gitster.dls.corp.google.com>

On Fri, Apr 17, 2015 at 01:54:27PM -0700, Junio C Hamano wrote:

> > 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.

> +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

  reply	other threads:[~2015-04-17 21:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-17 14:52 [PATCH] type_from_string_gently: make sure length matches Jeff King
2015-04-17 20:54 ` Junio C Hamano
2015-04-17 21:07   ` Jeff King [this message]
2015-04-17 21:11     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150417210713.GA7403@peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=johannes.schindelin@gmx.de \
    --cc=karthik.188@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).