Skip to content

Commit f2e8dce

Browse files
committed
perf: speed up simulation
Caching the tick2() function speeds up the "stripes" by almost 40%. Other examples also show speed improvement in the range of 25%-40%.
1 parent bfe42a9 commit f2e8dce

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/sim/hdlwasm.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ export class HDLModuleWASM implements HDLModuleRunner {
230230
stopped: boolean;
231231
resetStartTimeMsec: number;
232232

233+
_tick2: (ofs: number, iters: number) => void;
234+
233235
constructor(moddef: HDLModuleDef, constpool: HDLModuleDef, maxMemoryMB?: number) {
234236
this.hdlmod = moddef;
235237
this.constpool = constpool;
@@ -243,12 +245,19 @@ export class HDLModuleWASM implements HDLModuleRunner {
243245
await this.genModule();
244246
this.genStateInterface();
245247
this.enableTracing();
248+
this.cacheFunctions();
246249
}
247250

248251
initSync() {
249252
this.genModuleSync();
250253
this.genStateInterface();
251254
this.enableTracing();
255+
this.cacheFunctions();
256+
}
257+
258+
private cacheFunctions() {
259+
// Cache the tick2 function for performance:
260+
this._tick2 = (this.instance.exports as any).tick2;
252261
}
253262

254263
powercycle() {
@@ -281,7 +290,7 @@ export class HDLModuleWASM implements HDLModuleRunner {
281290
}
282291

283292
tick2(iters: number) {
284-
(this.instance.exports as any).tick2(GLOBALOFS, iters);
293+
this._tick2(GLOBALOFS, iters);
285294
}
286295

287296
isFinished() {

0 commit comments

Comments
 (0)