Skip to content

Commit 0a7df47

Browse files
committed
kv-cache : improve find_slot impl
1 parent 4534123 commit 0a7df47

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/llama-kv-cache-unified.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -624,14 +624,15 @@ llama_kv_cache_unified::slot_info llama_kv_cache_unified::find_slot(const llama_
624624
}
625625
}
626626

627-
uint32_t n_found = 0;
628627
uint32_t n_tested = 0;
629628

630629
const uint32_t n_test = cont ? n_tokens : 1;
631630

632631
slot_info res;
633632

634-
res.idxs.resize(n_tokens);
633+
auto & idxs = res.idxs;
634+
635+
idxs.reserve(n_tokens);
635636

636637
while (true) {
637638
if (head_cur + n_test > cells.size()) {
@@ -677,20 +678,18 @@ llama_kv_cache_unified::slot_info llama_kv_cache_unified::find_slot(const llama_
677678
n_tested++;
678679

679680
if (can_use) {
680-
res.idxs[n_found] = idx;
681-
682-
n_found++;
681+
idxs.push_back(idx);
683682
} else {
684683
break;
685684
}
686685
}
687686

688-
if (n_found == n_tokens) {
687+
if (idxs.size() == n_tokens) {
689688
break;
690689
}
691690

692691
if (cont) {
693-
n_found = 0;
692+
idxs.clear();
694693
}
695694

696695
if (n_tested >= cells.size()) {
@@ -700,7 +699,7 @@ llama_kv_cache_unified::slot_info llama_kv_cache_unified::find_slot(const llama_
700699
}
701700

702701
// we didn't find a suitable slot - return empty result
703-
if (n_found < n_tokens) {
702+
if (idxs.size() < n_tokens) {
704703
res.clear();
705704
}
706705

0 commit comments

Comments
 (0)