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 7, 04:00 UTC
perf: add __slots__ to KVCacheBlock (#36164)
Signed-off-by: cong-or <conchubhar.gannon@gmail.com>
cong-or committed
Mar 6, 2026 at 06:04 UTC
57c84ff129de4ab8072bbc9756942650803001ef
2 files changed
+13
-1
tests/v1/core/test_kv_cache_utils.py
+12
@@ -202,6 +202,18 @@ def test_kv_cache_block():
202
assert block.block_hash is None
203
204
205
+def test_kv_cache_block_uses_slots():
206
+ block = KVCacheBlock(block_id=0)
207
+
208
+ # Slots eliminate per-instance __dict__, saving ~264 bytes per block.
209
+ # At 100K+ blocks this avoids tens of MB of overhead and GC pressure.
210
+ assert not hasattr(block, "__dict__")
211
+
212
+ # Verify that slots actually prevent dynamic attribute assignment.
213
+ with pytest.raises(AttributeError):
214
+ block.unexpected_field = True
215
+
216
+
217
def test_free_kv_cache_block_queue_initialization():
218
# Test with a single block
219
block = KVCacheBlock(block_id=0)
vllm/v1/core/kv_cache_utils.py
+1
-1
@@ -106,7 +106,7 @@ def init_none_hash(hash_fn: Callable[[Any], bytes]):
106
NONE_HASH = BlockHash(hash_fn(hash_seed))
107
108
109
-@dataclass
109
+@dataclass(slots=True)
110
class KVCacheBlock:
111
"""KV-cache block metadata."""
112