perf: eliminate heap allocations in gocql wrapper chainable methods#9621
Open
mykaul wants to merge 2 commits intotemporalio:mainfrom
Open
perf: eliminate heap allocations in gocql wrapper chainable methods#9621mykaul wants to merge 2 commits intotemporalio:mainfrom
mykaul wants to merge 2 commits intotemporalio:mainfrom
Conversation
All chainable methods on the query wrapper (Consistency, WithTimestamp, WithContext, PageSize, PageState, Idempotent) previously allocated a new *query struct on every call. Upstream gocql mutates in place for all methods except WithContext (which does a shallow copy). Mirror this by mutating and returning the receiver directly, removing one heap allocation per call site in the persistence hot path. Note: WithContext mutates the receiver in place (via the underlying gocql shallow copy behavior), unlike gocql's method signature which suggests a copy.
Same pattern as query.go: mutate and return the receiver instead of allocating a new *batch on each chainable call. WithContext mutates in place via gocql's underlying shallow copy.
Contributor
Author
|
I've tested this extensively with ScyllaDB 2026.1, Cassandra 5.0 and Cassandra 3.11, running multiple Omes workloads. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Consistency,WithContext,PageSize, etc.) by mutating in-place and returning*queryImplinstead of allocating new wrapper objects.WithContext,WithTimestamp).Motivation
The gocql query/batch wrappers use a fluent (method chaining) pattern where each method previously created a new
queryImpl/batchImplon the heap. Since every Cassandra query goes through these wrappers, this creates significant allocation pressure on the hot path — each query allocates 3-5 wrapper objects that are immediately discarded.Approach
Changed chainable methods from copy-on-return to mutate-in-place semantics. This is safe because all callers use method chaining (e.g.,
q.Consistency(x).WithContext(ctx).PageSize(n)) and never retain intermediate copies.Before:
After:
Benchmark Results (Cassandra 5.0, throughput_stress scenario)
Testing
-race.