conversation cache: a match that reaches a pin=N prefix is taken instead of borrowed (#1164)

A borrow copies the donor's checkpoints up to the match, so a match that reached a pinned shared prefix gave the borrower the pin too. Such a match now takes the conversation as before. The borrow's guard is guard()/unguard()/guard_blocked(), and the refusal before evicting counts the pinned conversations along with the donor.

Rebased by hand onto 0.1.44.1 (evict_oldest() honours the guarded donor).

Co-authored-by: BlueKingMuch <53972523+BlueKingMuch@users.noreply.github.com>
This commit is contained in:
Niko1221andBlueKingMuch committed 2026-10-15 07:02:42 +02:00
1 parent c9e571e057
commit 2b296987b9
4 files changed
+98 -37

No files matched your search

+63 -15
View File
@@ -327,17 +327,17 @@ int main() {
const auto cont = cache.best(std::vector<int64_t>{1, 2, 3, 4, 10, 11, 12, 13, 14}, {}, true);
check(cont.live && !cache.borrows(cont), "A going on is taken");
check(!cache.borrows(cache.best(std::vector<int64_t>{5, 6, 7}, {}, true)), "no match, nothing to borrow");
// while the outgoing conversations are parked, the pinned donor is neither evicted nor dropped
cache.pin(sib.serial);
// while the outgoing conversations are parked, the guarded donor is neither evicted nor dropped
cache.guard(sib.serial);
check(cache.put(image({7, 7, 7})) && cache.put(image({8, 8, 8})), "two outgoing conversations park");
check(cache.find(sib.serial) != nullptr && cache.size() == 2 && cache.evictions() == 1 && !cache.pin_blocked(),
"the slot limit evicts the other entry, not the pinned donor");
check(cache.best(std::vector<int64_t>{7, 7, 7, 1}, {}, true).tokens == 0, "the unpinned one went");
check(cache.find(sib.serial) != nullptr && cache.size() == 2 && cache.evictions() == 1 && !cache.guard_blocked(),
"the slot limit evicts the other entry, not the guarded donor");
check(cache.best(std::vector<int64_t>{7, 7, 7, 1}, {}, true).tokens == 0, "the other one went");
SavedConversation rewritten = image({1, 2, 3, 4, 10, 11, 40}); // holds A's deepest checkpoint: supersedes A
rewritten.checkpoints = {cp({1, 2, 3, 4}), cp({1, 2, 3, 4, 10, 11})};
check(cache.put(std::move(rewritten)) && cache.find(sib.serial) != nullptr && cache.superseded() == 0,
"a pinned donor is not dropped as superseded");
cache.unpin();
"a guarded donor is not dropped as superseded");
cache.unguard();
// identity survives index shifts: take an older entry, the donor's serial still finds it
const auto donor_index = cache.index_of(sib.serial);
check(donor_index != SIZE_MAX, "donor indexed by its serial");
@@ -345,7 +345,7 @@ int main() {
"matches report the entry's serial");
}
{
// the outgoing conversation fits only in the pinned donor's room: make_room refuses before evicting anything
// the outgoing conversation fits only in the guarded donor's room: make_room refuses before evicting anything
auto cp = [](std::vector<int32_t> ids) { ConversationCheckpoint c; c.ids = std::move(ids); return c; };
SavedConversation a = image({1, 2, 3, 4, 10, 11});
a.checkpoints = {cp({1, 2, 3, 4}), cp({1, 2, 3, 4, 10})};
@@ -355,10 +355,10 @@ int main() {
check(tight.put(std::move(a)), "park the donor");
const auto m = tight.best(std::vector<int64_t>{1, 2, 3, 4, 50}, {}, true);
check(tight.borrows(m), "a sibling would borrow");
tight.pin(m.serial);
check(!tight.put(image({9, 9, 9})) && tight.pin_blocked(), "no room beside the pinned donor: refused");
tight.guard(m.serial);
check(!tight.put(image({9, 9, 9})) && tight.guard_blocked(), "no room beside the guarded donor: refused");
check(tight.size() == 1 && tight.evictions() == 0, "nothing evicted by the refusal");
tight.unpin();
tight.unguard();
auto taken = tight.take(tight.index_of(m.serial)); // what the caller does then: take it, as before
check(taken.live.ids.size() == 6 && taken.checkpoints.size() == 2 && tight.size() == 0,
"the taken donor is whole");
@@ -367,10 +367,58 @@ int main() {
b.checkpoints = {cp({1, 2, 3, 4}), cp({1, 2, 3, 4, 10})};
check(one_slot.put(std::move(b)), "park in a single slot");
const auto mb = one_slot.best(std::vector<int64_t>{1, 2, 3, 4, 50}, {}, true);
one_slot.pin(mb.serial);
check(!one_slot.put(image({9, 9, 9})) && one_slot.pin_blocked() && one_slot.size() == 1,
"one slot: the pinned donor blocks, nothing evicted");
one_slot.unpin();
one_slot.guard(mb.serial);
check(!one_slot.put(image({9, 9, 9})) && one_slot.guard_blocked() && one_slot.size() == 1,
"one slot: the guarded donor blocks, nothing evicted");
one_slot.unguard();
}
{
// pin=N beside a borrow: the guarded donor and a conversation holding a pinned shared prefix both stay; the
// oldest other entry is the one evicted (the case midhatn tested on #1164)
auto cp = [](std::vector<int32_t> ids, bool pinned = false) {
ConversationCheckpoint c; c.ids = std::move(ids); c.pinned = pinned; return c;
};
SavedConversation doc = image({1, 2, 3, 4, 5, 6, 30, 31});
doc.checkpoints = {cp({1, 2, 3, 4, 5, 6}, true), cp({1, 2, 3, 4, 5, 6, 30})};
SavedConversation donor = image({1, 2, 10, 11, 12});
donor.checkpoints = {cp({1, 2}), cp({1, 2, 10, 11})};
ConversationCache cache(1 << 20, 3);
check(cache.put(std::move(doc)) && cache.put(std::move(donor)) && cache.put(image({7, 7, 7})),
"pinned document, donor and an ordinary conversation parked");
const auto m = cache.best(std::vector<int64_t>{1, 2, 40, 41}, {}, true);
check(cache.borrows(m), "a sibling on the donor's root borrows");
cache.guard(m.serial);
check(cache.put(image({8, 8, 8})) && !cache.guard_blocked() && cache.size() == 3 && cache.evictions() == 1,
"the outgoing conversation parks");
check(cache.find(m.serial) != nullptr &&
cache.best(std::vector<int64_t>{1, 2, 3, 4, 5, 6, 9}, {}, true).tokens == 6 &&
cache.best(std::vector<int64_t>{7, 7, 7, 1}, {}, true).tokens == 0,
"only the ordinary entry was evicted: the donor and the pinned document stay");
check(cache.put(image({9, 9, 9})) && cache.size() == 3 && cache.evictions() == 2 &&
cache.find(m.serial) != nullptr && cache.best(std::vector<int64_t>{8, 8, 8, 1}, {}, true).tokens == 0,
"the next outgoing one evicts the previous outgoing one, still not the donor or the document");
cache.unguard();
ConversationCache two(1 << 20, 2);
SavedConversation doc2 = image({1, 2, 3, 4, 5, 6, 30});
doc2.checkpoints = {cp({1, 2, 3, 4, 5, 6}, true)};
SavedConversation donor2 = image({1, 2, 10, 11, 12});
donor2.checkpoints = {cp({1, 2}), cp({1, 2, 10, 11})};
check(two.put(std::move(doc2)) && two.put(std::move(donor2)), "pinned document and donor fill two slots");
const auto m2 = two.best(std::vector<int64_t>{1, 2, 40}, {}, true);
two.guard(m2.serial);
check(!two.put(image({9, 9, 9})) && two.guard_blocked() && two.size() == 2 && two.evictions() == 0,
"no slot beside the donor and the pinned document: refused before evicting anything");
two.unguard();
// a match that reaches the pinned prefix is taken, not borrowed: one conversation keeps holding the pin
const auto on_pin = two.best(std::vector<int64_t>{1, 2, 3, 4, 5, 6, 77}, {}, true);
check(on_pin.tokens == 6 && !on_pin.live && !two.borrows(on_pin), "a pinned prefix is taken, not borrowed");
ConversationCache three(1 << 20, 3);
SavedConversation doc3 = image({1, 2, 3, 4, 5, 6, 30, 31, 32});
doc3.checkpoints = {cp({1, 2, 3, 4, 5, 6}, true), cp({1, 2, 3, 4, 5, 6, 30}), cp({1, 2, 3, 4, 5, 6, 30, 31})};
check(three.put(std::move(doc3)), "a conversation past its pinned prefix is parked");
const auto past_pin = three.best(std::vector<int64_t>{1, 2, 3, 4, 5, 6, 30, 99}, {}, true);
check(past_pin.tokens == 7 && !three.borrows(past_pin),
"a match past the pinned prefix (the copy would carry the pin) is taken, not borrowed");
}
{
ConversationCache disabled(0,4), no_slots(1024,0);