@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / af376ca19d
Fork 0

A high-throughput and memory-efficient inference and serving engine for LLMs

Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git

Synced Nov 15, 01:00 UTC

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

Signed-off-by: Jialin Ouyang <Jialin.Ouyang@gmail.com>

Jialin Ouyang committed Jul 21, 2025 at 22:37 UTC af376ca19d4588b1d5ace72ffc0b4bbd778c15f2
1 file changed +21 -16
vllm/v1/core/block_pool.py
+21 -16
@@ -243,22 +243,27 @@ class BlockPool:
243 True if the block is evicted, False otherwise.
244 """
245 block_hash = block.block_hash
246 - if block_hash and block_hash in self.cached_block_hash_to_block:
247 - block.reset_hash()
248 - del self.cached_block_hash_to_block[block_hash][block.block_id]
249 -
250 - if len(self.cached_block_hash_to_block[block_hash]) == 0:
251 - del self.cached_block_hash_to_block[block_hash]
252 -
253 - if self.enable_kv_cache_events:
254 - # FIXME (Chen): Not sure whether we should return `hash_value`
255 - # or `(hash_value, group_id)` here. But it's fine now because
256 - # we disable hybrid kv cache manager when kv cache event is
257 - # enabled, so there is only one group.
258 - self.kv_event_queue.append(
259 - BlockRemoved(block_hashes=[block_hash.get_hash_value()]))
260 - return True
261 - return False
246 + if block_hash is None:
247 + # The block doesn't have hash, eviction is not needed
248 + return False
249 + blocks_by_id = self.cached_block_hash_to_block.get(block_hash)
250 + if blocks_by_id is None:
251 + # block_hash not found in cached_block_hash_to_block,
252 + # eviction is not needed
253 + return False
254 + block.reset_hash()
255 + blocks_by_id.pop(block.block_id, None)
256 + if blocks_by_id:
257 + del self.cached_block_hash_to_block[block_hash]
258 +
259 + if self.enable_kv_cache_events:
260 + # FIXME (Chen): Not sure whether we should return `hash_value`
261 + # or `(hash_value, group_id)` here. But it's fine now because
262 + # we disable hybrid kv cache manager when kv cache event is
263 + # enabled, so there is only one group.
264 + self.kv_event_queue.append(
265 + BlockRemoved(block_hashes=[block_hash.get_hash_value()]))
266 + return True
267
268 def touch(self, blocks: tuple[list[KVCacheBlock], ...]) -> None:
269 """Touch a block increases its reference count by 1, and may remove