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
| You need | Use | You store |
|---|---|---|
| Near-duplicate pictures in your own collection, cheaply | perceptual-v1 | 8 bytes: one BIGINT |
| To trade hashes with systems that speak PDQ, or finer matching | pdq-v1 | 32 bytes, and a quality score |
| Pictures where transparency matters, or blockhash hashes from other tools | blockhash-v1 | 32 bytes |
| Near-duplicate texts, long ones, in the least space | simhash-v1 | 8 bytes |
| Near-duplicate texts, short or edited ones, with a measure of overlap | minhash-v1 | 512 bytes |
| To agree with MinHash signatures datasketch made before its version 2 | minhash-legacy-v1 | 512 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:
- Choose one size, reduce every picture to it the same way, and hash that. A common reduction helps; it does not hurt.
- Never compare a hash of an original with a hash of a thumbnail.
- Decide what to do with pictures smaller than your chosen size before you start. Leaving them out is a fair answer.
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:
- A photo's stored orientation is not applied. A picture taken sideways hashes sideways. Turn it first if that matters.
- Only
blockhash-v1sees transparency. For the others a see-through pixel is whatever colour is stored under it, often black. - A JPEG cut off half way still opens, with its missing part filled in, and hashes without an error. Hashing is not a check that a file is whole.
- PNG and JPEG only. Open anything else yourself and pass the pixels in.
- A JPEG can decode to slightly different pixels under a later version of the decoder, so its hash can move by a few bits when you upgrade.
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
- The text form carries the name:
perceptual-v1:c3f00f3c99a5a55a. Comparing hashes of two definitions is an error, never a number. - A 64-bit hash fits a
BIGINTthroughto_i64. Half of all hashes have their top bit set and so are negative there. XOR and counting bits still work on them. Ordering them, or reading them back as an unsigned number in another language, does not. - A 256-bit hash has no integer form. Store its 32 bytes in a fixed-width binary column.
- A MinHash signature is 512 bytes. Only its text form says which of the two definitions made it, so keep one definition per column.
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:
| Within | 1 million stored | 10 million | 100 million |
|---|---|---|---|
| 6 bits | 0.000005 | 0.00005 | 0.0005 |
| 8 bits | 0.0003 | 0.003 | 0.03 |
| 10 bits | 0.01 | 0.1 | 1 |
| 12 bits | 0.2 | 2 | 23 |
| 14 bits | 4 | 35 | 353 |
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.
Finding near matches
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:
| Bits apart | 0 to 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|
| Found | 100% | 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.
- Filter and order by the real distance in the same query that reads the keys. A query that takes "the first thousand rows sharing a key" and checks distance afterwards throws away most true matches once a key is common.
- For 64-bit hashes, try the plain approach before building anything. A straight scan of 90 million 64-bit values, counting bits against each, took under half a second on one laptop core. For many workloads that is the whole index.
- Keys do not stretch to 256 bits. At Meta's 31 bits, thirty-two keys of 8 bits each match about one row in 256. That suits thousands of hashes; for millions, use an index built for Hamming distance.
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.
- With
pdq-v1, keep the quality score and leave hashes of 49 or less out of matching. - With the 64-bit hashes, leave out the handful of values that turn up far more often than chance allows.
- Before you act on a match against a list of known pictures, count how many stored hashes sit near that list entry. One that matches thousands of things is telling you nothing.
Moving from another hash
- 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.
- Record what each new hash was a hash of, as above.
- 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.
- 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.
- Neither changes a character before hashing. If capitals, runs of spaces or accents should not count, lower-case, collapse and normalise the text yourself first.
- Start MinHash with runs of 5 characters:
minhash_v1_shingles(text, 5). - The word splitter knows ASCII spaces and punctuation only. For text written without spaces, use runs of characters.
- To find probable matches among stored signatures, index band keys from
Signature::band_keys. Thirty-two bands of four values find a pair that shares half its runs 87% of the time, and one that shares 70% almost always. - An empty text matches every other empty text exactly. Check for it.
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.