Skip to content

Avoid quadratic path copying in prompt trie search - #1607

Closed
markjouh wants to merge 1 commit into
ml-explore:mainfrom
markjouh:agent/optimize-prompt-trie-search
Closed

Avoid quadratic path copying in prompt trie search#1607
markjouh wants to merge 1 commit into
ml-explore:mainfrom
markjouh:agent/optimize-prompt-trie-search

Conversation

@markjouh

Copy link
Copy Markdown

Summary

  • reuse one backtracked path while searching PromptTrie instead of copying the growing path at every visited edge
  • preserve the existing LIFO traversal and equal-length tie-breaking behavior
  • add regression coverage for shortest-match selection and tie-breaking

Motivation

PromptTrie.search() looks for the shortest cached sequence extending a shared prompt prefix. Its DFS currently builds every child path with extra + [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:

Divergent suffix Before After Speedup
8,192 tokens 156.5 ms 3.43 ms 45.7x
32,768 tokens 2.822 s 13.57 ms 208.4x

The 8K result is the median of seven runs; the 32K result is the median of three runs.

Validation

  • 200,000 randomized searches matched the previous traversal exactly
  • 30 prompt-trie and prompt-cache tests pass under Python 3.10
  • all 27 server tests pass under Python 3.10, including HTTP request paths
  • Black, isort, and git diff --check pass

@markjouh
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 zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like low effort vibe coding without actually understanding the algorithm.

@zcbenz zcbenz closed this Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants