Skip to content

Conversation

@mykaul
Copy link

@mykaul mykaul commented Jan 15, 2026

  • Add slots definition to _Frame class in connection.py
  • Optimizes memory usage for protocol frame objects
  • _Frame is created for every protocol message, making this a high-impact optimization
  • Reduces object overhead from ~300+ bytes to ~40-60 bytes per frame
  • All existing functionality preserved, comprehensive tests pass

Pre-review checklist

  • I have split my patch into logically separate commits.
  • All commit messages clearly explain what they change and why.
  • I added relevant tests for new features and bug fixes.
  • All commits compile, pass static checks and pass test.
  • PR description sums up the changes and reasons why they should be introduced.
  • I have provided docstrings for the public items that I want to introduce.
  • I have adjusted the documentation in ./docs/source/.
  • I added appropriate Fixes: annotations to PR description.

@mykaul mykaul marked this pull request as draft January 15, 2026 13:39
- Add __slots__ definition to _Frame class in connection.py
- Optimizes memory usage for protocol frame objects
- _Frame is created for every protocol message, making this a high-impact optimization
- Reduces object overhead from ~300+ bytes to ~40-60 bytes per frame
- All existing functionality preserved, comprehensive tests pass

Signed-off-by: Yaniv Kaul <[email protected]>
Add __slots__ to _MessageType base class and all protocol message classes
to reduce memory overhead. Each message instance saves approximately 280-300
bytes by eliminating the per-instance __dict__.

Changes:
- _MessageType: Added __slots__ with 'custom_payload' and 'tracing'
- _MessageType: Added __init__ to initialize attributes with proper defaults
- _DecodableMessageType: Removed duplicate 'custom_payload' from __slots__
- All message subclasses: Added super().__init__() calls for proper initialization

Key attributes must be in __slots__ because:
- custom_payload: Accessed by encode_message() for ALL message types (line 1127)
- tracing: Set on message instances in cluster.py (line 2972)

Without these in __slots__, attempting to set them raises AttributeError,
causing connection failures with: 'OptionsMessage' object has no attribute
'custom_payload'

Message classes covered:
- Outgoing (_MessageType): StartupMessage, OptionsMessage, QueryMessage,
  ExecuteMessage, PrepareMessage, BatchMessage, RegisterMessage, etc.
- Incoming (_DecodableMessageType): ResultMessage, EventMessage,
  AuthenticateMessage, SupportedMessage, etc.

Signed-off-by: Yaniv Kaul <[email protected]>
Implemented __slots__ for ColumnMetadata and IndexMetadata classes
in cassandra/metadata.py to reduce per-instance memory overhead.

Changes:
- ColumnMetadata: Added __slots__ with 6 attributes (table, name,
  cql_type, is_static, is_reversed, _cass_type)
- IndexMetadata: Added __slots__ with 5 attributes (keyspace_name,
  table_name, name, kind, index_options)
- Removed class-level attribute definitions that conflicted with
  __slots__ declarations
- All attribute initialization moved to __init__ methods
- Preserved attribute documentation in class docstrings

Memory Impact:
Each instance saves approximately 280-300 bytes by eliminating the
per-instance __dict__. These metadata objects are created frequently
when parsing schema information, making this optimization valuable
for clusters with many tables and indexes.

Testing:
- All 648 unit tests pass successfully
- Metadata-specific tests: 53 passed, 2 skipped
- No functionality changes or regressions

Signed-off-by: Yaniv Kaul <[email protected]>
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