git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Duy Nguyen <pclouds@gmail.com>
To: "brian m. carlson" <sandals@crustytoothpaste.net>
Cc: "Git Mailing List" <git@vger.kernel.org>,
	"Jeff King" <peff@peff.net>, "Derrick Stolee" <stolee@gmail.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: Re: [PATCH v2 04/13] cache: make hashcmp and hasheq work with larger hashes
Date: Tue, 16 Oct 2018 17:44:47 +0200	[thread overview]
Message-ID: <CACsJy8Cyep229CH8OfcVRqxC4soRiiCDhE_FMPhiEzVf-Z9v+Q@mail.gmail.com> (raw)
In-Reply-To: <20181015021900.1030041-5-sandals@crustytoothpaste.net>

On Mon, Oct 15, 2018 at 4:21 AM brian m. carlson
<sandals@crustytoothpaste.net> wrote:
> diff --git a/cache.h b/cache.h
> index a13d14ce0a..0b88c3a344 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -1024,16 +1024,12 @@ extern const struct object_id null_oid;
>  static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
>  {
>         /*
> -        * This is a temporary optimization hack. By asserting the size here,
> -        * we let the compiler know that it's always going to be 20, which lets
> -        * it turn this fixed-size memcmp into a few inline instructions.
> -        *
> -        * This will need to be extended or ripped out when we learn about
> -        * hashes of different sizes.
> +        * Teach the compiler that there are only two possibilities of hash size
> +        * here, so that it can optimize for this case as much as possible.
>          */
> -       if (the_hash_algo->rawsz != 20)
> -               BUG("hash size not yet supported by hashcmp");
> -       return memcmp(sha1, sha2, the_hash_algo->rawsz);
> +       if (the_hash_algo->rawsz == GIT_MAX_RAWSZ)

It's tangent. But performance is probably another good reason to
detach the_hash_algo from the_repository so we have one less
dereference to do. (the other good reason is these hash operations
should work in "no-repo" commands as well, where the_repository does
not really make sense).

> +               return memcmp(sha1, sha2, GIT_MAX_RAWSZ);
> +       return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
>  }
>
>  static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
> @@ -1043,7 +1039,13 @@ static inline int oidcmp(const struct object_id *oid1, const struct object_id *o
>
>  static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2)
>  {
> -       return !hashcmp(sha1, sha2);
> +       /*
> +        * We write this here instead of deferring to hashcmp so that the
> +        * compiler can properly inline it and avoid calling memcmp.
> +        */
> +       if (the_hash_algo->rawsz == GIT_MAX_RAWSZ)
> +               return !memcmp(sha1, sha2, GIT_MAX_RAWSZ);
> +       return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
>  }
>
>  static inline int oideq(const struct object_id *oid1, const struct object_id *oid2)



-- 
Duy

  reply	other threads:[~2018-10-16 15:45 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-15  2:18 [PATCH v2 00/13] Base SHA-256 implementation brian m. carlson
2018-10-15  2:18 ` [PATCH v2 01/13] sha1-file: rename algorithm to "sha1" brian m. carlson
2018-10-16 15:17   ` Duy Nguyen
2018-10-17 22:53     ` brian m. carlson
2018-10-15  2:18 ` [PATCH v2 02/13] sha1-file: provide functions to look up hash algorithms brian m. carlson
2018-10-17 13:32   ` SZEDER Gábor
2018-10-15  2:18 ` [PATCH v2 03/13] hex: introduce functions to print arbitrary hashes brian m. carlson
2018-10-16  1:54   ` Junio C Hamano
2018-10-17 23:49     ` brian m. carlson
2018-10-15  2:18 ` [PATCH v2 04/13] cache: make hashcmp and hasheq work with larger hashes brian m. carlson
2018-10-16 15:44   ` Duy Nguyen [this message]
2018-10-15  2:18 ` [PATCH v2 05/13] t: add basic tests for our SHA-1 implementation brian m. carlson
2018-10-15  2:18 ` [PATCH v2 06/13] t: make the sha1 test-tool helper generic brian m. carlson
2018-10-15  2:18 ` [PATCH v2 07/13] sha1-file: add a constant for hash block size brian m. carlson
2018-10-15  2:18 ` [PATCH v2 08/13] t/helper: add a test helper to compute hash speed brian m. carlson
2018-10-15  2:18 ` [PATCH v2 09/13] commit-graph: convert to using the_hash_algo brian m. carlson
2018-10-15 15:10   ` Derrick Stolee
2018-10-15  2:18 ` [PATCH v2 10/13] Add a base implementation of SHA-256 support brian m. carlson
2018-10-15 14:59   ` Duy Nguyen
2018-10-15 23:30     ` brian m. carlson
2018-10-16 14:59       ` Duy Nguyen
2018-10-17 16:12   ` SZEDER Gábor
2018-10-17 23:04     ` brian m. carlson
2018-10-15  2:18 ` [PATCH v2 11/13] sha256: add an SHA-256 implementation using libgcrypt brian m. carlson
2018-10-15  2:18 ` [PATCH v2 12/13] hash: add an SHA-256 implementation using OpenSSL brian m. carlson
2018-10-16 15:36   ` Duy Nguyen
2018-10-15  2:19 ` [PATCH v2 13/13] commit-graph: specify OID version for SHA-256 brian m. carlson
2018-10-15 15:11   ` Derrick Stolee
2018-10-16  2:00   ` Junio C Hamano
2018-10-16 22:39     ` brian m. carlson
2018-10-16 15:35   ` Duy Nguyen
2018-10-16 16:01     ` Derrick Stolee
2018-10-16 16:09       ` Duy Nguyen
2018-10-16 22:44         ` brian m. carlson
2018-10-17 14:31           ` Duy Nguyen
2018-10-18  0:06             ` brian m. carlson
2018-10-18 13:03               ` Derrick Stolee
2018-10-19 22:21                 ` brian m. carlson
2018-10-17 12:21   ` Derrick Stolee
2018-10-17 22:38     ` brian m. carlson
2018-10-16  2:00 ` [PATCH v2 00/13] Base SHA-256 implementation Junio C Hamano
2018-10-16  4:01 ` Junio C Hamano
2018-10-16 22:45   ` brian m. carlson
2018-10-16 15:39 ` Duy Nguyen

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=CACsJy8Cyep229CH8OfcVRqxC4soRiiCDhE_FMPhiEzVf-Z9v+Q@mail.gmail.com \
    --to=pclouds@gmail.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    --cc=sandals@crustytoothpaste.net \
    --cc=stolee@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).