Skip to content

Commit 7cb0761

Browse files
committed
optimize the garbage collector (only inc when storing, and skip earlier)
1 parent df33282 commit 7cb0761

File tree

6 files changed

+28
-46
lines changed

6 files changed

+28
-46
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"test:create-new": "pnpm --filter conformance-tests run test:create-new",
1818
"test:rust": "cargo test",
1919
"test": "pnpm run test:rust && CI=true pnpm run test:math && CI=true pnpm run test:conformance && pnpm run build",
20-
"build": "node --stack-size=8100 --max-old-space-size=4096 --max-semi-space-size=4096 ./node_modules/typescript/lib/tsc",
20+
"build": "node --stack-size=16384 --max-old-space-size=8192 --max-semi-space-size=8192 ./node_modules/typescript/lib/tsc",
2121
"build:trace": "pnpm run build --generateTrace trace",
2222
"build:force": "MICHIGAN_TYPESCRIPT=true time tsc --incremental false --tsBuildInfoFile null --generateCpuProfile tsc-output.cpuprofile"
2323
},

packages/playground/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"c-to-wat": "pnpm run compile:c-to-wasm && pnpm run compile:wasm-to-wat",
1010
"wat-to-wasm": "wat2wasm code.wat --output code.wasm",
1111
"test": "vitest",
12-
"eval": "node --stack-size=8100 --max-old-space-size=4096 --max-semi-space-size=4096 --import tsx/esm ./evaluate/run.ts"
12+
"eval": "node --stack-size=16384 --max-old-space-size=8192 --max-semi-space-size=8192 --import tsx/esm ./evaluate/run.ts"
1313
},
1414
"devDependencies": {
1515
"http-server": "^14.1.1"

packages/ts-type-math/store.ts

-10
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,6 @@ export type BytePatch<
9090
: never // unreachable
9191
}>, MemoryByAddress>>;
9292

93-
export type GarbageCollect<
94-
Memory extends MemoryByAddress
95-
> = Satisfies<MemoryByAddress, {
96-
[k in keyof Memory as
97-
Memory[k] extends Wasm.I8False
98-
? never
99-
: k
100-
]: Memory[k]
101-
}>
102-
10393
export namespace Store {
10494
export type I32<
10595
a extends WasmValue

packages/wasm-to-typescript-types/state.test.ts renamed to packages/wasm-to-typescript-types/garbageCollection.test.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
import { Equal, Expect } from "type-testing"
22
import { ProgramState } from "./types"
3-
import { State } from './state'
43
import type { Satisfies } from 'ts-type-math'
54
import { executeInstruction } from "./program"
65

7-
86
// end-to-end test for garbage collection
97

108
type actual1022 = Satisfies<ProgramState, {
119
count: 1022;
1210
results: [];
1311
stack: ["00000000000000000000010000000001", "00000000000000000000000000000000"];
1412
instructions: [
15-
{
16-
kind: "Store";
17-
subkind: "I32Store8";
18-
offset: "00000000000000000000000000000000";
19-
},
13+
{ kind: "Store"; subkind: "I32Store8" },
2014
{ kind: 'Const'; value: '10000000000000000000000000000000' },
2115
{ kind: 'Nop', ziltoid: 'theOmniscient' },
2216
];
@@ -29,7 +23,7 @@ type actual1022 = Satisfies<ProgramState, {
2923
"00000000000000000000010000000000": "01000001";
3024
};
3125
memory: {};
32-
garbageCollection: 1022;
26+
garbageCollection: 1023;
3327
indirect: {};
3428
memorySize: "";
3529
executionContexts: [];
@@ -52,10 +46,9 @@ type expected1023 = Satisfies<ProgramState, {
5246
globals: {};
5347
L1Cache: {
5448
"00000000000000000000010000000000": "01000001";
55-
"00000000000000000000010000000001": "00000000";
5649
};
5750
memory: {};
58-
garbageCollection: 1023;
51+
garbageCollection: 1024;
5952
indirect: {};
6053
memorySize: "";
6154
executionContexts: [];

packages/wasm-to-typescript-types/instructions/memory.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,20 @@ export type Store<
301301
infer address extends MemoryAddress,
302302
infer value extends WasmValue,
303303
]
304-
? State.Stack.set<
305-
remaining,
306-
307-
instruction['subkind'] extends 'I32Store' ? StoreAll<address, value, instruction, state> :
308-
instruction['subkind'] extends 'I32Store8' ? Store8<address, value, instruction, state> :
309-
instruction['subkind'] extends 'I32Store16' ? Store16<address, value, instruction, state> :
310-
311-
instruction['subkind'] extends 'I64Store' ? StoreAll<address, value, instruction, state> :
312-
instruction['subkind'] extends 'I64Store8' ? Store8<address, value, instruction, state> :
313-
instruction['subkind'] extends 'I64Store16' ? Store16<address, value, instruction, state> :
314-
instruction['subkind'] extends 'I64Store32' ? Store32<address, value, instruction, state> :
315-
never
304+
? State.GarbageCollection.increment<
305+
State.Stack.set<
306+
remaining,
307+
308+
instruction['subkind'] extends 'I32Store' ? StoreAll<address, value, instruction, state> :
309+
instruction['subkind'] extends 'I32Store8' ? Store8<address, value, instruction, state> :
310+
instruction['subkind'] extends 'I32Store16' ? Store16<address, value, instruction, state> :
311+
312+
instruction['subkind'] extends 'I64Store' ? StoreAll<address, value, instruction, state> :
313+
instruction['subkind'] extends 'I64Store8' ? Store8<address, value, instruction, state> :
314+
instruction['subkind'] extends 'I64Store16' ? Store16<address, value, instruction, state> :
315+
instruction['subkind'] extends 'I64Store32' ? Store32<address, value, instruction, state> :
316+
never
317+
>
316318
>
317319
: State.error<"stack exhausted", instruction, state>
318320
>

packages/wasm-to-typescript-types/state.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,13 @@ export namespace State {
407407
? CollectBytes<
408408
I32AddBinary<address, Wasm.I32True>,
409409
tail,
410-
// POTENTIAL OPTIMIZATION: if we are setting head for the first time we can just skip it entirely
411410
_Acc & { [k in address]: head } // note: this doesn't need Patch because we are building it up from scratch
412411
>
413-
: _Acc
412+
: { [
413+
k in keyof _Acc as _Acc[k] extends Wasm.I8False
414+
? never
415+
: k
416+
]: _Acc[k] }
414417
>
415418

416419
export type insert<
@@ -424,7 +427,7 @@ export namespace State {
424427
bytes
425428
>
426429
> = Satisfies<ProgramState,
427-
// State.debug<[_update], {
430+
// State.debug<[_update],
428431
{
429432
count: state['count'];
430433
stack: state['stack'];
@@ -475,13 +478,10 @@ export namespace State {
475478
state extends ProgramState,
476479
force extends 'force' | 'schedule' = 'schedule',
477480

478-
// increment first, then check for collection
479-
_next extends ProgramState = increment<state>,
480-
481481
_shoudlCollect extends boolean =
482482
force extends 'force'
483483
? true
484-
: _next['garbageCollection'] extends CollectAt
484+
: state['garbageCollection'] extends CollectAt
485485
? true
486486
: false,
487487
> = Satisfies<ProgramState,
@@ -495,10 +495,7 @@ export namespace State {
495495
instructions: state['instructions'];
496496
activeBranches: state['activeBranches'];
497497
L1Cache: {}; // clear the L1Cache
498-
memory: Patch<
499-
state['memory'],
500-
TypeMath.GarbageCollect<state['L1Cache']>
501-
>; // update the memory with the L1Cache
498+
memory: Patch<state['memory'], state['L1Cache']>; // update the memory with the L1Cache
502499
executionContexts: state['executionContexts'];
503500
funcs: state['funcs'];
504501
garbageCollection: 0; // reset
@@ -507,7 +504,7 @@ export namespace State {
507504
indirect: state['indirect'];
508505
results: state['results'];
509506
}
510-
: _next
507+
: state
511508
>
512509
}
513510

0 commit comments

Comments
 (0)