Skip to content

Commit 4e92dde

Browse files
perf: minimize lock contention (#581)
Signed-off-by: Zhou Fang <[email protected]> Co-authored-by: Yuhan Liu <[email protected]>
1 parent 5c26bdf commit 4e92dde

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/vllm_router/prefix/hashtrie.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,20 @@ async def longest_prefix_match(
8484
"""
8585
node = self.root
8686
match_length = 0
87-
chunk_hashes = self._chunk_and_hash(request)
8887
selected_endpoints = available_endpoints
8988

90-
for i, chunk_hash in enumerate(chunk_hashes):
89+
for chunk_hash in self._chunk_and_hash(request):
9190
async with node.lock:
92-
if chunk_hash in node.children:
93-
94-
node = node.children[chunk_hash]
95-
96-
# reached longest prefix match in currently-available endpoints.
97-
if not node.endpoints.intersection(selected_endpoints):
98-
break
99-
100-
match_length += self.chunk_size
101-
selected_endpoints = node.endpoints.intersection(selected_endpoints)
102-
else:
103-
break
91+
node = node.children.get(chunk_hash)
92+
if not node:
93+
break
94+
async with node.lock:
95+
endpoints = node.endpoints.copy()
96+
intersection = endpoints.intersection(selected_endpoints)
97+
# reached longest prefix match in currently-available endpoints.
98+
if not intersection:
99+
break
100+
match_length += self.chunk_size
101+
selected_endpoints = intersection
104102

105103
return match_length, selected_endpoints

0 commit comments

Comments
 (0)