O(1) slot allocation in ReqToTokenPool.alloc() (#32208)

Co-authored-by: Zhiqiang Xie <xiezhq@stanford.edu>
This commit is contained in:
Dmitrii Sergeev
2026-08-11 11:26:05 -07:00
committed by GitHub
co-authored by Zhiqiang Xie
parent aadb9720fe
commit c58953d90a
3 changed files with 25 additions and 6 deletions
+8 -2
View File
@@ -306,8 +306,14 @@ class ReqToTokenPool:
need_size = len(reqs) - len(reusing)
if need_size > len(self.free_slots):
return None
select_index = self.free_slots[:need_size]
self.free_slots = self.free_slots[need_size:]
if need_size > 0:
# Pop from the tail: O(need_size), unlike a prefix pop which is
# O(len(free_slots)).
select_index = self.free_slots[-need_size:]
del self.free_slots[-need_size:]
else:
# Handled separately: free_slots[-0:] is the entire list, not [].
select_index = []
offset = 0
for r in reqs:
if r.req_pool_idx is None: