Skip to content

Commit 9c3e548

Browse files
committed
refactoring
1 parent 02c2124 commit 9c3e548

File tree

9 files changed

+4767
-5130
lines changed

9 files changed

+4767
-5130
lines changed

std/assembly/util/hash.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @ts-ignore: decorator
2-
@inline
31
export function HASH<T>(key: T): u64 {
42
if (isString<T>()) {
53
return hashStr(changetype<string>(key));
@@ -10,7 +8,7 @@ export function HASH<T>(key: T): u64 {
108
if (sizeof<T>() == 4) return hash32(reinterpret<u32>(f32(key)));
119
if (sizeof<T>() == 8) return hash64(reinterpret<u64>(f64(key)));
1210
} else {
13-
if (sizeof<T>() <= 4) return hash32(u32(key));
11+
if (sizeof<T>() <= 4) return hash32(u32(key), sizeof<T>());
1412
if (sizeof<T>() == 8) return hash64(u64(key));
1513
}
1614
return unreachable();
@@ -32,8 +30,10 @@ export function HASH<T>(key: T): u64 {
3230
// @ts-ignore: decorator
3331
@inline const XXH64_SEED: u64 = 0;
3432

35-
function hash32(key: u32): u64 {
36-
var h: u64 = XXH64_SEED + XXH64_P5 + 4;
33+
// @ts-ignore: decorator
34+
@inline
35+
function hash32(key: u32, len: u64 = 4): u64 {
36+
var h: u64 = XXH64_SEED + XXH64_P5 + len;
3737
h ^= u64(key) * XXH64_P1;
3838
h = rotl(h, 23) * XXH64_P2 + XXH64_P3;
3939
h ^= h >> 33;
@@ -44,6 +44,8 @@ function hash32(key: u32): u64 {
4444
return h;
4545
}
4646

47+
// @ts-ignore: decorator
48+
@inline
4749
function hash64(key: u64): u64 {
4850
var h: u64 = XXH64_SEED + XXH64_P5 + 8;
4951
h ^= rotl(key * XXH64_P2, 31) * XXH64_P1;
@@ -68,6 +70,8 @@ function mix2(h: u64, s: u64): u64 {
6870
return (h ^ (rotl(s, 31) * XXH64_P1)) * XXH64_P1 + XXH64_P4;
6971
}
7072

73+
// @ts-ignore: decorator
74+
@inline
7175
function hashStr(key: string): u64 {
7276
if (key === null) {
7377
return XXH64_SEED;
@@ -80,6 +84,7 @@ function hashStr(key: string): u64 {
8084
let s2 = XXH64_SEED + XXH64_P2;
8185
let s3 = XXH64_SEED;
8286
let s4 = XXH64_SEED - XXH64_P1;
87+
let ln = len;
8388

8489
let i = 0;
8590
len -= 32;
@@ -102,11 +107,11 @@ function hashStr(key: string): u64 {
102107
h = mix2(h, s2);
103108
h = mix2(h, s3);
104109
h = mix2(h, s4);
110+
h += <u64>ln;
105111

106-
h += u64(len);
107112
len -= i;
108113
} else {
109-
h = u64(len) + XXH64_SEED + XXH64_P5;
114+
h = <u64>len + XXH64_SEED + XXH64_P5;
110115
}
111116

112117
var i = 0;

0 commit comments

Comments
 (0)