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