You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The defrag callback is implemented as "copy + free" instead of calling the VM_DefragAlloc API. The original intention is for easy implementation. Each JSON document is a RapidJSON document tree that may have lots of small allocations. Without using "copy + free", we would have to traverse the JSON tree to find every pointer and call the VM_DefragAlloc for each pointer, which would be very complicated.
However, the problem with this approach is that it is very likely we will reallocate the exact same pointer we just freed, because the memory will be freed to tcache and it will be used for the next allocation. The way the defrag avoids that is that it uses API to explicitly tell the allocator to skip the tcache when doing alloc & free for defrag.
To fix the issue, there are several options:
Calling VM_DefragAlloc: Walk through the JSON tree, for every malloc'ed chunk, call VM_DefragAlloc.
Redirect alloc/free:
defrag:
redirect alloc/free to zmalloc_no_tcache/zfree_no_tcache
copy json object
free the old json object
redirect alloc/free to back to RedisModule_Alloc/RedisModule_Free
Expose zmalloc_no_tcache/zfree_no_tcache as module APIs.
Flush tcache:
*defrag:
flush tcache
copy json object
free the old json object
Add a module API for flushing tcache.
disable tcache:
defrag:
VM_DisableTcache (a new api)
copy json object
free the old json object
VM_EnableTcache (a new api)
Add module API VM_DisableTcache and VM_EnableTcache
Modify VM_Alloc/Free to check if tcache is disabled. If yes, use zmalloc_no_cache/zfree_no_cache.
The text was updated successfully, but these errors were encountered:
The defrag callback is implemented as "copy + free" instead of calling the VM_DefragAlloc API. The original intention is for easy implementation. Each JSON document is a RapidJSON document tree that may have lots of small allocations. Without using "copy + free", we would have to traverse the JSON tree to find every pointer and call the VM_DefragAlloc for each pointer, which would be very complicated.
However, the problem with this approach is that it is very likely we will reallocate the exact same pointer we just freed, because the memory will be freed to tcache and it will be used for the next allocation. The way the defrag avoids that is that it uses API to explicitly tell the allocator to skip the tcache when doing alloc & free for defrag.
To fix the issue, there are several options:
Calling VM_DefragAlloc: Walk through the JSON tree, for every malloc'ed chunk, call VM_DefragAlloc.
Redirect alloc/free:
*defrag:
The text was updated successfully, but these errors were encountered: