Guide

What to decide before you store your first hash, in the order the questions tend to arrive.

The reference says what each function does. This page is about the decisions around them. Most of it was learned while preparing to move an application that holds about 90 million hashes from an older library onto this crate.

Which hash

Start from what you need
You need Use You store
Near-duplicate pictures in your own collection, cheaplyperceptual-v18 bytes: one BIGINT
To trade hashes with systems that speak PDQ, or finer matchingpdq-v132 bytes, and a quality score
Pictures where transparency matters, or blockhash hashes from other toolsblockhash-v132 bytes
Near-duplicate texts, long ones, in the least spacesimhash-v18 bytes
Near-duplicate texts, short or edited ones, with a measure of overlapminhash-v1512 bytes
To agree with MinHash signatures datasketch made before its version 2minhash-legacy-v1512 bytes

average-v1 and difference-v1 are the cheapest and the weakest: use them for exact copies and light re-saving. There is no default and no "latest". Whatever you pick, its name is stored with its bits, and upgrading the crate never changes what you have stored.

Hash one size of every picture

This is the mistake that costs the most, and nothing stops you making it. A hash of an original and a hash of a reduced copy of it are not the same hash. With pdq-v1, a picture and its copy shrunk to 512 pixels were a middle 10 bits apart and up to 34. A copy under 256 pixels on its longer side was a middle 20 bits from its original, which is most of the way to Meta's suggested limit of 31.

Two copies of different sizes that were both reduced to 500 pixels first were a middle 0 to 2 bits apart, and never more than 16. So:

The numbers are from the evaluation. perceptual-v1 is far less sensitive to size: it moved at most 2 of its 64 bits on the same pairs.

Know what opening a file does

The definitions start from pixels. Getting from a file to pixels is a separate step, and it is where surprises live:

So keep, beside each hash, what it was a hash of: the size and a digest of the bytes, how they were reduced, and the versions of this crate and of the image crate. When a number looks wrong a year later, that row is how you find out why.

Storing and comparing

Where to draw the line

The crate makes no claim about which distance means "the same picture". That depends on your pictures. Two things decide it.

The floor that chance sets. Unrelated pictures are not infinitely far apart, and the more hashes you hold, the more unrelated ones land near any given hash. If hash bits were coin flips, a collection would hold this many unrelated hashes within each distance of a query:

Unrelated 64-bit hashes expected within a distance, if bits were coin flips
Within 1 million stored 10 million 100 million
6 bits0.0000050.000050.0005
8 bits0.00030.0030.03
10 bits0.010.11
12 bits0.2223
14 bits435353

Real hashes are worse than coin flips, because pictures share structure: skies are at the top, and many pictures are flat. In one collection of about 90 million 64-bit hashes, a typical picture had about 0.5 neighbours at exactly 8 bits, where coin flips predict 0.02, and 79 at exactly 12, where they predict 16. Nearly all of them were unrelated pictures. Past the floor, a "match" is noise. Measure yours: take a few thousand of your own hashes and count what each one finds at every distance.

Pairs a person has judged. Pull pairs at each distance, hide the distance, and have someone say whether each pair is the same picture or an edited copy of it. In one such review of 184 pairs under an older 64-bit hash, all 18 pairs at 4 bits were, 16 of 20 at 5 bits, 9 of 20 at 6, 7 of 17 at 7, 1 of 17 at 8, and none of 60 from 9 to 16. That collection's line belongs at about 5. Yours will be somewhere else, and you find it the same way.

For pdq-v1, Meta suggests 31 bits or fewer, and discarding hashes with a quality of 49 or less. Treat both as places to start.

To find every stored hash within d bits of a new one, split each hash into d + 1 keys with Hash::chunks and index them. Two hashes within d bits always share a key, because d differing bits can spoil at most d keys. Fetch the rows that share a key, then check the real distance.

The guarantee is exact, and so is its limit. Four keys of 16 bits find every hash within 3 bits. Beyond that they find only some:

Four 16-bit keys: share of true neighbours found, by distance
Bits apart 0 to 3 4 5 6 7 8
Found100%90%74%58%44%33%

An index built with four keys and searched at a distance of 8 quietly misses two of every three matches that sit 8 bits away. If your line is at 8, you need nine keys. Real pictures can do a little worse than the arithmetic at short distances: in one collection the four keys found 84% of pairs 4 bits apart, against the 90% predicted.

Flat pictures and quality

A picture with almost nothing in it (one colour, a smooth gradient, a logo on white) gives every hash little to work with. Many unrelated flat pictures land on the same few values, such as all zeros. One such value can sit next to thousands of stored hashes.

Moving from another hash

  1. Compute the new hash into its own column or table, beside the old one. Do not mix them: another library's hash of the same picture is close to this crate's, not equal.
  2. Record what each new hash was a hash of, as above.
  3. Judge pairs under the new hash, blind, before choosing its line. The old hash's line does not carry over, least of all from 64 bits to 256.
  4. Switch readers over only when the new one has been checked against the old on the same questions.

Text

simhash-v1 is one 64-bit value a text, cheap to store and index, and of little use on a text under about 50 words. minhash-v1 is 512 bytes and estimates how much of two texts is shared, to within a few hundredths; it holds up on short and edited texts.

The square beside the crate's name is simhash-v1:f39b4eb36b565f15, the hash of this page's opening sentence, computed by the crate as the page was served. Each page here has its own.