Skip to content

perf: cache framer extension values on Conn and pool framer structs #800

@dkropachev

Description

@dkropachev

Problem

Every call to Conn.exec() and Conn.recv() invokes newFramerWithExts(), which:

  1. Allocates a new framer struct on the heap
  2. Allocates a 128-byte buffer (make([]byte, defaultBufSize)) that grows dynamically per frame
  3. Scans cqlProtoExts three times via findCQLProtoExtByName() to extract flagLWT, rateLimitingErrorCode, and tabletsRoutingV1 -- values that are constant for the lifetime of a connection
  4. Recomputes flags and proto from compressor and version -- also constant per connection

There are 4 call sites in conn.go (lines 849, 859, 890, 1219), all passing identical arguments: c.compressor, c.version, c.cqlProtoExts, c.logger. This means every query round-trip does 2 allocations and 3 linear scans that produce the same results every time.

Additionally, setTabletSupported(framer.tabletsRoutingV1) is called after every newFramerWithExts -- an atomic.StoreInt32 that stores the same value each time.

Proposed Fix

  1. Cache extension-derived values on Conn: Compute flagLWT, rateLimitingErrorCode, tabletsRoutingV1, flags, and proto once during connection setup and store them on the Conn struct. Eliminate the 3x findCQLProtoExtByName scans and redundant setTabletSupported calls per query.

  2. Pool framer structs: Use sync.Pool to reuse framer structs and their backing buffers across queries, eliminating 2 heap allocations per query.

Expected Impact

Based on CPU profiling of the benchmark suite, newFramerWithExts and runtime.mallocgc are significant contributors to per-query CPU time. This optimization should reduce allocations by 2 per query and eliminate redundant computation on every frame operation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions