Skip to content

Commit 2818aef

Browse files
committed
perf: streamline tablet payload parsing in _set_result
- Replace double dict lookup ('in' + .get()) with single .get() + None check - Use tuple unpacking instead of three separate indexing operations - Guard add_tablet with 'if tablet is not None' (from_row can return None for empty replicas — previously this was silently passed through) - Inline single-use locals (protocol, keyspace, table) Saves ~13 ns on the tablet-hit path (noise-level, but cleaner code).
1 parent 7157b11 commit 2818aef

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

cassandra/cluster.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4734,21 +4734,17 @@ def _set_result(self, host, connection, pool, response):
47344734
self._warnings = getattr(response, 'warnings', None)
47354735
self._custom_payload = getattr(response, 'custom_payload', None)
47364736

4737-
if self._custom_payload and self.session.cluster.control_connection._tablets_routing_v1 and 'tablets-routing-v1' in self._custom_payload:
4738-
protocol = self.session.cluster.protocol_version
4737+
if self._custom_payload and self.session.cluster.control_connection._tablets_routing_v1:
47394738
info = self._custom_payload.get('tablets-routing-v1')
4740-
ctype = ResponseFuture._TABLET_ROUTING_CTYPE
4741-
if ctype is None:
4742-
ctype = types.lookup_casstype('TupleType(LongType, LongType, ListType(TupleType(UUIDType, Int32Type)))')
4743-
ResponseFuture._TABLET_ROUTING_CTYPE = ctype
4744-
tablet_routing_info = ctype.from_binary(info, protocol)
4745-
first_token = tablet_routing_info[0]
4746-
last_token = tablet_routing_info[1]
4747-
tablet_replicas = tablet_routing_info[2]
4748-
tablet = Tablet.from_row(first_token, last_token, tablet_replicas)
4749-
keyspace = self.query.keyspace
4750-
table = self.query.table
4751-
self.session.cluster.metadata._tablets.add_tablet(keyspace, table, tablet)
4739+
if info is not None:
4740+
ctype = ResponseFuture._TABLET_ROUTING_CTYPE
4741+
if ctype is None:
4742+
ctype = types.lookup_casstype('TupleType(LongType, LongType, ListType(TupleType(UUIDType, Int32Type)))')
4743+
ResponseFuture._TABLET_ROUTING_CTYPE = ctype
4744+
first_token, last_token, tablet_replicas = ctype.from_binary(info, self.session.cluster.protocol_version)
4745+
tablet = Tablet.from_row(first_token, last_token, tablet_replicas)
4746+
if tablet is not None:
4747+
self.session.cluster.metadata._tablets.add_tablet(self.query.keyspace, self.query.table, tablet)
47524748

47534749
if isinstance(response, ResultMessage):
47544750
if response.kind == RESULT_KIND_SET_KEYSPACE:

0 commit comments

Comments
 (0)