• debing.sun's avatar
    Introduce reusable query buffer for client reads (#13488) · ea3e8b79
    debing.sun authored
    This PR is based on the commits from PR
    https://github.com/valkey-io/valkey/pull/258,
    https://github.com/valkey-io/valkey/pull/593,
    https://github.com/valkey-io/valkey/pull/639
    
    This PR optimizes client query buffer handling in Redis by introducing
    a reusable query buffer that is used by default for client reads. This
    reduces memory usage by ~20KB per client by avoiding allocations for
    most clients using short (<16KB) complete commands. For larger or
    partial commands, the client still gets its own private buffer.
    
    The primary changes are:
    
    * Adding a reusable query buffer `thread_shared_qb` that clients use by
    default.
    * Modifying client querybuf initialization and reset logic.
    * Freeing idle client query buffers when empty to allow reuse of the
    reusable query buffer.
    * Master client query buffers are kept private as their contents need to
    be preserved for replication stream.
    * When nested commands is executed, only the first user uses the reuse
    buffer, and subsequent users will still use the private buffer.
    
    In addition to the memory savings, this change shows a 3% improvement in
    latency and throughput when running with 1000 active clients.
    
    The memory reduction may also help reduce the need to evict clients when
    reaching max memory limit, as the query buffer is the main memory
    consumer per client.
    
    This PR is different from https://github.com/valkey-io/valkey/pull/258
    
    
    1. When a client is in the mid of requiring a reused buffer and
    returning it, regardless of whether the query buffer has changed
    (expanded), we do not update the reused query buffer in the middle, but
    return the reused query buffer (expanded or with data remaining) or
    reset it at the end.
    2. Adding a new thread variable `thread_shared_qb_used` to avoid
    multiple clients requiring the reusable query buffer at the same time.
    
    ---------
    Signed-off-by: default avatarUri Yagelnik <uriy@amazon.com>
    Signed-off-by: default avatarMadelyn Olson <matolson@amazon.com>
    Co-authored-by: default avatarUri Yagelnik <uriy@amazon.com>
    Co-authored-by: default avatarMadelyn Olson <madelyneolson@gmail.com>
    Co-authored-by: default avataroranagra <oran@redislabs.com>
    ea3e8b79
querybuf.tcl 6.35 KB