mirror of
https://github.com/redis/redis.git
synced 2026-04-21 03:01:35 -04:00
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 <ozantezcan@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user