Skip to content

perf: lazy-init _callbacks/_errbacks in ResponseFuture (25ns saving, 2x speedup, 112 bytes saved)#795

Draft
mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul:perf/lazy-init-callbacks
Draft

perf: lazy-init _callbacks/_errbacks in ResponseFuture (25ns saving, 2x speedup, 112 bytes saved)#795
mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul:perf/lazy-init-callbacks

Conversation

@mykaul
Copy link
Copy Markdown

@mykaul mykaul commented Apr 5, 2026

Summary

  • Defer list allocation for _callbacks and _errbacks from ResponseFuture.__init__() to first use in add_callback()/add_errback()
  • On the synchronous execute path (session.execute()), no callbacks are registered, so both lists are never allocated — saving 112 bytes per request
  • All access is under _callback_lock; _set_final_result and _set_final_exception use or () guard to iterate safely when None

Benchmark

Scenario Per-call Speedup
Init with [] x2 50 ns baseline
Init with None x2 25 ns ~2x

Memory saved per request (no callbacks): 112 bytes (2 x 56-byte empty list)

Tests

  • 11 focused unit tests covering lazy init, callback/errback invocation, clear_callbacks, add_callback after result
  • Full unit test suite passes (656 passed)

@mykaul mykaul force-pushed the perf/lazy-init-callbacks branch from 75550f3 to 40c469e Compare April 5, 2026 16:40
Defer list allocation for _callbacks and _errbacks from __init__ to
first use in add_callback()/add_errback(). On the synchronous execute
path (session.execute()), no callbacks are registered, so both lists
are never allocated — saving 112 bytes per request.

All access is under _callback_lock; _set_final_result and
_set_final_exception use 'or ()' guard to iterate safely when None.

Benchmark: 2.2x faster init (0.06 -> 0.03 us), 112 bytes saved/request.
@mykaul mykaul force-pushed the perf/lazy-init-callbacks branch from 40c469e to 89dabcf Compare April 5, 2026 17:31
@mykaul mykaul changed the title perf: lazy-init _callbacks/_errbacks in ResponseFuture perf: lazy-init _callbacks/_errbacks in ResponseFuture (25ns saving, 2x speedup, 112 bytes saved) Apr 7, 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