Skip to content

(improvement) Lazily initialize ResponseFuture._errors dict#801

Closed
mykaul wants to merge 1 commit into
scylladb:masterfrom
mykaul:perf/errors-lazy-init
Closed

(improvement) Lazily initialize ResponseFuture._errors dict#801
mykaul wants to merge 1 commit into
scylladb:masterfrom
mykaul:perf/errors-lazy-init

Conversation

@mykaul

@mykaul mykaul commented Apr 6, 2026

Copy link
Copy Markdown

Summary

Change ResponseFuture._errors initialization from {} to None, with a _add_error() helper that creates the dict on first use. Saves one dict allocation per successful query.

Motivation

The vast majority of queries succeed without errors. Pre-allocating an empty {} for _errors on every ResponseFuture wastes memory. By deferring allocation to first error, successful queries (the common case) avoid this allocation entirely.

Benchmark (CPython 3.14, per-call)

Timing:

Path Original Optimized Savings per call
Successful query (common) 176ns 158ns 18ns (10%)
Error path 274ns 256ns 18ns (no regression)

Memory:

Metric Value
Per-query savings 48 bytes (sys.getsizeof({}) = 64B → sys.getsizeof(None) = 16B)
At 1K concurrent in-flight queries ~47KB saved
tracemalloc (10k queries, success path) 5,120 → 0 bytes (100% elimination)

The primary benefit is reduced GC pressure from eliminating a dict allocation on every successful query.

Changes

  • cassandra/cluster.py:
    • __init__: self._errors = None instead of self._errors = {}
    • New _add_error(host, error) method that lazily creates the dict
    • All 7 write sites (self._errors[host] = ...) converted to self._add_error(host, ...)
    • Single read site uses self._errors or {} to handle None (for NoHostAvailable)

Testing

Unit tests pass (58/58 in test_response_future.py and test_cluster.py). Thread safety is preserved — _add_error() runs under the GIL and individual dict assignment is atomic.

Change _errors initialization from {} to None, with a _add_error()
helper that creates the dict on first use. This saves one dict
allocation (~232 bytes on CPython) per successful query, which is the
overwhelmingly common case.

On the read side, the single consumer (NoHostAvailable) uses
'self._errors or {}' to handle the None case. The falsy check in
_send_request_on_next_host() works unchanged since None is falsy.
@mykaul mykaul marked this pull request as draft April 6, 2026 19:26
@mykaul

mykaul commented Apr 6, 2026

Copy link
Copy Markdown
Author

Benchmark results (CPython 3.14, 500k iterations)

Per-call timing:

Path Original Optimized Δ per call
Successful query (common) 176ns 158ns -18ns
Error path 274ns 256ns -18ns (no regression)

Memory savings:

  • sys.getsizeof({}) = 64 bytes, sys.getsizeof(None) = 16 bytes → 48 bytes saved per successful query
  • tracemalloc confirms 100% allocation elimination on the success path (10k queries: 5,120 → 0 bytes)
  • At 1K concurrent in-flight queries: ~47KB saved, reducing GC pressure

@mykaul

mykaul commented Apr 6, 2026

Copy link
Copy Markdown
Author

I don't think it's safe for multi-threading.

@mykaul mykaul closed this Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant