All of lore.kernel.org
 help / color / mirror / Atom feed
From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: git@vger.kernel.org
Cc: "Derrick Stolee" <stolee@gmail.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"Duy Nguyen" <pclouds@gmail.com>,
	"SZEDER Gábor" <szeder.dev@gmail.com>
Subject: [PATCH v3 04/12] cache: make hashcmp and hasheq work with larger hashes
Date: Mon, 22 Oct 2018 02:43:34 +0000	[thread overview]
Message-ID: <20181022024342.489564-5-sandals@crustytoothpaste.net> (raw)
In-Reply-To: <20181022024342.489564-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)

  parent reply	other threads:[~2018-10-22  2:44 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-22  2:43 [PATCH v3 00/12] Base SHA-256 implementation brian m. carlson
2018-10-22  2:43 ` [PATCH v3 01/12] sha1-file: rename algorithm to "sha1" brian m. carlson
2018-10-22  2:43 ` [PATCH v3 02/12] sha1-file: provide functions to look up hash algorithms brian m. carlson
2018-10-22  2:43 ` [PATCH v3 03/12] hex: introduce functions to print arbitrary hashes brian m. carlson
2018-10-22  2:43 ` brian m. carlson [this message]
2018-10-22  2:43 ` [PATCH v3 05/12] t: add basic tests for our SHA-1 implementation brian m. carlson
2018-10-22  2:43 ` [PATCH v3 06/12] t: make the sha1 test-tool helper generic brian m. carlson
2018-10-22  2:43 ` [PATCH v3 07/12] sha1-file: add a constant for hash block size brian m. carlson
2018-10-22  2:43 ` [PATCH v3 08/12] t/helper: add a test helper to compute hash speed brian m. carlson
2018-10-22  2:43 ` [PATCH v3 09/12] commit-graph: convert to using the_hash_algo brian m. carlson
2018-10-24 13:22   ` Derrick Stolee
2018-10-22  2:43 ` [PATCH v3 10/12] Add a base implementation of SHA-256 support brian m. carlson
2018-10-22  9:44   ` SZEDER Gábor
2018-10-23  0:43     ` brian m. carlson
2018-10-27 14:34   ` Jakub Narebski
2018-10-22  2:43 ` [PATCH v3 11/12] sha256: add an SHA-256 implementation using libgcrypt brian m. carlson
2018-10-22  2:43 ` [PATCH v3 12/12] hash: add an SHA-256 implementation using OpenSSL 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=20181022024342.489564-5-sandals@crustytoothpaste.net \
    --to=sandals@crustytoothpaste.net \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=pclouds@gmail.com \
    --cc=stolee@gmail.com \
    --cc=szeder.dev@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.