-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.bench.js
More file actions
210 lines (185 loc) · 3.79 KB
/
pool.bench.js
File metadata and controls
210 lines (185 loc) · 3.79 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import '../src/object.js';
import { Pool } from '../src/pool.js';
import '../src/primitives.js';
import buffer from './pool.test.wat?multi_memory'
const N = 100000;
const blueprint = {
type: 'object',
properties: {
x: {
type: 'object',
properties: {
y: {
type: 'object',
properties: {
z: {
defaultValue: 123,
type: 'float32',
},
},
},
},
},
},
};
const pool = new Pool(blueprint);
const exports = await WebAssembly.instantiate(buffer, {pool: pool.imports()})
.then(({instance: {exports}}) => exports);
function create() {
for (let i = 0; i < N; ++i) {
new Struct(i);
}
}
class Y {
z = 123;
}
class X {
constructor() {
this.y = new Y();
}
}
class P {
constructor() {
this.x = new X();
}
}
function createNative() {
for (let i = 0; i < N; ++i) {
new P();
}
}
const Concrete = pool.property.concrete();
function createConcrete() {
for (let i = 0; i < N; ++i) {
new Concrete(i);
}
}
function allocate() {
for (let i = 0; i < N; ++i) {
pool.allocate();
}
}
function allocateNative() {
const proxies = [];
for (let i = 0; i < N; ++i) {
proxies.push(new P());
}
}
function setNative(setting) {
for (let i = 0; i < N; ++i) {
setting[i].x.y.z += i + 1;
}
}
function setAllocated(setting) {
for (let i = 0; i < N; ++i) {
setting[i].x.y.z += i + 1;
}
}
let start;
function measure(label) {
const ms = performance.now() - start;
console.log(
`\x1b[33m${ms.toFixed(2).padStart(7, ' ')}\x1b[0mms`,
`(\x1b[33m${(ms / N * 1000).toFixed(4)}\x1b[0mμs/op)`,
`(\x1b[33m${Math.floor(N * (16.6 / ms)).toLocaleString().padStart(10, ' ')}\x1b[0m/tick)`,
'to',
label,
);
}
function clear(pool) {
for (const proxy of pool.proxies) {
if (proxy) {
pool.free(proxy);
}
}
pool.views.dirty?.fill(0);
pool.length.value = 0;
pool.freeList = [];
pool.proxies = [];
}
function warm(f, ...args) {
for (let i = 0; i < 1000000 / N; ++i) {
f(...args);
}
global.gc();
}
// const localeN = N.toLocaleString();
const label = `pool (N=${N.toLocaleString()})`;
console.log(label);
console.log('='.repeat(label.length + 1));
const Struct = pool.property.map({data: new DataView(new ArrayBuffer(N * pool.property.dataWidth))});
warm(create);
start = performance.now();
create();
measure('create');
warm(createNative);
start = performance.now();
createNative();
measure('create native');
warm(createConcrete);
start = performance.now();
createConcrete();
measure('create concrete');
warm(() => {
allocate();
clear(pool);
});
start = performance.now();
allocate();
measure('allocate');
clear(pool);
warm(() => {
allocate();
for (const proxy of pool.proxies) {
if (proxy) {
pool.free(proxy);
}
}
});
start = performance.now();
allocate();
measure('\x1b[93mallocate (cached)\x1b[0m');
clear(pool);
warm(allocateNative);
start = performance.now();
allocateNative();
measure('allocate native');
{
const setting = Array(N);
for (let i = 0; i < N; ++i) {
setting[i] = new P();
}
warm(setNative, setting);
start = performance.now();
setNative(setting);
measure('set native');
}
{
const setting = Array(N);
for (let i = 0; i < N; ++i) {
setting[i] = pool.allocate();
}
warm(() => {
setAllocated(setting);
clear(pool);
}, setting);
start = performance.now();
setAllocated(setting);
clear(pool);
measure('set allocated');
}
{
const setting = Array(N);
for (let i = 0; i < N; ++i) {
setting[i] = pool.allocate();
}
const r = Math.random();
warm(() => {
exports.thisIsAWasmTest(r);
clear(pool);
}, setting);
start = performance.now();
pool.views.dirty.fill(0)
exports.thisIsAWasmTest(r);
measure('\x1b[93mset allocated (buffer)\x1b[0m');
}