Skip to content

Commit c8f4db2

Browse files
committed
perf: skip lock acquisition when no orphaned requests in process_msg
Check orphaned_request_ids truthiness before acquiring the lock. Since orphaned requests are rare (only on timeouts), the set is almost always empty. Skipping the lock in the common case saves ~57 ns per response. The unlocked truthiness check on a set is thread-safe under the GIL. Worst case (false positive): we enter the lock block and re-check, which is correct. Worst case (false negative): an orphaned response is processed normally — acceptable behavior. Benchmark (2M iters, Python 3.14): Empty set (common): 80.6 -> 23.2 ns (3.47x, -57.4 ns/response) Non-empty set (rare): 79.7 -> 87.8 ns (+8.1 ns overhead)
1 parent fb4ee4d commit c8f4db2

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

cassandra/connection.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,11 +1395,17 @@ def process_msg(self, header, body):
13951395
result_metadata = None
13961396
else:
13971397
need_notify_of_release = False
1398-
with self.lock:
1399-
if stream_id in self.orphaned_request_ids:
1400-
self.in_flight -= 1
1401-
self.orphaned_request_ids.remove(stream_id)
1402-
need_notify_of_release = True
1398+
# Fast path: skip lock when no orphaned requests (common case).
1399+
# Reading orphaned_request_ids without the lock is safe: it's a
1400+
# set and we only check truthiness. A false negative just means
1401+
# we'll process the orphaned response normally; a false positive
1402+
# (rare) falls through to the locked check which is correct.
1403+
if self.orphaned_request_ids:
1404+
with self.lock:
1405+
if stream_id in self.orphaned_request_ids:
1406+
self.in_flight -= 1
1407+
self.orphaned_request_ids.remove(stream_id)
1408+
need_notify_of_release = True
14031409
if need_notify_of_release and self._on_orphaned_stream_released:
14041410
self._on_orphaned_stream_released()
14051411

0 commit comments

Comments
 (0)