fix(json_family): Fix memory tracking for JSON (#4777)

* fix(json_family): Fix memory tracking for JSON

fixes dragonflydb#4725

Signed-off-by: Stepan Bagritsevich <stefan@dragonflydb.io>

* refactor: address comments

Signed-off-by: Stepan Bagritsevich <stefan@dragonflydb.io>

* small fix

Signed-off-by: Stepan Bagritsevich <stefan@dragonflydb.io>

---------

Signed-off-by: Stepan Bagritsevich <stefan@dragonflydb.io>
This commit is contained in:
Stepan Bagritsevich 2025-03-20 10:23:58 +01:00 committed by GitHub
parent ce5c44b57b
commit ca65a49a0f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 52 additions and 7 deletions

View file

@ -55,6 +55,19 @@ size_t QlMAllocSize(quicklist* ql, bool slow) {
return node_size + ql->count * 16; // we account for each member 16 bytes.
}
size_t UpdateSize(size_t size, int64_t update) {
if (update >= 0) {
return size + update;
}
int64_t result = static_cast<int64_t>(size) + update;
if (result < 0) {
DCHECK(false) << "Can't decrease " << size << " from " << -update;
LOG_EVERY_N(ERROR, 30) << "Can't decrease " << size << " from " << -update;
}
return result;
}
inline void FreeObjSet(unsigned encoding, void* ptr, MemoryResource* mr) {
switch (encoding) {
case kEncodingStrMap2: {
@ -927,10 +940,7 @@ void CompactObj::SetJson(JsonType&& j) {
void CompactObj::SetJsonSize(int64_t size) {
if (taglen_ == JSON_TAG && JsonEnconding() == kEncodingJsonCons) {
// JSON.SET or if mem hasn't changed from a JSON op then we just update.
if (size < 0) {
DCHECK(static_cast<int64_t>(u_.json_obj.cons.bytes_used) >= size);
}
u_.json_obj.cons.bytes_used += size;
u_.json_obj.cons.bytes_used = UpdateSize(u_.json_obj.cons.bytes_used, size);
}
}