Repository: valkey-io/valkey
File: src/lazyfree.c (C)

Code before:
void freeFunctionsAsync(functionsLibCtx *functions_lib_ctx, list *engine_callbacks) {
    if (functionsLibCtxFunctionsLen(functions_lib_ctx) > LAZYFREE_THRESHOLD) {
        atomic_fetch_add_explicit(&lazyfree_objects, functionsLibCtxFunctionsLen(functions_lib_ctx),
                                  memory_order_relaxed);
        bioCreateLazyFreeJob(lazyFreeFunctionsCtx, 2, functions_lib_ctx, engine_callbacks);
    } else {
        functionsLibCtxFree(functions_lib_ctx, NULL, engine_callbacks);
    }
}

/* Free replication backlog referencing buffer blocks and rax index. */
void freeReplicationBacklogRefMemAsync(list *blocks, rax *index) {
    if (listLength(blocks) > LAZYFREE_THRESHOLD || raxSize(index) > LAZYFREE_THRESHOLD) {
        atomic_fetch_add_explicit(&lazyfree_objects, listLength(blocks) + raxSize(index), memory_order_relaxed);
        bioCreateLazyFreeJob(lazyFreeReplicationBacklogRefMem, 2, blocks, index);
    } else {
        listRelease(blocks);
        raxFree(index);
    }
}


/* Free replicaKeysWithExpire dict, if the dict is huge enough, free it in async way. */
void freeReplicaKeysWithExpireAsync(dict *replica_keys_with_expire) {
    if (dictSize(replica_keys_with_expire) > LAZYFREE_THRESHOLD) {
        bioCreateLazyFreeJob(lazyFreeReplicaKeysWithExpire,1,replica_keys_with_expire);
    } else {
        dictRelease(replica_keys_with_expire);
    }
}

Review comment (suggestion):
```suggestion
        atomic_fetch_add_explicit(&lazyfree_objects, dictSize(replica_keys_with_expire), memory_order_relaxed);
        bioCreateLazyFreeJob(lazyFreeReplicaKeysWithExpire, 1, replica_keys_with_expire);
```

Code after:
}

/* Free functions ctx, if the functions ctx contains enough functions, free it in async way. */
void freeFunctionsAsync(functionsLibCtx *functions_lib_ctx, list *engine_callbacks) {
    if (functionsLibCtxFunctionsLen(functions_lib_ctx) > LAZYFREE_THRESHOLD) {
        atomic_fetch_add_explicit(&lazyfree_objects, functionsLibCtxFunctionsLen(functions_lib_ctx),
                                  memory_order_relaxed);
        bioCreateLazyFreeJob(lazyFreeFunctionsCtx, 2, functions_lib_ctx, engine_callbacks);
    } else {
        functionsLibCtxFree(functions_lib_ctx, NULL, engine_callbacks);
    }
}

/* Free replication backlog referencing buffer blocks and rax index. */
void freeReplicationBacklogRefMemAsync(list *blocks, rax *index) {
    if (listLength(blocks) > LAZYFREE_THRESHOLD || raxSize(index) > LAZYFREE_THRESHOLD) {
        atomic_fetch_add_explicit(&lazyfree_objects, listLength(blocks) + raxSize(index), memory_order_relaxed);
        bioCreateLazyFreeJob(lazyFreeReplicationBacklogRefMem, 2, blocks, index);
    } else {
        listRelease(blocks);
        raxFree(index);
    }
}


/* Free replicaKeysWithExpire dict, if the dict is huge enough, free it in async way. */
void freeReplicaKeysWithExpireAsync(dict *replica_keys_with_expire) {
    if (dictSize(replica_keys_with_expire) > LAZYFREE_THRESHOLD) {
        atomic_fetch_add_explicit(&lazyfree_objects, dictSize(replica_keys_with_expire), memory_order_relaxed);
        bioCreateLazyFreeJob(lazyFreeReplicaKeysWithExpire, 1, replica_keys_with_expire);
    } else {
        dictRelease(replica_keys_with_expire);
    }
}