From ef33b5d10b19d77361c2e630b0f75985b3d3202e Mon Sep 17 00:00:00 2001 From: sb32445 Date: Fri, 2 Oct 2026 19:04:10 +0200 Subject: [PATCH] kv-cache : limit seq_rm to the used cell range seq_rm walked all cells of the cache on every call. Cells outside [used_min, used_max_p1) are empty, so skip them. With a large context and speculative decoding (seq_rm after rejected drafts) this showed up in the host profile. Co-Authored-By: Claude Sonnet 5.5 --- src/llama-kv-cache.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp index 3df5cedd082e..d406353d564c 100644 --- a/src/llama-kv-cache.cpp +++ b/src/llama-kv-cache.cpp @@ -403,7 +403,8 @@ bool llama_kv_cache::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) { uint32_t new_head = cells.size(); - for (uint32_t i = 0; i < cells.size(); ++i) { + // cells outside the used range are empty + for (uint32_t i = cells.used_min(), i_end = cells.used_max_p1(); i < i_end; ++i) { if (!cells.pos_in(i, p0, p1)) { continue; } @@ -427,7 +428,7 @@ bool llama_kv_cache::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) { uint32_t new_head = cells.size(); - for (uint32_t i = 0; i < cells.size(); ++i) { + for (uint32_t i = cells.used_min(), i_end = cells.used_max_p1(); i < i_end; ++i) { if (!cells.pos_in(i, p0, p1)) { continue; }