Avoid zmalloc_size() in kvobjAllocSize() and approximate instead (#14704)
Some checks failed
CI / test-ubuntu-latest (push) Has been cancelled
CI / test-sanitizer-address (push) Has been cancelled
CI / build-debian-old (push) Has been cancelled
CI / build-macos-latest (push) Has been cancelled
CI / build-32bit (push) Has been cancelled
CI / build-libc-malloc (push) Has been cancelled
CI / build-centos-jemalloc (push) Has been cancelled
CI / build-old-chain-jemalloc (push) Has been cancelled
Codecov / code-coverage (push) Has been cancelled
External Server Tests / test-external-standalone (push) Has been cancelled
External Server Tests / test-external-cluster (push) Has been cancelled
External Server Tests / test-external-nodebug (push) Has been cancelled
Spellcheck / Spellcheck (push) Has been cancelled

Avoid zmalloc_size() in kvobjAllocSize() and use approximation instead.

Since for ongoing key allocation histograms work (#14695) we need to
call
kvobjAllocSize() more often on hot paths, using zmalloc_size() would
cause
unnecessary performance overhead.
This commit is contained in:
Slavomir Kaslev 2026-06-05 12:21:38 +03:00 committed by GitHub
parent 37894faeea
commit 4625b8942a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1329,9 +1329,23 @@ size_t kvobjComputeSize(robj *key, kvobj *o, size_t sample_size, int dbid) {
serverPanic("Unknown object type");
}
/* Returns the size in bytes consumed by the object header, key and value in RAM.
* Note that the returned value is accurate approximation of the actual allocated
* size. For performance reasons it accumulates requested size instead in several
* cases (e.g. kvobj allocation, type 5 sds, listpacks, etc) but it does so in a
* self-consistent way.
*/
size_t kvobjAllocSize(kvobj *o) {
/* All kv-objects has at least kvobj header and embedded key */
size_t asize = zmalloc_size(kvobjGetAllocPtr(o));
debugServerAssert(o->iskvobj);
size_t asize = sizeof(kvobj);
/* Add metadata size */
asize += getNumMeta(o->metabits) * sizeof(uint64_t);
/* Add embedded key size */
asize += 1; /* embedded key header size */
asize += sdsAllocSize(kvobjGetKey(o));
/* Add embedded string size */
if (o->encoding == OBJ_ENCODING_EMBSTR)
asize += sdsAllocSize(o->ptr);
if (o->type == OBJ_STRING) {
asize += stringObjectAllocSize(o);