11/35
Part 3 of 7 · AutocompleteLearn

Ranking at query time costs the most on a short prefix, whose subtree is huge

The cost has three parts:

  • Find the node for a prefix of p letters: O(p).
  • Walk the s nodes under it: O(s).
  • Rank its m words: O(m log k).

A short prefix has a huge subtree.

On the 100,000 words, each scored in sample order by rng = random.Random(3) with rng.randint(1, 1_000_000), with k = 5 and the best of 7 runs:

best_k on the 100,000 words, log scale
s: 10,679 words under it
7,154 µs
co: 3,008
1,943 µs
pre: 1,480
981 µs
trans: 238
163 µs
zyx: 0
0.4 µs

Seven milliseconds on the first keystroke, and the first keystroke is the one every user types.