Subject: [PATCH v2 2/4] xdiff: annotate unlikely branch

From: Alexander Monakov <[email-masked]>
Date: Mon, 8 Sep 2025 21:49:37 +0300

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.

Signed-off-by: Alexander Monakov <[email-masked]>
---
 xdiff/xmacros.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/xdiff/xmacros.h b/xdiff/xmacros.h
index 8487bb396f..d892e022d4 100644
--- a/xdiff/xmacros.h
+++ b/xdiff/xmacros.h
@@ -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) */
-- 
2.49.1