Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
ac8103ec
Commit
ac8103ec
authored
Jan 23, 2026
by
Tobias Koppers
Committed by
Tobias Koppers
Jan 27, 2026
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve short key hash function
parent
d39e495d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
12 deletions
+30
-12
key.rs
turbopack/crates/turbo-persistence/src/key.rs
+30
-12
No files found.
turbopack/crates/turbo-persistence/src/key.rs
View file @
ac8103ec
...
...
@@ -226,9 +226,24 @@ impl Hasher for ShortKeyHasher {
}
fn
finish
(
&
self
)
->
u64
{
// Convert bytes to u64 and rotate right by 2 bits
// This moves the 2 LSBs to become the 2 MSBs
u64
::
from_be_bytes
(
self
.bytes
)
.rotate_right
(
2
+
8
*
(
8
-
self
.pos
as
u32
))
// Convert collected bytes to u64 (big-endian: first byte is most significant)
let
raw
=
u64
::
from_be_bytes
(
self
.bytes
);
// Rotate right by (8 - pos) bytes to align the key bytes to the LSB side.
// This makes trailing zeros significant: [1] and [1, 0] produce different hashes
// because they have different pos values and thus different rotations.
let
shift
=
8
*
(
8
-
self
.pos
as
u32
);
let
rotated
=
raw
.rotate_right
(
shift
);
// Create bit-reversed version for mixing.
// reverse_bits() mirrors all 64 bits: bit 0 ↔ bit 63, bit 1 ↔ bit 62, etc.
let
reversed
=
rotated
.reverse_bits
();
// Interleave bits from original and reversed using mask 0xCCCC...
// Binary: 0xC = 1100, so this selects bits 2-3, 6-7, 10-11, ... from each nibble.
// Each output nibble gets bits 2-3 from original OR'd with bits 2-3 from reversed,
// combining nearby bits with their distant mirror positions for better distribution.
(
rotated
&
0xCCCC
_CCCC_CCCC_CCCC
)
|
(
reversed
&
0xCCCC
_CCCC_CCCC_CCCC
)
}
}
...
...
@@ -295,45 +310,48 @@ mod tests {
#[test]
fn
hash_short_keys_exact_values
()
{
// Test exact hash values for short keys of various lengths (0-8 bytes)
// Values computed
as: u64::from_be_bytes([bytes padded with zeros]).rotate_right(2)
// Values computed
using bit interleaving
// Empty key (0 bytes)
assert_eq!
(
hash_key
(
&
[
0u8
;
0
]),
0x0000
_0000_0000_0000
);
// 1 byte
assert_eq!
(
hash_key
(
&
[
0x01
]),
0x4000
_0000_0000_0000
);
assert_eq!
(
hash_key
(
&
[
0x01
]),
0x8000
_0000_0000_0000
);
assert_eq!
(
hash_key
(
&
[
0x02
]),
0x4000
_0000_0000_0000
);
assert_eq!
(
hash_key
(
&
[
0x04
]),
0x0000
_0000_0000_0004
);
assert_eq!
(
hash_key
(
&
[
0x08
]),
0x0000
_0000_0000_0008
);
// 2 bytes
assert_eq!
(
hash_key
(
&
[
0x01
,
0x02
]),
0x
8000
_0000_0000_004
0
);
assert_eq!
(
hash_key
(
&
[
0x01
,
0x02
]),
0x
4080
_0000_0000_000
0
);
// 3 bytes
assert_eq!
(
hash_key
(
&
[
0x01
,
0x02
,
0x03
]),
0xC0
00
_0000_0000_408
0
);
assert_eq!
(
hash_key
(
&
[
0x01
,
0x02
,
0x03
]),
0xC0
40
_8000_0000_000
0
);
// 4 bytes
assert_eq!
(
hash_key
(
&
[
0x01
,
0x02
,
0x03
,
0x04
]),
0x00
00
_0000_0040_80C1
);
assert_eq!
(
hash_key
(
&
[
0x01
,
0x02
,
0x03
,
0x04
]),
0x00
C0
_4080_0000_0004
);
// 5 bytes
assert_eq!
(
hash_key
(
&
[
0x01
,
0x02
,
0x03
,
0x04
,
0x05
]),
0x
4000
_0000_4080_C101
0x
8000
_C040_8000_0404
);
// 6 bytes
assert_eq!
(
hash_key
(
&
[
0x01
,
0x02
,
0x03
,
0x04
,
0x05
,
0x06
]),
0x
8000
_0040_80C1_0141
0x
4080
_00C0_4084_0404
);
// 7 bytes
assert_eq!
(
hash_key
(
&
[
0x01
,
0x02
,
0x03
,
0x04
,
0x05
,
0x06
,
0x07
]),
0xC0
00
_4080_C101_4181
0xC0
40
_8000_C444_8404
);
// 8 bytes
assert_eq!
(
hash_key
(
&
[
0x01
,
0x02
,
0x03
,
0x04
,
0x05
,
0x06
,
0x07
,
0x08
]),
0x00
40
_80C1_0141_81C2
0x00
C0
_4084_04C4_4488
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment