Add length check before content comparison in equalStringObjects (#14062)

### Issue 

Previously, even when only string equality needed to be determined, the
comparison logic still performed unnecessary `memcmp()` calls to check
string ordering, even if the lengths were not equal.

### Change
This PR add length check before content comparison in
`equalStringObjects` function.

---------

Co-authored-by: debing.sun <debing.sun@redis.com>
This commit is contained in:
Vitah Lin 2025-05-24 11:47:13 +08:00 committed by GitHub
parent 7998a2a05f
commit 35e15962b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 1 deletions

View File

@ -980,7 +980,7 @@ int collateStringObjects(const robj *a, const robj *b) {
/* Equal string objects return 1 if the two objects are the same from the
* point of view of a string comparison, otherwise 0 is returned. Note that
* this function is faster then checking for (compareStringObject(a,b) == 0)
* this function is faster than checking for (compareStringObject(a,b) == 0)
* because it can perform some more optimization. */
int equalStringObjects(robj *a, robj *b) {
if (a->encoding == OBJ_ENCODING_INT &&
@ -989,6 +989,11 @@ int equalStringObjects(robj *a, robj *b) {
* long is the same. */
return a->ptr == b->ptr;
} else {
if (sdsEncodedObject(a) && sdsEncodedObject(b)
&& sdslen(a->ptr) != sdslen(b->ptr))
{
return 0;
}
return compareStringObjects(a,b) == 0;
}
}