Thread (4 messages) flat view 4 messages, 1 author, 2025-09-08
STALE333d

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

From: Alexander Monakov <hidden>
Date: 2025-09-08 18:49:52
Subsystem: the rest · Maintainer: Linus Torvalds

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.

Signed-off-by: Alexander Monakov <redacted>
---
 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
diff --git a/xdiff-interface.c b/xdiff-interface.c
index 4971f722b3..e21a7aa0c9 100644
--- a/xdiff-interface.c
+++ b/xdiff-interface.c
@@ -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;
diff --git a/xdiff/xhash.h b/xdiff/xhash.h
new file mode 100644
index 0000000000..27da4288c8
--- /dev/null
+++ b/xdiff/xhash.h
@@ -0,0 +1,52 @@
+/*
+ *  LibXDiff by Davide Libenzi ( File Differential Library )
+ *  Copyright (C) 2003  Davide Libenzi
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, see
+ *  <http://www.gnu.org/licenses/>.
+ *
+ *  Davide Libenzi <davidel@xmailserver.org>
+ *
+ */
+
+#if !defined(XHASH_H)
+#define XHASH_H
+
+
+
+unsigned long xdl_hash_record_with_whitespace(char const **data, char const *top, long flags);
+
+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;
+	}
+	*data = ptr < top ? ptr + 1: ptr;
+
+	return ha;
+}
+
+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);
+}
+
+#endif /* #if !defined(XHASH_H) */
diff --git a/xdiff/xinclude.h b/xdiff/xinclude.h
index a4285ac0eb..68b2d9f1f1 100644
--- a/xdiff/xinclude.h
+++ b/xdiff/xinclude.h
@@ -31,6 +31,7 @@
 #include "xprepare.h"
 #include "xdiffi.h"
 #include "xemit.h"
+#include "xhash.h"
 
 
 #endif /* #if !defined(XINCLUDE_H) */
diff --git a/xdiff/xutils.c b/xdiff/xutils.c
index e070ed649f..0fff5b26a0 100644
--- a/xdiff/xutils.c
+++ b/xdiff/xutils.c
@@ -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;
 
diff --git a/xdiff/xutils.h b/xdiff/xutils.h
index 13f6831047..f51336fce1 100644
--- a/xdiff/xutils.h
+++ b/xdiff/xutils.h
@@ -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,
-- 
2.49.1
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help