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

[PATCH v4 04/12] cache: make hashcmp and hasheq work with larger hashes

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Oct 25, 2018, 02:39 UTC
Message-ID
<20181025024005.154208-5-sandals@crustytoothpaste.net>
In-Reply-To
<20181025024005.154208-1-sandals@crustytoothpaste.net>

In 183a638b7d ("hashcmp: assert constant hash size", 2018-08-23), we modified hashcmp to assert that the hash size was always 20 to help it optimize and inline calls to memcmp. In a future series, we replaced many calls to hashcmp and oidcmp with calls to hasheq and oideq to improve inlining further.

However, we want to support hash algorithms other than SHA-1, namely SHA-256. When doing so, we must handle the case where these values are 32 bytes long as well as 20. Adjust hashcmp to handle two cases: 20-byte matches, and maximum-size matches. Therefore, when we include SHA-256, we'll automatically handle it properly, while at the same time teaching the compiler that there are only two possible options to consider. This will allow the compiler to write the most efficient possible code.

Copy similar code into hasheq and perform an identical transformation. At least with GCC 8.2.0, making hasheq defer to hashcmp when there are two branches prevents the compiler from inlining the comparison, while the code in this patch is inlined properly. Add a comment to avoid an accidental performance regression from well-intentioned refactoring.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 cache.h | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
diff --git a/cache.h b/cache.h
index 51580c4b77..bab8e8964f 100644
--- a/cache.h
+++ b/cache.h
@@ -1027,16 +1027,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)
+		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)
@@ -1046,7 +1042,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)
Previous: brian m. carlsonNext: brian m. carlson
Message 5 of 58 in “Base SHA-256 implementation”
  1. 00/12 Base SHA-256 implementationbrian m. carlson, Oct 25, 2018
  2. 01/12 sha1-file: rename algorithm to "sha1"brian m. carlson, Oct 25, 2018
  3. 02/12 sha1-file: provide functions to look up hash algorithmsbrian m. carlson, Oct 25, 2018
  4. 03/12 hex: introduce functions to print arbitrary hashesbrian m. carlson, Oct 25, 2018
  5. 04/12 cache: make hashcmp and hasheq work with larger hashesbrian m. carlson, Oct 25, 2018
  6. 05/12 t: add basic tests for our SHA-1 implementationbrian m. carlson, Oct 25, 2018
  7. 06/12 t: make the sha1 test-tool helper genericbrian m. carlson, Oct 25, 2018
  8. 07/12 sha1-file: add a constant for hash block sizebrian m. carlson, Oct 25, 2018
  9. 08/12 t/helper: add a test helper to compute hash speedbrian m. carlson, Oct 25, 2018
  10. 09/12 commit-graph: convert to using the_hash_algobrian m. carlson, Oct 25, 2018
  11. 10/12 Add a base implementation of SHA-256 supportbrian m. carlson, Oct 25, 2018
  12. Carlo ArenasOct 25, 2018
  13. brian m. carlsonOct 28, 2018
  14. Junio C HamanoOct 29, 2018
  15. brian m. carlsonOct 31, 2018
  16. Junio C HamanoNov 1, 2018
  17. Christian CouderOct 27, 2018
  18. 11/12 sha256: add an SHA-256 implementation using libgcryptbrian m. carlson, Oct 25, 2018
  19. 12/12 hash: add an SHA-256 implementation using OpenSSLbrian m. carlson, Oct 25, 2018
  20. 00/12 Base SHA-256 implementationbrian m. carlson, Nov 4, 2018
  21. 01/12 sha1-file: rename algorithm to "sha1"brian m. carlson, Nov 4, 2018
  22. Ævar Arnfjörð BjarmasonNov 5, 2018
  23. 02/12 sha1-file: provide functions to look up hash algorithmsbrian m. carlson, Nov 4, 2018
  24. Derrick StoleeNov 13, 2018
  25. Duy NguyenNov 13, 2018
  26. brian m. carlsonNov 14, 2018
  27. Ramsay JonesNov 14, 2018
  28. Ramsay JonesNov 14, 2018
  29. Jeff KingNov 14, 2018
  30. brian m. carlsonNov 14, 2018
  31. Ramsay JonesNov 14, 2018
  32. 03/12 hex: introduce functions to print arbitrary hashesbrian m. carlson, Nov 4, 2018
  33. 05/12 t: add basic tests for our SHA-1 implementationbrian m. carlson, Nov 4, 2018
  34. 04/12 cache: make hashcmp and hasheq work with larger hashesbrian m. carlson, Nov 4, 2018
  35. 07/12 sha1-file: add a constant for hash block sizebrian m. carlson, Nov 4, 2018
  36. 06/12 t: make the sha1 test-tool helper genericbrian m. carlson, Nov 4, 2018
  37. 08/12 t/helper: add a test helper to compute hash speedbrian m. carlson, Nov 4, 2018
  38. 09/12 commit-graph: convert to using the_hash_algobrian m. carlson, Nov 4, 2018
  39. 10/12 Add a base implementation of SHA-256 supportbrian m. carlson, Nov 4, 2018
  40. Ævar Arnfjörð BjarmasonNov 5, 2018
  41. brian m. carlsonNov 7, 2018
  42. Ævar Arnfjörð BjarmasonNov 10, 2018
  43. 11/12 sha256: add an SHA-256 implementation using libgcryptbrian m. carlson, Nov 4, 2018
  44. 12/12 hash: add an SHA-256 implementation using OpenSSLbrian m. carlson, Nov 4, 2018
  45. Junio C HamanoNov 5, 2018
  46. 00/12 Base SHA-256 implementationbrian m. carlson, Nov 14, 2018
  47. 01/12 sha1-file: rename algorithm to "sha1"brian m. carlson, Nov 14, 2018
  48. 02/12 sha1-file: provide functions to look up hash algorithmsbrian m. carlson, Nov 14, 2018
  49. 03/12 hex: introduce functions to print arbitrary hashesbrian m. carlson, Nov 14, 2018
  50. 04/12 cache: make hashcmp and hasheq work with larger hashesbrian m. carlson, Nov 14, 2018
  51. 06/12 t: make the sha1 test-tool helper genericbrian m. carlson, Nov 14, 2018
  52. 05/12 t: add basic tests for our SHA-1 implementationbrian m. carlson, Nov 14, 2018
  53. 07/12 sha1-file: add a constant for hash block sizebrian m. carlson, Nov 14, 2018
  54. 08/12 t/helper: add a test helper to compute hash speedbrian m. carlson, Nov 14, 2018
  55. 09/12 commit-graph: convert to using the_hash_algobrian m. carlson, Nov 14, 2018
  56. 10/12 Add a base implementation of SHA-256 supportbrian m. carlson, Nov 14, 2018
  57. 11/12 sha256: add an SHA-256 implementation using libgcryptbrian m. carlson, Nov 14, 2018
  58. 12/12 hash: add an SHA-256 implementation using OpenSSLbrian m. carlson, Nov 14, 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.