SIGN IN SIGN UP

Attribute LZ4 codec context memory to client memory usage (#4702)

Follow-up to #3853, addresses
https://github.com/valkey-io/valkey/pull/3853#discussion_r4017512328.

The LZ4 contexts go through the custom zmalloc allocator, so their
memory is inside `used_memory` but not attributed to anything and shows
up under `used_memory_dataset`. There are two of them: the per-replica
`LZ4F_cctx` on the primary, and the stream reader on the replica
(`LZ4F_dctx` plus a retained input buffer), which lives on the server
struct so per-client accounting never sees it.

`zmalloc_size(ctx)` doesn't work here. The context struct is ~200 bytes
and the staging buffers it allocates lazily are ~80 KiB. So the
allocator callbacks now count the bytes into a `ctx_memory` field on the
owning stream, via `LZ4F_CustomMem.opaqueState`.

`getClientMemoryUsage()` picks up the compressor context in the existing
replica block, and charges the replica-side reader to the primary
client, so it shows up in `mem_clients_normal` like other client memory.

Added a unit test that checks the counter grows when the streaming
buffers are allocated and drops back to zero on free.

Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
S
Sarthak Aggarwal committed
00a8a19554a39facb591837ea41ba4350260919d
Parent: e082696
Committed by GitHub <noreply@github.com> on 9/16/2026, 8:10:03 PM