Skip to content

Commit 2bca4f1

Browse files
committed
Add __slots__ to Token classes and TokenMap for memory optimization
Add __slots__ to frequently instantiated token-related classes to reduce memory overhead by preventing dynamic attribute creation: * Token base class with __slots__ = ('value',) * All Token subclasses (HashToken, Murmur3Token, MD5Token, BytesToken) with __slots__ = () * TokenMap class with comprehensive attribute tuple and consolidated docstring documentation These classes are instantiated frequently during cluster metadata parsing and query planning. The __slots__ optimization prevents Python from creating __dict__ for each instance, significantly reducing memory usage especially in large clusters with many tokens. All existing functionality is preserved and unit tests pass. Signed-off-by: Yaniv Kaul <yaniv.kaul@scylladb.com>
1 parent 9b54ce0 commit 2bca4f1

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

cassandra/metadata.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,30 +1729,18 @@ def export_as_string(self):
17291729
class TokenMap(object):
17301730
"""
17311731
Information about the layout of the ring.
1732-
"""
17331732
1734-
token_class = None
1735-
"""
1736-
A subclass of :class:`.Token`, depending on what partitioner the cluster uses.
1737-
"""
1738-
1739-
token_to_host_owner = None
1740-
"""
1741-
A map of :class:`.Token` objects to the :class:`.Host` that owns that token.
1742-
"""
1743-
1744-
tokens_to_hosts_by_ks = None
1745-
"""
1746-
A map of keyspace names to a nested map of :class:`.Token` objects to
1747-
sets of :class:`.Host` objects.
1748-
"""
1749-
1750-
ring = None
1751-
"""
1752-
An ordered list of :class:`.Token` instances in the ring.
1733+
Attributes:
1734+
token_class: A subclass of :class:`.Token`, depending on what partitioner the cluster uses.
1735+
token_to_host_owner: A map of :class:`.Token` objects to the :class:`.Host` that owns that token.
1736+
tokens_to_hosts_by_ks: A map of keyspace names to a nested map of :class:`.Token` objects to sets of :class:`.Host` objects.
1737+
ring: An ordered list of :class:`.Token` instances in the ring.
1738+
_metadata: Metadata reference for internal use.
1739+
_rebuild_lock: Lock for thread-safe operations.
17531740
"""
17541741

1755-
_metadata = None
1742+
__slots__ = ('token_class', 'token_to_host_owner', 'tokens_to_hosts_by_ks',
1743+
'ring', '_metadata', '_rebuild_lock')
17561744

17571745
def __init__(self, token_class, token_to_host_owner, all_tokens, metadata):
17581746
self.token_class = token_class
@@ -1815,6 +1803,8 @@ class Token(object):
18151803
Abstract class representing a token.
18161804
"""
18171805

1806+
__slots__ = ('value',)
1807+
18181808
def __init__(self, token):
18191809
self.value = token
18201810

@@ -1854,6 +1844,8 @@ class NoMurmur3(Exception):
18541844

18551845
class HashToken(Token):
18561846

1847+
__slots__ = ()
1848+
18571849
@classmethod
18581850
def from_string(cls, token_string):
18591851
""" `token_string` should be the string representation from the server. """
@@ -1866,6 +1858,8 @@ class Murmur3Token(HashToken):
18661858
A token for ``Murmur3Partitioner``.
18671859
"""
18681860

1861+
__slots__ = ()
1862+
18691863
@classmethod
18701864
def hash_fn(cls, key):
18711865
if murmur3 is not None:
@@ -1884,6 +1878,8 @@ class MD5Token(HashToken):
18841878
A token for ``RandomPartitioner``.
18851879
"""
18861880

1881+
__slots__ = ()
1882+
18871883
@classmethod
18881884
def hash_fn(cls, key):
18891885
if isinstance(key, str):
@@ -1896,6 +1892,8 @@ class BytesToken(Token):
18961892
A token for ``ByteOrderedPartitioner``.
18971893
"""
18981894

1895+
__slots__ = ()
1896+
18991897
@classmethod
19001898
def from_string(cls, token_string):
19011899
""" `token_string` should be the string representation from the server. """

0 commit comments

Comments
 (0)