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

Re: [PATCH v2 04/13] cache: make hashcmp and hasheq work with larger hashes

From
Duy Nguyen <pclouds@gmail.com>
Date
Oct 16, 2018, 15:44 UTC
Message-ID
<CACsJy8Cyep229CH8OfcVRqxC4soRiiCDhE_FMPhiEzVf-Z9v+Q@mail.gmail.com>
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:

Show 21 quoted lines
> 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).

Show 20 quoted lines
> +               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
Previous: brian m. carlsonNext: brian m. carlson
Message 8 of 44 in “Base SHA-256 implementation”
  1. 00/13 Base SHA-256 implementationbrian m. carlson, Oct 15, 2018
  2. 01/13 sha1-file: rename algorithm to "sha1"brian m. carlson, Oct 15, 2018
  3. Duy NguyenOct 16, 2018
  4. brian m. carlsonOct 17, 2018
  5. 02/13 sha1-file: provide functions to look up hash algorithmsbrian m. carlson, Oct 15, 2018
  6. SZEDER GáborOct 17, 2018
  7. 04/13 cache: make hashcmp and hasheq work with larger hashesbrian m. carlson, Oct 15, 2018
  8. Duy NguyenOct 16, 2018
  9. 06/13 t: make the sha1 test-tool helper genericbrian m. carlson, Oct 15, 2018
  10. 07/13 sha1-file: add a constant for hash block sizebrian m. carlson, Oct 15, 2018
  11. 03/13 hex: introduce functions to print arbitrary hashesbrian m. carlson, Oct 15, 2018
  12. Junio C HamanoOct 16, 2018
  13. brian m. carlsonOct 17, 2018
  14. 05/13 t: add basic tests for our SHA-1 implementationbrian m. carlson, Oct 15, 2018
  15. 08/13 t/helper: add a test helper to compute hash speedbrian m. carlson, Oct 15, 2018
  16. 09/13 commit-graph: convert to using the_hash_algobrian m. carlson, Oct 15, 2018
  17. Derrick StoleeOct 15, 2018
  18. 11/13 sha256: add an SHA-256 implementation using libgcryptbrian m. carlson, Oct 15, 2018
  19. 12/13 hash: add an SHA-256 implementation using OpenSSLbrian m. carlson, Oct 15, 2018
  20. Duy NguyenOct 16, 2018
  21. 13/13 commit-graph: specify OID version for SHA-256brian m. carlson, Oct 15, 2018
  22. Derrick StoleeOct 15, 2018
  23. Junio C HamanoOct 16, 2018
  24. brian m. carlsonOct 16, 2018
  25. Duy NguyenOct 16, 2018
  26. Derrick StoleeOct 16, 2018
  27. Duy NguyenOct 16, 2018
  28. brian m. carlsonOct 16, 2018
  29. Duy NguyenOct 17, 2018
  30. brian m. carlsonOct 18, 2018
  31. Derrick StoleeOct 18, 2018
  32. brian m. carlsonOct 19, 2018
  33. Derrick StoleeOct 17, 2018
  34. brian m. carlsonOct 17, 2018
  35. 10/13 Add a base implementation of SHA-256 supportbrian m. carlson, Oct 15, 2018
  36. Duy NguyenOct 15, 2018
  37. brian m. carlsonOct 15, 2018
  38. Duy NguyenOct 16, 2018
  39. SZEDER GáborOct 17, 2018
  40. brian m. carlsonOct 17, 2018
  41. Junio C HamanoOct 16, 2018
  42. Junio C HamanoOct 16, 2018
  43. brian m. carlsonOct 16, 2018
  44. Duy NguyenOct 16, 2018

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.