-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGearHash.js
More file actions
34 lines (32 loc) · 1.25 KB
/
Copy pathGearHash.js
File metadata and controls
34 lines (32 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Gear hash table — the 256 random u32 constants used by FastCDC's rolling hash.
*
* The update rule is: h = ((h << 1) + GEAR[byte]) >>> 0
*
* Each byte adds in a value drawn from this table; the shift makes older bytes'
* contributions decay (and after ~32 iterations fall off entirely), so `h` behaves
* as a rolling hash over a short suffix of the stream. This is exactly the
* property FastCDC needs to detect chunk boundaries by content.
*
* The table is generated deterministically at module load by a small xorshift32
* PRNG seeded with a fixed constant. Doing it this way avoids hand-typing 256
* literals (which had a subtle bit-31 bias in the earlier version) and guarantees
* that two implementations using the same seed produce identical chunk boundaries.
*
* @see https://www.usenix.org/conference/atc16/technical-sessions/presentation/xia
*/
const GEAR_SEED = 0xC0FFEE12;
/** @type {Uint32Array} */
export const GEAR = (() => {
// xorshift32 — uniformly distributed across all 32 bits, period 2^32 - 1.
let s = GEAR_SEED >>> 0;
const out = new Uint32Array(256);
for (let i = 0; i < 256; i++) {
s ^= s << 13;
s ^= s >>> 17;
s ^= s << 5;
s >>>= 0;
out[i] = s;
}
return out;
})();