(improvement) Add __slots__ to _Frame to eliminate per-instance __dict__ (30-40ns saving, 264 bytes saved per call)#802
Draft
mykaul wants to merge 1 commit intoscylladb:masterfrom
Draft
Conversation
_Frame is instantiated for every response frame received from the server. Adding __slots__ eliminates the per-instance __dict__ allocation (~104 bytes on CPython), reducing memory pressure on high-throughput workloads. _Frame only has 6 fixed attributes (version, flags, stream, opcode, body_offset, end_pos) and is never monkey-patched or dynamically extended.
Author
Benchmark results (CPython 3.14, 500k iterations)Per-instance memory:
Per-call timing:
Bulk allocation (10k frames):
|
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
Add
__slots__to the_Frameclass incassandra/connection.py. Eliminates per-instance__dict__allocation.Motivation
_Frameis instantiated for every response frame received from the server. It has exactly 6 fixed attributes (version,flags,stream,opcode,body_offset,end_pos) and is never monkey-patched or dynamically extended. Adding__slots__removes the per-instance__dict__, reducing memory pressure on high-throughput workloads.Benchmark (CPython 3.14, per-call)
Memory:
__dict__)__slots__)Timing:
Bulk (10K frames):
Changes
cassandra/connection.py: Add__slots__ = ('version', 'flags', 'stream', 'opcode', 'body_offset', 'end_pos')to_FrameTesting
Unit tests pass (28/28 in test_connection.py). Verified that
_Frameinstances no longer have__dict__.