public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Alexander Monakov <amonakov@ispras•ru>
To: git@vger•kernel.org
Cc: Alexander Monakov <amonakov@ispras•ru>
Subject: [PATCH v2 3/4] xdiff: move hashing functions to a separate header
Date: Mon,  8 Sep 2025 21:49:38 +0300	[thread overview]
Message-ID: <20250908184939.16338-3-amonakov@ispras.ru> (raw)
In-Reply-To: <20250908184939.16338-1-amonakov@ispras.ru>

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 <amonakov@ispras•ru>
---
 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


  parent reply	other threads:[~2025-09-08 18:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-08 18:49 [PATCH v2 1/4] xdiff: refactor xdl_hash_record() Alexander Monakov
2025-09-08 18:49 ` [PATCH v2 2/4] xdiff: annotate unlikely branch Alexander Monakov
2025-09-08 18:49 ` Alexander Monakov [this message]
2025-09-08 18:49 ` [PATCH v2 4/4] xdiff: use a faster hash in xdl_hash_record_verbatim Alexander Monakov

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=20250908184939.16338-3-amonakov@ispras.ru \
    --to=amonakov@ispras$(echo .)ru \
    --cc=git@vger$(echo .)kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox