From 4b29be3f36b05c4e2479a80712f2c73f216ae031 Mon Sep 17 00:00:00 2001 From: "debing.sun" Date: Wed, 30 Oct 2024 08:45:25 +0800 Subject: [PATCH] Avoid redundant lpGet to boost quicklistCompare (#11533) `lpCompare()` in `quicklistCompare()` will call `lpGet()` again, which would be a waste. The change will result in a boost for all commands that use `quicklistCompre()`, including `linsert`, `lpos` and `lrem`. --- src/quicklist.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/quicklist.c b/src/quicklist.c index 7fe3430fc..6577525a7 100644 --- a/src/quicklist.c +++ b/src/quicklist.c @@ -1244,10 +1244,17 @@ int quicklistDelRange(quicklist *quicklist, const long start, /* compare between a two entries */ int quicklistCompare(quicklistEntry* entry, unsigned char *p2, const size_t p2_len) { - if (unlikely(QL_NODE_IS_PLAIN(entry->node))) { + if (entry->value) { return ((entry->sz == p2_len) && (memcmp(entry->value, p2, p2_len) == 0)); + } else { + /* We use string2ll() to get an integer representation of the + * string 'p2' and compare it to 'entry->longval', it's much + * faster than convert integer to string and comparing. */ + long long sval; + if (string2ll((const char*)p2, p2_len, &sval)) + return entry->longval == sval; } - return lpCompare(entry->zi, p2, p2_len); + return 0; } /* Returns a quicklist iterator 'iter'. After the initialization every