Prefix Sharing Is a Sorting Problem
cs.DS, cs.CL
Submitted: 2026-09-12
Updated: 2026-09-12
Comments: 15 pages. Code: https://github.com/Darrenus/prefix-sharing-is-sorting
Code: https://github.com/Darrenus/prefix-sharing-is-sorting
License: http://creativecommons.org/licenses/by/4.0/
The gist: LLM serving reuses KV cache by exact prefix match, so when a prompt is assembled from a set of reusable pieces -- retrieved passages, tool definitions, few-shot exemplars -- the order chosen for
Terminology
Abstract
LLM serving reuses KV cache by exact prefix match, so when a prompt is assembled from a set of reusable pieces -- retrieved passages, tool definitions, few-shot exemplars -- the order chosen for those pieces determines how much computation can be shared. Every deployed system fixes that order by a single global convention. We prove this is optimal only when requests contain at most two pieces, and asymptotically wrong in general. Our main result is a structure theorem: the minimum prefix-trie cost equals min H sum x w(x) t x(H) over binary hierarchies H on the requests, where t x(H) is the canonical decomposition size of the set of requests needing chunk x. Choosing chunk orders is therefore equivalent to choosing one hierarchy over requests. The identity yields an O(3 m) exact algorithm, identifies the two-chunk case as minimum vertex cover, and shows that on the leave-one-out family the optimum is the minimum external path length of a binary tree -- the merge-sort recursion -- so a global order pays Theta(n 2) against a true cost of Theta(n log n). Agglomerative clustering by common intersection is a tight 1/2-approximation for the achievable saving. On BM25 retrieval traces over three BEIR corpora the resulting layout reduces prefill by 17-36% against production RAG ordering, and the margin widens with retrieval depth as the theory predicts. Serving requests in the hierarchy's DFS order finally lets a cache holding one request's context attain the unbounded-cache optimum exactly, so cache capacity and reorder window act as substitutes.
Related papers
- Cascaded Learned Bloom Filter for Optimizing Model-Filter Size Balance and Fast Rejection
- Edge-Private Matching Kernels Through Local Decoding
- Local Node Differential Privacy
- Cheaper by the Batch: Shared Traversal for Genotype Graph Editing
- Scalable Algorithms for Approximate DNF Model Counting
- On the Approximation Relationship between Optimizing Ratio of Submodular (RS) and Difference of Submodular (DS) Functions