[Core] Minimize number of dict lookup in _maybe_evict_cached_block (#21281)

Signed-off-by: Jialin Ouyang <Jialin.Ouyang@gmail.com>
This commit is contained in:
Jialin Ouyang
2025-07-21 22:37:34 -07:00
committed by GitHub
parent e7b2042681
commit af376ca19d

View File

@@ -243,22 +243,27 @@ class BlockPool:
True if the block is evicted, False otherwise. True if the block is evicted, False otherwise.
""" """
block_hash = block.block_hash block_hash = block.block_hash
if block_hash and block_hash in self.cached_block_hash_to_block: if block_hash is None:
block.reset_hash() # The block doesn't have hash, eviction is not needed
del self.cached_block_hash_to_block[block_hash][block.block_id] return False
blocks_by_id = self.cached_block_hash_to_block.get(block_hash)
if blocks_by_id is None:
# block_hash not found in cached_block_hash_to_block,
# eviction is not needed
return False
block.reset_hash()
blocks_by_id.pop(block.block_id, None)
if blocks_by_id:
del self.cached_block_hash_to_block[block_hash]
if len(self.cached_block_hash_to_block[block_hash]) == 0: if self.enable_kv_cache_events:
del self.cached_block_hash_to_block[block_hash] # FIXME (Chen): Not sure whether we should return `hash_value`
# or `(hash_value, group_id)` here. But it's fine now because
if self.enable_kv_cache_events: # we disable hybrid kv cache manager when kv cache event is
# FIXME (Chen): Not sure whether we should return `hash_value` # enabled, so there is only one group.
# or `(hash_value, group_id)` here. But it's fine now because self.kv_event_queue.append(
# we disable hybrid kv cache manager when kv cache event is BlockRemoved(block_hashes=[block_hash.get_hash_value()]))
# enabled, so there is only one group. return True
self.kv_event_queue.append(
BlockRemoved(block_hashes=[block_hash.get_hash_value()]))
return True
return False
def touch(self, blocks: tuple[list[KVCacheBlock], ...]) -> None: def touch(self, blocks: tuple[list[KVCacheBlock], ...]) -> None:
"""Touch a block increases its reference count by 1, and may remove """Touch a block increases its reference count by 1, and may remove