Git development
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: "brian m. carlson" <sandals@crustytoothpaste.net>, git@vger.kernel.org
Subject: Re: [RFC PATCH 3/6] hex: make hex_to_bytes accept kind of hex to use
Date: Sat, 1 Aug 2026 10:35:13 -0400	[thread overview]
Message-ID: <20260801143513.GE2041176@coredump.intra.peff.net> (raw)
In-Reply-To: <xmqqzez7hamu.fsf@gitster.g>

On Fri, Jul 31, 2026 at 12:38:17AM -0700, Junio C Hamano wrote:

> "brian m. carlson" <sandals@crustytoothpaste.net> writes:
> 
> > -int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
> > +int hex_to_bytes(unsigned char *binary, const char *hex, size_t len, enum hexkind kind)
> >  {
> >  	for (; len; len--, hex += 2) {
> > -		unsigned int val = (hexval(hex[0], HEX_KIND_MIXED) << 4) | hexval(hex[1], HEX_KIND_MIXED);
> > +		unsigned int val = (hexval(hex[0], kind) << 4) | hexval(hex[1], kind);
> >  
> >  		if (val & ~0xff)
> >  			return -1;
> 
> It depends on how big 'len' would be to matter, but if we are
> looping for a long stretch, choosing which one of the two hexval
> tables to use outside the loop and using that inside may of course
> be more performant.
> 
> I wondered how ugly such a restructure of the API would look like,
> and it does not look _too_ bad.
> 
> 	void *hextable = hex_table(HEX_KIND_MIXED);
> 
> 	for (; len; len--, hex += 2) {
> 		unsigned int val =
> 			(hexval(hex[0], hextable) << 4) | hexval(hex[1], hextable);
> 		...
> 	}
> 
> The true type of hextable would be "signed char [256]", but the
> callers of the hexval() function do not need to know it, hence I
> chose "void *" here.

I had the same thought when reading this, but I wondered if the compiler
might be able to hoist the comparison out of the loop itself (because
hexval() it inlined anyway). It doesn't seem to do so, though (at least
with gcc-15). It loads both table addresses into registers, but there's
still a branch in the loop to decide which table to use.

So in theory this kind of manual hoisting could help.  Might not be that
big a deal with branch prediction, though.

-Peff

  reply	other threads:[~2026-08-01 14:35 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-29 23:32 [RFC PATCH 0/6] Git 3.0: restrict hex object IDs to lowercase only brian m. carlson
2026-07-29 23:32 ` [RFC PATCH 1/6] hex: add functionality for lowercase-only hex brian m. carlson
2026-07-31  7:38   ` Junio C Hamano
2026-07-29 23:32 ` [RFC PATCH 2/6] hex: allow specifying hex type with hex2chr brian m. carlson
2026-07-29 23:32 ` [RFC PATCH 3/6] hex: make hex_to_bytes accept kind of hex to use brian m. carlson
2026-07-31  7:38   ` Junio C Hamano
2026-08-01 14:35     ` Jeff King [this message]
2026-07-29 23:32 ` [RFC PATCH 4/6] hex: label usages of hex parsing for object IDs brian m. carlson
2026-07-31  3:24   ` Junio C Hamano
2026-07-29 23:32 ` [RFC PATCH 5/6] object-name: use hexval brian m. carlson
2026-07-29 23:32 ` [RFC PATCH 6/6] hex: allow only lowercase object IDs in breaking changes mode brian m. carlson
2026-07-31  7:48   ` Junio C Hamano
2026-07-31 12:33     ` Junio C Hamano
2026-08-02 22:09     ` brian m. carlson
2026-08-04 19:32       ` Junio C Hamano
2026-08-04 21:46         ` brian m. carlson
2026-08-05  3:09       ` Michael Montalbo
2026-07-30  8:21 ` [RFC PATCH 0/6] Git 3.0: restrict hex object IDs to lowercase only Junio C Hamano
2026-07-30 21:18   ` brian m. carlson
2026-08-01 14:45     ` Jeff King
2026-08-01 18:22       ` Junio C Hamano
2026-08-02 21:55       ` brian m. carlson

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=20260801143513.GE2041176@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=sandals@crustytoothpaste.net \
    /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