Basis Lab

Finding Things by Meaning

You Cannot Compare the Question Against Everything

Last timeExact Words and Rough Meaning

Comparing a question against every piece is simple, correct and impossible at scale. The structures that avoid it have build times, memory costs and rules about changing them.

A question has become a list of numbers. There are ten million pieces in the index, each with its own list. The obvious thing to do is compare the question against all ten million and keep the best ten.

This works. It is also the only method that is exactly correct, and for a collection of fifty thousand pieces it is the right choice: a few lines of code with no index to build, no parameters and no failure modes.

At ten million it costs a few billion multiplications per question. That is a substantial fraction of a second of a machine's full attention, on every single question, before anything else has happened.

FIG 1Comparisons for one question, ten million pieces divided into four thousand partitions
stephow far the question is allowed to lookcomparisons to choose the partitionscomparisons inside themcomparisons in totalwhat happened
1every piece, no partitions at allnone to make10,000,00010,000,000A few billion multiplications, and the only answer that is exactly correct. This is the row the other three are measured against.
2one partition of the four thousand4,000, one per representative2,500, the members of that one6,500, or 1,538 times fewerFour thousand partitions over ten million pieces is two thousand five hundred members each. At this setting the choosing costs more than the looking, which is the sign that the division has been taken too far.
3eight partitions of the four thousand4,00020,000, eight lots of 2,50024,000, or 417 times fewerThe ordinary setting. Twenty thousand comparisons against ten million, and the answer is now usually right rather than right.
4forty partitions of the four thousand4,000100,000, forty lots of 2,500104,000, or 96 times fewerFive times the looking for a smaller gain in how often the answer is found, because the partitions opened after the first few are the ones least likely to hold anything.
4 steps
Only the first column was changed, and the word usually in the sentence below is that column written as a number. The second row is worth a second look: four thousand comparisons are spent before any piece is examined, so a collection divided into more partitions than it can afford to choose between has bought nothing. What every row below the first pays is the question whose answer sat in a partition that was not opened.

Two ways to avoid it

Notice what has been given up. The exact method returns the true nearest pieces by construction. A partition index returns the nearest pieces among those it happened to look at, and there is no way to know from the result whether something better was sitting in an unopened partition. The answer is no longer correct, only usually correct, and the word usually is now a parameter you set.

The second family does not divide the space at all. It joins each piece to a few dozen of its neighbours, forming a graph, and searches by walking.

FIG 2A graph walk for one question
stephops taken so fardistance from the questionneighbours checked so farwhat happened
101.420Start at an arbitrary entry point. The distance is roughly what you would expect between two unrelated pieces.
230.8196Three hops in, already in a broadly relevant region of the space.
370.44224Seven hops. Close now, and the improvements per hop are getting small.
4110.39352No neighbour of the current piece is nearer than the current piece. The walk stops here, having compared against three hundred and fifty two pieces rather than ten million.
4 steps
Eleven hops for ten million pieces. Doubling the collection adds roughly one hop, which is why this family scales well. The last row is also the failure mode: stopping because no neighbour improves is not the same as having arrived at the best piece.

Memory, not time

Having fixed the time problem, a second one becomes visible, and on real deployments it binds first.

The lesson stops here

3 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01The Step That Decides Everything After It
  2. 02The Decision Made Before Anything Is Searchedopening only
  3. 03Neither Side of the Comparison Is Textopening only
  4. 04The Two Methods Fail on Opposite Questionsopening only
  5. 05You Cannot Compare the Question Against Everythingyou are here
  6. 06One Knob, and What It Is Really Selling Youopening only
  7. 07The Stage That Can Afford to Be Slowopening only
  8. 08A Hundred Questions You Wrote Yourselfopening only

Read alongside