Avoid quadratic path copying in prompt trie search - #1607
Closed
markjouh wants to merge 1 commit into
Closed
Conversation
markjouh
marked this pull request as ready for review
July 22, 2026 20:22
pierre427
pushed a commit
to pierre427/mlx-lm
that referenced
this pull request
Aug 21, 2026
Adopts six post-ml-explore#1500 third-party mlx-lm efficiency/correctness PRs, each verified on M5 NAX (see wiki research/efficiency-pr-adoption-2026-07-23.md): ml-explore#1607 de-quadratic PromptTrie.search ml-explore#1571 local_files_only-first _download() ml-explore#1579 fused Metal SSD prefill kernels (complements ml-explore#1586) ml-explore#1584 continuous-batching KV-cache quantization ml-explore#1593 drop import-level sampler mx.compile (server seed bug) ml-explore#1590 never lower RLIMIT_NOFILE on import ml-explore#1555 (Hadamard-rotated KV) was already in-tree — not re-adopted. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
zcbenz
requested changes
Aug 26, 2026
zcbenz
left a comment
Member
There was a problem hiding this comment.
Looks like low effort vibe coding without actually understanding the algorithm.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PromptTrieinstead of copying the growing path at every visited edgeMotivation
PromptTrie.search()looks for the shortest cached sequence extending a shared prompt prefix. Its DFS currently builds every child path withextra + [tok], so a long branch repeatedly copies its full prefix. This makes the server's prompt-cache lookup quadratic in the length of a long divergent suffix before generation begins.The new traversal keeps a single mutable path and uses stack markers to backtrack. It only copies the path when recording a better match; cache contents and selection semantics are unchanged.
Performance
Measured on an Apple M5 Max through the actual
LRUPromptCache.fetch_nearest_cache()path with four cached branches:The 8K result is the median of seven runs; the 32K result is the median of three runs.
Validation
git diff --checkpass