Skip to content

Commit 3f955a4

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 3f955a4

1 file changed

Lines changed: 20 additions & 21 deletions

File tree

cassandra/metadata.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,30 +1729,19 @@ 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
1737+
sets of :class:`.Host` objects.
1738+
ring: An ordered list of :class:`.Token` instances in the ring.
1739+
_metadata: Metadata reference for internal use.
1740+
_rebuild_lock: Lock for thread-safe operations.
17531741
"""
17541742

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

17571746
def __init__(self, token_class, token_to_host_owner, all_tokens, metadata):
17581747
self.token_class = token_class
@@ -1815,6 +1804,8 @@ class Token(object):
18151804
Abstract class representing a token.
18161805
"""
18171806

1807+
__slots__ = ('value',)
1808+
18181809
def __init__(self, token):
18191810
self.value = token
18201811

@@ -1854,6 +1845,8 @@ class NoMurmur3(Exception):
18541845

18551846
class HashToken(Token):
18561847

1848+
__slots__ = ()
1849+
18571850
@classmethod
18581851
def from_string(cls, token_string):
18591852
""" `token_string` should be the string representation from the server. """
@@ -1866,6 +1859,8 @@ class Murmur3Token(HashToken):
18661859
A token for ``Murmur3Partitioner``.
18671860
"""
18681861

1862+
__slots__ = ()
1863+
18691864
@classmethod
18701865
def hash_fn(cls, key):
18711866
if murmur3 is not None:
@@ -1884,6 +1879,8 @@ class MD5Token(HashToken):
18841879
A token for ``RandomPartitioner``.
18851880
"""
18861881

1882+
__slots__ = ()
1883+
18871884
@classmethod
18881885
def hash_fn(cls, key):
18891886
if isinstance(key, str):
@@ -1896,6 +1893,8 @@ class BytesToken(Token):
18961893
A token for ``ByteOrderedPartitioner``.
18971894
"""
18981895

1896+
__slots__ = ()
1897+
18991898
@classmethod
19001899
def from_string(cls, token_string):
19011900
""" `token_string` should be the string representation from the server. """

0 commit comments

Comments
 (0)