Skip to content

Commit d1dbc45

Browse files
committed
Add __slots__ to Tablet and Tablets classes for memory optimization
Implemented __slots__ for tablet-related classes in cassandra/tablets.py to reduce per-instance memory overhead. Changes: - Tablet class: Added __slots__ with 3 attributes (first_token, last_token, replicas) - Tablets class: Added __slots__ with 2 attributes (_tablets, _lock) - Removed class-level attribute definitions that conflicted with __slots__ - Preserved and enhanced attribute documentation in class docstrings Memory Impact: Each Tablet instance saves approximately 280-300 bytes by eliminating the per-instance __dict__. Tablet objects are created for each token range in ScyllaDB tablets-enabled clusters. In a cluster with many tablets, this optimization significantly reduces the driver's memory footprint. The Tablets class is typically a singleton per cluster, so the memory savings are minimal there, but the change maintains consistency with the optimization pattern and prevents future __dict__ usage. Testing: - All 7 tablet-specific unit tests pass - Verified __dict__ is not created (memory optimization confirmed) - No functionality changes or regressions Signed-off-by: Yaniv Kaul <yaniv.kaul@scylladb.com>
1 parent bed473c commit d1dbc45

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

cassandra/tablets.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ class Tablet(object):
88
Represents a single ScyllaDB tablet.
99
It stores information about each replica, its host and shard,
1010
and the token interval in the format (first_token, last_token].
11+
12+
Attributes:
13+
first_token: The start of the token range (exclusive)
14+
last_token: The end of the token range (inclusive)
15+
replicas: List of replicas for this tablet, each containing (host_id, shard)
1116
"""
12-
first_token = 0
13-
last_token = 0
14-
replicas = None
17+
__slots__ = ('first_token', 'last_token', 'replicas')
1518

1619
def __init__(self, first_token=0, last_token=0, replicas=None):
1720
self.first_token = first_token
@@ -42,8 +45,14 @@ def replica_contains_host_id(self, uuid: UUID) -> bool:
4245

4346

4447
class Tablets(object):
45-
_lock = None
46-
_tablets = {}
48+
"""
49+
Manages the tablet mapping for ScyllaDB tables.
50+
51+
Attributes:
52+
_tablets: Dictionary mapping (keyspace, table) tuples to lists of Tablet objects
53+
_lock: Thread lock for synchronizing access to the tablets dictionary
54+
"""
55+
__slots__ = ('_tablets', '_lock')
4756

4857
def __init__(self, tablets):
4958
self._tablets = tablets

0 commit comments

Comments
 (0)