Skip to contentSkip to conversation

xdiff: refactor xdl_hash_record()

PATCH New Alexander Monakov 1 participant 4 messages Sep 8, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 xdiff: refactor xdl_hash_record()

From: Phillip Wood <[email protected]>

Inline the check for whitespace flags so that the compiler can hoist it out of the loop in xdl_prepare_ctx(). This improves the performance by 8%.

$ hyperfine --warmup=1 -L rev HEAD,HEAD^  --setup='git checkout {rev} -- :/ && make git' ': {rev}; GIT_CONFIG_GLOBAL=/dev/null ./git log --oneline --shortstat v2.0.0..v2.5.0'
Benchmark 1: : HEAD; GIT_CONFIG_GLOBAL=/dev/null ./git log --oneline --shortstat v2.0.0..v2.5.0
  Time (mean ± σ):      1.670 s ±  0.044 s    [User: 1.473 s, System: 0.196 s]
  Range (min … max):    1.619 s …  1.754 s    10 runs

Benchmark 2: : HEAD^; GIT_CONFIG_GLOBAL=/dev/null ./git log --oneline --shortstat v2.0.0..v2.5.0
Time (mean ± σ): 1.801 s ± 0.021 s [User: 1.605 s, System: 0.192 s]
Range (min … max): 1.766 s … 1.831 s 10 runs

Summary
': HEAD^; GIT_CONFIG_GLOBAL=/dev/null ./git log --oneline --shortstat v2.0.0..v2.5.0' ran

    1.08 ± 0.03 times faster than ': HEAD^^; GIT_CONFIG_GLOBAL=/dev/null ./git log --oneline --shortstat v2.0.0..v2.5.0'
---
 xdiff/xutils.c |  7 ++-----
 xdiff/xutils.h | 10 +++++++++-
 2 files changed, 11 insertions(+), 6 deletions(-)
xdiff/xutils.c +2 -5
index 444a108f87..e070ed649f 100644
@@ -249,7 +249,7 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
 	return 1;
 }
 
-static unsigned long xdl_hash_record_with_whitespace(char const **data,
+unsigned long xdl_hash_record_with_whitespace(char const **data,
 		char const *top, long flags) {
 	unsigned long ha = 5381;
 	char const *ptr = *data;
@@ -294,13 +294,10 @@ static unsigned long xdl_hash_record_with_whitespace(char const **data,
 	return ha;
 }
 
-unsigned long xdl_hash_record(char const **data, char const *top, long flags) {
+unsigned long xdl_hash_record_verbatim(char const **data, char const *top) {
 	unsigned long ha = 5381;
 	char const *ptr = *data;
 
-	if (flags & XDF_WHITESPACE_FLAGS)
-		return xdl_hash_record_with_whitespace(data, top, flags);
-
 	for (; ptr < top && *ptr != '\n'; ptr++) {
 		ha += (ha << 5);
 		ha ^= (unsigned long) *ptr;
xdiff/xutils.h +9 -1
index fd0bba94e8..13f6831047 100644
@@ -34,7 +34,15 @@ void *xdl_cha_alloc(chastore_t *cha);
 long xdl_guess_lines(mmfile_t *mf, long sample);
 int xdl_blankline(const char *line, long size, long flags);
 int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags);
-unsigned long xdl_hash_record(char const **data, char const *top, long flags);
+unsigned long xdl_hash_record_verbatim(char const **data, char const *top);
+unsigned long xdl_hash_record_with_whitespace(char const **data, char const *top, long flags);
+static inline unsigned long xdl_hash_record(char const **data, char const *top, long flags)
+{
+	if (flags & XDF_WHITESPACE_FLAGS)
+		return xdl_hash_record_with_whitespace(data, top, flags);
+	else
+		return xdl_hash_record_verbatim(data, top);
+}
 unsigned int xdl_hashbits(unsigned int size);
 int xdl_num_out(char *out, long val);
 int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2,
Signature · 1 line
2.49.1
#1 lore raw

PATCH v2 2/4 xdiff: annotate unlikely branch

XDL_ALLOC_GROW is used in a hot loop in xdl_prepare_ctx. Inform the compiler that branching to the reallocation helper happens rarely to improve code layout.

---
 xdiff/xmacros.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
xdiff/xmacros.h +7 -4
index 8487bb396f..d892e022d4 100644
@@ -23,8 +23,11 @@
 #if !defined(XMACROS_H)
 #define XMACROS_H
 
-
-
+#if __GNUC__ >= 3
+#define XDL_LIKELY(e) __builtin_expect((e), 1)
+#else
+#define XDL_LIKELY(e) (e)
+#endif
 
 #define XDL_MIN(a, b) ((a) < (b) ? (a): (b))
 #define XDL_MAX(a, b) ((a) > (b) ? (a): (b))
@@ -64,8 +67,8 @@ do { \
  * elements) as necessary. Frees p and returns -1 on failure, returns
  * 0 on success
  */
-#define XDL_ALLOC_GROW(p, nr, alloc)	\
-	(-!((nr) <= (alloc) ||		\
+#define XDL_ALLOC_GROW(p, nr, alloc)	    \
+	(-!(XDL_LIKELY((nr) <= (alloc)) ||  \
 	    ((p) = xdl_alloc_grow_helper((p), (nr), &(alloc), sizeof(*(p))))))
 
 #endif /* #if !defined(XMACROS_H) */
Signature · 1 line
2.49.1
#2 lore raw

PATCH v2 3/4 xdiff: move hashing functions to a separate header

In reply to Alexander Monakov

Move xdl_hash_record to a separate header file, and expose xdl_hash_record_verbatim as an inline function to avoid call overhead for short strings.

---
 xdiff-interface.c |  1 +
 xdiff/xhash.h     | 52 +++++++++++++++++++++++++++++++++++++++++++++++
 xdiff/xinclude.h  |  1 +
 xdiff/xutils.c    | 13 ------------
 xdiff/xutils.h    |  9 --------
 5 files changed, 54 insertions(+), 22 deletions(-)
 create mode 100644 xdiff/xhash.h
xdiff-interface.c +1 -0
index 4971f722b3..e21a7aa0c9 100644
@@ -12,6 +12,7 @@
 #include "xdiff/xtypes.h"
 #include "xdiff/xdiffi.h"
 #include "xdiff/xutils.h"
+#include "xdiff/xhash.h"
 
 struct xdiff_emit_state {
 	xdiff_emit_hunk_fn hunk_fn;
xdiff/xhash.h new file +52 -0

52 lines not on this page · raw message

xdiff/xinclude.h +1 -0
index a4285ac0eb..68b2d9f1f1 100644
@@ -31,6 +31,7 @@
 #include "xprepare.h"
 #include "xdiffi.h"
 #include "xemit.h"
+#include "xhash.h"
 
 
 #endif /* #if !defined(XINCLUDE_H) */
xdiff/xutils.c +0 -13
index e070ed649f..0fff5b26a0 100644
@@ -294,19 +294,6 @@ unsigned long xdl_hash_record_with_whitespace(char const **data,
 	return ha;
 }
 
-unsigned long xdl_hash_record_verbatim(char const **data, char const *top) {
-	unsigned long ha = 5381;
-	char const *ptr = *data;
-
-	for (; ptr < top && *ptr != '\n'; ptr++) {
-		ha += (ha << 5);
-		ha ^= (unsigned long) *ptr;
-	}
-	*data = ptr < top ? ptr + 1: ptr;
-
-	return ha;
-}
-
 unsigned int xdl_hashbits(unsigned int size) {
 	unsigned int val = 1, bits = 0;
 
xdiff/xutils.h +0 -9
index 13f6831047..f51336fce1 100644
@@ -34,15 +34,6 @@ void *xdl_cha_alloc(chastore_t *cha);
 long xdl_guess_lines(mmfile_t *mf, long sample);
 int xdl_blankline(const char *line, long size, long flags);
 int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags);
-unsigned long xdl_hash_record_verbatim(char const **data, char const *top);
-unsigned long xdl_hash_record_with_whitespace(char const **data, char const *top, long flags);
-static inline unsigned long xdl_hash_record(char const **data, char const *top, long flags)
-{
-	if (flags & XDF_WHITESPACE_FLAGS)
-		return xdl_hash_record_with_whitespace(data, top, flags);
-	else
-		return xdl_hash_record_verbatim(data, top);
-}
 unsigned int xdl_hashbits(unsigned int size);
 int xdl_num_out(char *out, long val);
 int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2,
Signature · 1 line
2.49.1
#3 lore raw

PATCH v2 4/4 xdiff: use a faster hash in xdl_hash_record_verbatim

In reply to Alexander Monakov

Reimplement xdl_hash_record_verbatim such that it computes two djb2 hashes for even and odd characters independently, each at latency two per character. The new loop is expected to be issue and execution port throughput bound at three cycles per a pair of characters. The more efficient ways to evaluate such hashes involve use of multiplication and higher interleaving factors. The scheme used here is expected to be a reasonable compromise for lines of source code (i.e. not very long strings).

Due to interleaving, the new function does not produce the same hash values as the original. Given values H0 for even characters and H1 for odd characters, we combine them to produce the final hash value as 'H1 * 257 + H0'. The factor 257 is chosen to approximately match the original in quality (collision count in xdiff hashtable).

---
 xdiff/xhash.h | 62 +++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 55 insertions(+), 7 deletions(-)
xdiff/xhash.h +55 -7
index 27da4288c8..1e9e13cc45 100644
@@ -29,16 +29,64 @@ unsigned long xdl_hash_record_with_whitespace(char const **data, char const *top
 
 static inline unsigned long xdl_hash_record_verbatim(char const **data, char const *top)
 {
-	unsigned long ha = 5381;
 	char const *ptr = *data;
-
-	for (; ptr < top && *ptr != '\n'; ptr++) {
-		ha += (ha << 5);
-		ha ^= (unsigned long) *ptr;
+#if 0
+	/*
+	 * djb2 hash (below) is latency-bound; on x86, this baseline form cannot
+	 * run faster than two cycles per iteration, and to achieve that it is
+	 * neccessary to arrange for 'ha * 32' to be computed in parallel with
+	 * 'ha + ch'. To avoid being latency-bound, we run two independent djb2
+	 * hashes over even and odd characters, then combine them in the end.
+	 * The resulting hash is not equivalent to the original djb2.
+	 */
+	unsigned long ha = 5381, ch;
+	while (ptr < top) {
+		if ((ch = *ptr++) == '\n')
+			break;
+		ha = ha * 33 + ch;
 	}
-	*data = ptr < top ? ptr + 1: ptr;
-
+	*data = ptr;
 	return ha;
+#else
+#ifdef __GNUC__
+/*
+ * Compiler reassociation barrier: pretend to modify X and Y to disallow
+ * changing evaluation order with respect to following uses of X or Y.
+ */
+#define REASSOC_FENCE(x, y) __asm__("" : "+r"(x), "+r"(y))
+#else
+#define REASSOC_FENCE(x, y)
+#endif
+	unsigned long h0 = 5381, h1 = 0, ch;
+	/* Process two characters per iteration. */
+	if (top - ptr >= 2) do {
+		if ((ch = *ptr++) == '\n') {
+			*data = ptr;
+			h0 += h1;
+			REASSOC_FENCE(h0, h1);
+			return h1 * 256 + h0;
+		}
+		ch += h0;
+		REASSOC_FENCE(ch, h0);
+		h0 = h0 * 32 + ch;
+
+		if ((ch = *ptr++) == '\n') {
+			*data = ptr;
+			h0 += h1;
+			REASSOC_FENCE(h0, h1);
+			return h1 * 256 + h0;
+		}
+		ch += h1;
+		REASSOC_FENCE(ch, h1);
+		h1 = h1 * 32 + ch;
+
+	} while (ptr < top - 1);
+	*data = top;
+	if (ptr < top && (ch = *ptr++) != '\n')
+		h0 = h0 * 33 + ch;
+	return h1 * 257 + h0;
+#undef REASSOC_FENCE
+#endif
 }
 
 static inline unsigned long xdl_hash_record(char const **data, char const *top, long flags)
Signature · 1 line
2.49.1