Skip to content

Commit fc30a96

Browse files
committed
update cashew for some hashing optimizations
1 parent b7e8dd3 commit fc30a96

4 files changed

+177
-44
lines changed

tests/test_other.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1901,7 +1901,7 @@ def test_js_optimizer(self):
19011901
['asm', 'eliminate']),
19021902
(path_from_root('tools', 'test-js-optimizer-asm-regs.js'), open(path_from_root('tools', 'test-js-optimizer-asm-regs-output.js')).read(),
19031903
['asm', 'registerize']),
1904-
(path_from_root('tools', 'test-js-optimizer-asm-regs-harder.js'), [open(path_from_root('tools', 'test-js-optimizer-asm-regs-harder-output.js')).read(), open(path_from_root('tools', 'test-js-optimizer-asm-regs-harder-output2.js')).read()],
1904+
(path_from_root('tools', 'test-js-optimizer-asm-regs-harder.js'), [open(path_from_root('tools', 'test-js-optimizer-asm-regs-harder-output.js')).read(), open(path_from_root('tools', 'test-js-optimizer-asm-regs-harder-output2.js')).read(), open(path_from_root('tools', 'test-js-optimizer-asm-regs-harder-output3.js')).read()],
19051905
['asm', 'registerizeHarder']),
19061906
(path_from_root('tools', 'test-js-optimizer-asm-regs-min.js'), open(path_from_root('tools', 'test-js-optimizer-asm-regs-min-output.js')).read(),
19071907
['asm', 'registerize', 'minifyLocals']),

tools/optimizer/istring.h

+11-7
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ namespace cashew {
1414
struct IString {
1515
const char *str;
1616

17-
static size_t hash_c(const char *str) { // TODO: optimize?
18-
uint64_t ret = 0;
19-
while (*str) {
20-
ret = (ret*6364136223846793005ULL) + *str;
21-
str++;
17+
static size_t hash_c(const char *str) { // see http://www.cse.yorku.ca/~oz/hash.html
18+
unsigned int hash = 5381;
19+
int c;
20+
while ((c = *str++)) {
21+
hash = ((hash << 5) + hash) ^ c;
2222
}
23-
return (size_t)ret;
23+
return (size_t)hash;
2424
}
2525

2626
class CStringHash : public std::hash<const char *> {
@@ -74,6 +74,9 @@ struct IString {
7474
//assert((str == other.str) == !strcmp(str, other.str));
7575
return str != other.str; // fast!
7676
}
77+
bool operator<(const IString& other) const {
78+
return strcmp(str ? str : "", other.str ? other.str : "") < 0;
79+
}
7780

7881
char operator[](int x) {
7982
return str[x];
@@ -96,7 +99,8 @@ namespace std {
9699

97100
template <> struct hash<cashew::IString> : public unary_function<cashew::IString, size_t> {
98101
size_t operator()(const cashew::IString& str) const {
99-
return cashew::IString::hash_c(str.c_str());
102+
size_t hash = size_t(str.str);
103+
return hash = ((hash << 5) + hash) ^ 5381; /* (hash * 33) ^ c */
100104
}
101105
};
102106

tools/test-js-optimizer-asm-regs-harder-output2.js

+28-36
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,41 @@
1-
function asm(d2, i1) {
2-
d2 = +d2;
3-
i1 = i1 | 0;
4-
i1 = d2 + d2 | 0;
5-
d2 = d(Math_max(10, Math_min(5, f())));
6-
i1 = i1 + 2 | 0;
7-
print(i1);
8-
d2 = d2 * 5;
9-
return d2;
10-
}
11-
12-
function _doit(i2, i3, i1) {
1+
function asm(d1, i2) {
2+
d1 = +d1;
133
i2 = i2 | 0;
4+
i2 = d1 + d1 | 0;
5+
d1 = d(Math_max(10, Math_min(5, f())));
6+
i2 = i2 + 2 | 0;
7+
print(i2);
8+
d1 = d1 * 5;
9+
return d1;
10+
}
11+
function _doit(i3, i2, i1) {
1412
i3 = i3 | 0;
13+
i2 = i2 | 0;
1514
i1 = i1 | 0;
16-
i2 = STACKTOP;
17-
_printf(__str | 0, (tempInt = STACKTOP, STACKTOP = STACKTOP + 8 | 0, HEAP32[(tempInt & 16777215) >> 2] = i3, HEAP32[(tempInt + 4 & 16777215) >> 2] = i1, tempInt));
18-
STACKTOP = i2;
15+
i3 = STACKTOP;
16+
_printf(__str | 0, (tempInt = STACKTOP, STACKTOP = STACKTOP + 8 | 0, HEAP32[(tempInt & 16777215) >> 2] = i2, HEAP32[(tempInt + 4 & 16777215) >> 2] = i1, tempInt));
17+
STACKTOP = i3;
1918
return 0 | 0;
2019
}
21-
2220
function stackRestore(i1) {
2321
i1 = i1 | 0;
2422
STACKTOP = i1;
2523
}
26-
27-
function switchey(d2, i1) {
28-
d2 = +d2;
29-
i1 = i1 | 0;
30-
switch (d2 | 0) {
24+
function switchey(d1, i2) {
25+
d1 = +d1;
26+
i2 = i2 | 0;
27+
switch (d1 | 0) {
3128
case 0:
32-
i1 = d2 + d2 | 0;
33-
d2 = d(Math_max(10, Math_min(5, f())));
34-
i1 = i1 + 2 | 0;
35-
print(i1);
36-
d2 = d2 * 5;
37-
return d2;
29+
i2 = d1 + d1 | 0;
30+
d1 = d(Math_max(10, Math_min(5, f())));
31+
i2 = i2 + 2 | 0;
32+
print(i2);
33+
d1 = d1 * 5;
34+
return d1;
3835
case 1:
3936
return 20;
4037
}
4138
}
42-
4339
function switchey2() {
4440
var i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0, d6 = +0, d7 = +0;
4541
i4 = STACKTOP;
@@ -73,7 +69,6 @@ function switchey2() {
7369
}
7470
return 0;
7571
}
76-
7772
function iffey() {
7873
var i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0, d6 = +0, d7 = +0;
7974
i4 = STACKTOP;
@@ -101,7 +96,6 @@ function iffey() {
10196
}
10297
return 0;
10398
}
104-
10599
function labelledJump(i2) {
106100
i2 = i2 | 0;
107101
var i1 = 0, i3 = 0;
@@ -117,7 +111,6 @@ function labelledJump(i2) {
117111
}
118112
return i2;
119113
}
120-
121114
function linkedVars() {
122115
var i1 = 0, i2 = 0;
123116
while (1) {
@@ -136,10 +129,9 @@ function linkedVars() {
136129
}
137130
return i2 + i1;
138131
}
139-
140-
function deadCondExpr(i1) {
141-
i1 = i1 | 0;
142-
var i2 = 0;
143-
return i2 | 0;
132+
function deadCondExpr(i2) {
133+
i2 = i2 | 0;
134+
var i1 = 0;
135+
return i1 | 0;
144136
}
145137

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
function asm(d1, i2) {
2+
d1 = +d1;
3+
i2 = i2 | 0;
4+
i2 = d1 + d1 | 0;
5+
d1 = d(Math_max(10, Math_min(5, f())));
6+
i2 = i2 + 2 | 0;
7+
print(i2);
8+
d1 = d1 * 5;
9+
return d1;
10+
}
11+
function _doit(i3, i1, i2) {
12+
i3 = i3 | 0;
13+
i1 = i1 | 0;
14+
i2 = i2 | 0;
15+
i3 = STACKTOP;
16+
_printf(__str | 0, (tempInt = STACKTOP, STACKTOP = STACKTOP + 8 | 0, HEAP32[(tempInt & 16777215) >> 2] = i1, HEAP32[(tempInt + 4 & 16777215) >> 2] = i2, tempInt));
17+
STACKTOP = i3;
18+
return 0 | 0;
19+
}
20+
function stackRestore(i1) {
21+
i1 = i1 | 0;
22+
STACKTOP = i1;
23+
}
24+
function switchey(d1, i2) {
25+
d1 = +d1;
26+
i2 = i2 | 0;
27+
switch (d1 | 0) {
28+
case 0:
29+
i2 = d1 + d1 | 0;
30+
d1 = d(Math_max(10, Math_min(5, f())));
31+
i2 = i2 + 2 | 0;
32+
print(i2);
33+
d1 = d1 * 5;
34+
return d1;
35+
case 1:
36+
return 20;
37+
}
38+
}
39+
function switchey2() {
40+
var i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0, d6 = +0, d7 = +0;
41+
i4 = STACKTOP;
42+
STACKTOP = STACKTOP + 8 | 0;
43+
i3 = 1;
44+
while (1) switch (i3 | 0) {
45+
case 1:
46+
i5 = i4 | 0;
47+
__ZN6RandomC1Ev(i5);
48+
i1 = 0;
49+
i2 = 0;
50+
i3 = 2;
51+
break;
52+
case 2:
53+
d7 = +__ZN6Random3getEf(8, +1);
54+
d6 = +__ZN6Random3getEf(i5, +1);
55+
_printf(24, (tempInt = STACKTOP, STACKTOP = STACKTOP + 16 | 0, HEAPF64[CHECK_ALIGN_8(tempInt | 0) >> 3] = d7, HEAPF64[CHECK_ALIGN_8(tempInt + 8 | 0) >> 3] = d6, tempInt) | 0);
56+
i2 = (d7 != d6 & 1) + i2 | 0;
57+
i1 = i1 + 1 | 0;
58+
if ((i1 | 0) < 100) {
59+
i3 = 2;
60+
break;
61+
} else {
62+
i3 = 3;
63+
break;
64+
}
65+
case 3:
66+
_printf(16, (tempInt = STACKTOP, STACKTOP = STACKTOP + 8 | 0, HEAP32[CHECK_ALIGN_4(tempInt | 0) >> 2] = i2, tempInt) | 0);
67+
STACKTOP = i4;
68+
return 0;
69+
}
70+
return 0;
71+
}
72+
function iffey() {
73+
var i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0, d6 = +0, d7 = +0;
74+
i4 = STACKTOP;
75+
STACKTOP = STACKTOP + 8 | 0;
76+
i3 = 1;
77+
while (1) {
78+
if (i3 | 0) {
79+
i5 = i4 | 0;
80+
__ZN6RandomC1Ev(i5);
81+
i1 = 0;
82+
i2 = 0;
83+
i3 = 2;
84+
} else {
85+
d7 = +__ZN6Random3getEf(8, +1);
86+
d6 = +__ZN6Random3getEf(i5, +1);
87+
_printf(24, (tempInt = STACKTOP, STACKTOP = STACKTOP + 16 | 0, HEAPF64[CHECK_ALIGN_8(tempInt | 0) >> 3] = d7, HEAPF64[CHECK_ALIGN_8(tempInt + 8 | 0) >> 3] = d6, tempInt) | 0);
88+
i2 = (d7 != d6 & 1) + i2 | 0;
89+
i1 = i1 + 1 | 0;
90+
if ((i1 | 0) < 100) {
91+
i3 = 2;
92+
} else {
93+
return 10;
94+
}
95+
}
96+
}
97+
return 0;
98+
}
99+
function labelledJump(i2) {
100+
i2 = i2 | 0;
101+
var i1 = 0, i3 = 0;
102+
i1 = 2;
103+
if (i2) {
104+
i1 = 17;
105+
i3 = 1;
106+
}
107+
if (i3 == 1) {
108+
i2 = i1 + 1;
109+
} else {
110+
i2 = i1 + 1;
111+
}
112+
return i2;
113+
}
114+
function linkedVars() {
115+
var i1 = 0, i2 = 0;
116+
while (1) {
117+
i1 = 9;
118+
i2 = 5;
119+
while (i1 > 0 | i2 > 0) {
120+
if (i1 < i2) {
121+
i1 = i1 - 1;
122+
} else {
123+
i2 = i2 - 1;
124+
}
125+
}
126+
if (i1 < i2) {
127+
break;
128+
}
129+
}
130+
return i1 + i2;
131+
}
132+
function deadCondExpr(i2) {
133+
i2 = i2 | 0;
134+
var i1 = 0;
135+
return i1 | 0;
136+
}
137+

0 commit comments

Comments
 (0)