conversation cache: a conversation parked again keeps at most an eighth of each K/V buffer in reserve (was up to 16 MiB per buffer, 0.4-0.5 GB per conversation)

A conversation that was restored and is parked again keeps its unchanged K/V
pages and appends the new ones (92a3d5f). The appended segment reserved room
for later turns, growing geometrically up to 16 MiB - per buffer, and a parked
image has one per K/V layer and part (k, v, their scales, the indexer rows, the
draft layer). With Qwen3.8-Flash-Next (IQ3_S, int8 K/V) a 32K-token main agent
parked again took 1.49 GB instead of 0.97 GB, a 17K-token subagent 1.16 GB
instead of 0.74 GB. The cache counts that capacity against its budget, so with
2816 MiB the reserve decides which conversations are evicted.

The reserve is now at most an eighth of what the buffer held before the append
(at least 64 KiB, and at most 16 MiB as before). Small turns still fill the
reserve instead of adding a transfer each: the 4096-append test keeps its
segment bound, and 256 turns of 1 MiB onto a 16 MiB buffer take 28 segments
(17 before).

The docs said parking falls back to a full capture when the reserve would evict
another conversation; that check went in 6cd29d9. With the reserve bounded the
two captures differ by at most an eighth of the K/V, and the docs now say what
the reserve is.
This commit is contained in:
BlueKingMuch authored and Niko1221 committed 2026-10-15 05:42:52 +02:00
1 parent b6dac80740
commit 53f72571fb
3 files changed
+50 -8

No files matched your search

+36
View File
@@ -58,6 +58,42 @@ int main() {
bytes.visit(0,bytes.size(),[&](const uint8_t*,size_t,size_t){++segments;return true;});
check(segments <= 4,"thousands of small turns do not create thousands of restore transfers");
}
{
// a parked conversation's K/V buffer (16 MiB, as captured) grows by one turn: the reserve for later turns is
// an eighth of the buffer at most, not another 16 MiB segment
ConversationBuffer bytes;
bytes.resize(ConversationBuffer::segment_bytes, 3);
const size_t captured = bytes.bytes();
check(captured - bytes.size() < 1024, "a fresh capture reserves nothing");
const size_t turn = 1 << 20;
const size_t peak = bytes.allocation_peak(bytes.size() + turn);
bytes.resize(bytes.size() + turn, 4);
check(bytes.bytes() <= peak, "a turn's growth stays within the predicted allocation peak");
check(bytes.bytes() - captured <= turn + ConversationBuffer::segment_bytes / 8 + 1024,
"the reserve for later turns is at most an eighth of the buffer");
const size_t reserved = bytes.bytes();
bytes.resize(bytes.size() + turn / 2, 5);
check(bytes.bytes() == reserved, "the next small turn fills the reserve instead of allocating");
size_t segments = 0;
bytes.visit(0, bytes.size(), [&](const uint8_t*, size_t, size_t) { ++segments; return true; });
check(segments == 2, "the reserve extends the turn's segment");
}
{
// many turns of 1 MiB onto a 16 MiB buffer: the reserve grows with the buffer, so the segments do not
// multiply one per turn, and the reserve never exceeds an eighth of the buffer
ConversationBuffer bytes;
bytes.resize(ConversationBuffer::segment_bytes, 1);
for (int k = 0; k < 256; ++k) {
const size_t before = bytes.size();
const size_t peak = bytes.allocation_peak(before + (1 << 20));
bytes.resize(before + (1 << 20), 2);
check(bytes.bytes() <= peak, "each turn stays within the predicted allocation peak");
check(bytes.bytes() - bytes.size() <= before / 8 + 4096, "the reserve stays within an eighth");
}
size_t segments = 0;
bytes.visit(0, bytes.size(), [&](const uint8_t*, size_t, size_t) { ++segments; return true; });
check(segments <= 64, "256 turns of 1 MiB take far fewer than 256 segments");
}
{
ConversationKv layer;
layer.k.resize(400);