Skip to content

Commit 9efcc45

Browse files
committed
Add __slots__ to metadata classes for memory optimization
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 <yaniv.kaul@scylladb.com>
1 parent d062ec2 commit 9efcc45

1 file changed

Lines changed: 18 additions & 35 deletions

File tree

cassandra/metadata.py

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,38 +1647,26 @@ def escape_name(name):
16471647
class ColumnMetadata(object):
16481648
"""
16491649
A representation of a single column in a table.
1650-
"""
1651-
1652-
table = None
1653-
""" The :class:`.TableMetadata` this column belongs to. """
1654-
1655-
name = None
1656-
""" The string name of this column. """
1657-
1658-
cql_type = None
1659-
"""
1660-
The CQL type for the column.
1661-
"""
1662-
1663-
is_static = False
1664-
"""
1665-
If this column is static (available in Cassandra 2.1+), this will
1666-
be :const:`True`, otherwise :const:`False`.
1667-
"""
16681650
1669-
is_reversed = False
1670-
"""
1671-
If this column is reversed (DESC) as in clustering order
1651+
Attributes:
1652+
table: The :class:`.TableMetadata` this column belongs to.
1653+
name: The string name of this column.
1654+
cql_type: The CQL type for the column.
1655+
is_static: If this column is static (available in Cassandra 2.1+), this will
1656+
be :const:`True`, otherwise :const:`False`.
1657+
is_reversed: If this column is reversed (DESC) as in clustering order.
1658+
_cass_type: Internal cache for the cassandra type.
16721659
"""
16731660

1674-
_cass_type = None
1661+
__slots__ = ('table', 'name', 'cql_type', 'is_static', 'is_reversed', '_cass_type')
16751662

16761663
def __init__(self, table_metadata, column_name, cql_type, is_static=False, is_reversed=False):
16771664
self.table = table_metadata
16781665
self.name = column_name
16791666
self.cql_type = cql_type
16801667
self.is_static = is_static
16811668
self.is_reversed = is_reversed
1669+
self._cass_type = None
16821670

16831671
def __str__(self):
16841672
return "%s %s" % (self.name, self.cql_type)
@@ -1687,21 +1675,16 @@ def __str__(self):
16871675
class IndexMetadata(object):
16881676
"""
16891677
A representation of a secondary index on a column.
1690-
"""
1691-
keyspace_name = None
1692-
""" A string name of the keyspace. """
16931678
1694-
table_name = None
1695-
""" A string name of the table this index is on. """
1696-
1697-
name = None
1698-
""" A string name for the index. """
1699-
1700-
kind = None
1701-
""" A string representing the kind of index (COMPOSITE, CUSTOM,...). """
1679+
Attributes:
1680+
keyspace_name: A string name of the keyspace.
1681+
table_name: A string name of the table this index is on.
1682+
name: A string name for the index.
1683+
kind: A string representing the kind of index (COMPOSITE, CUSTOM, ...).
1684+
index_options: A dict of index options.
1685+
"""
17021686

1703-
index_options = {}
1704-
""" A dict of index options. """
1687+
__slots__ = ('keyspace_name', 'table_name', 'name', 'kind', 'index_options')
17051688

17061689
def __init__(self, keyspace_name, table_name, index_name, kind, index_options):
17071690
self.keyspace_name = keyspace_name

0 commit comments

Comments
 (0)