From de7f2f87f74c95857ae91ee77be5a6cda18d2dea Mon Sep 17 00:00:00 2001 From: "debing.sun" Date: Tue, 3 Sep 2024 22:53:13 +0800 Subject: [PATCH] Avoid overhead of comparision function pointer calls in lpFind() (#13503) In #13279 (found by @filipecosta90), for custom lookups, we introduce a comparison function for `lpFind()` to compare entry, but it also introduces some overhead. To avoid the overhead of function pointer calls: 1. Extract the lpFindCb() method into a lpFindCbInternal() method that is easier to inline. 2. Use unlikely to annotate the comparison method, as can only success once. --------- Co-authored-by: Ozan Tezcan --- src/listpack.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/listpack.c b/src/listpack.c index 5d9028e13d..8b9ae196e8 100644 --- a/src/listpack.c +++ b/src/listpack.c @@ -687,8 +687,8 @@ int lpGetIntegerValue(unsigned char *p, long long *lval) { * will be returned. 'user' is passed to this callback. * Skip 'skip' entries between every comparison. * Returns NULL when the field could not be found. */ -unsigned char *lpFindCb(unsigned char *lp, unsigned char *p, - void *user, lpCmp cmp, unsigned int skip) +static inline unsigned char *lpFindCbInternal(unsigned char *lp, unsigned char *p, + void *user, lpCmp cmp, unsigned int skip) { int skipcnt = 0; unsigned char *value; @@ -707,7 +707,7 @@ unsigned char *lpFindCb(unsigned char *lp, unsigned char *p, assert(p >= lp + LP_HDR_SIZE && p + entry_size < lp + lp_bytes); } - if (cmp(lp, p, user, value, ll) == 0) + if (unlikely(cmp(lp, p, user, value, ll) == 0)) return p; /* Reset skip count */ @@ -734,6 +734,12 @@ unsigned char *lpFindCb(unsigned char *lp, unsigned char *p, return NULL; } +unsigned char *lpFindCb(unsigned char *lp, unsigned char *p, + void *user, lpCmp cmp, unsigned int skip) +{ + return lpFindCbInternal(lp, p, user, cmp, skip); +} + struct lpFindArg { unsigned char *s; /* Item to search */ uint32_t slen; /* Item len */ @@ -787,7 +793,7 @@ unsigned char *lpFind(unsigned char *lp, unsigned char *p, unsigned char *s, .s = s, .slen = slen }; - return lpFindCb(lp, p, &arg, lpFindCmp, skip); + return lpFindCbInternal(lp, p, &arg, lpFindCmp, skip); } /* Insert, delete or replace the specified string element 'elestr' of length