Skip to content

Commit 351c262

Browse files
committed
only tick the garbage collector when storing values
1 parent 05dec9c commit 351c262

File tree

3 files changed

+71
-28
lines changed

3 files changed

+71
-28
lines changed

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

+34-9
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ type blank = Satisfies<ProgramState, {
2323
}>
2424

2525
type s<
26-
Update extends Partial<ProgramState>
26+
Source extends ProgramState,
27+
Update extends Partial<ProgramState>,
2728
> = Satisfies<ProgramState,
2829
evaluate<
2930
& Omit<
30-
blank,
31+
Source,
3132
keyof Update
3233
>
3334
& Required<Update>
@@ -36,10 +37,12 @@ type s<
3637

3738
// end-to-end test for garbage collection
3839

39-
type actual1023 = Satisfies<ProgramState, s<{
40-
count: 1023;
40+
type start1 = Satisfies<ProgramState, s<blank, {
41+
count: 1;
42+
stack: ["10000000000000000000000000000000", "00000000000000000000000000001111"]
4143
instructions: [
4244
{ kind: 'Nop', ziltoid: 'theOmniscient' },
45+
{ kind: 'Store', subkind: 'I32Store8' },
4346
{ kind: 'Nop', ziltoid: 'theOmniscient' },
4447
];
4548
L1Cache: {
@@ -59,20 +62,42 @@ type actual1023 = Satisfies<ProgramState, s<{
5962
garbageCollection: 1023;
6063
}>>
6164

62-
type actual1025 = executeInstruction<actual1023, true, 1024>
63-
type expected1025 = Satisfies<ProgramState, s<{
64-
count: 1024;
65+
// no change to the garbage collection counter for regular instructions
66+
type actual2 = executeInstruction<start1, true, 2>
67+
type expected2 = Satisfies<ProgramState, s<start1 /* note: using `start1` here, not `blank` like the rest */, {
68+
count: 2;
6569
instructions: [
70+
{ kind: 'Store', subkind: 'I32Store8' },
6671
{ kind: 'Nop', ziltoid: 'theOmniscient' },
6772
];
68-
L1Cache: {};
73+
}>>
74+
type test2 = Expect<Equal<actual2, expected2>>
75+
76+
type actual3 = executeInstruction<actual2, true, 3>
77+
type expected3 = Satisfies<ProgramState, s<blank, {
78+
count: 3;
79+
instructions: [
80+
{ kind: 'Nop', ziltoid: 'theOmniscient' },
81+
];
82+
L1Cache: evaluate<start1['L1Cache'] & {
83+
"10000000000000000000000000000000": "00001111"; // from the I32Store8
84+
}>;
85+
memory: start1['memory'];
86+
garbageCollection: 1024;
87+
}>>
88+
type test3 = Expect<Equal<actual3, expected3>>
89+
90+
type actual4 = executeInstruction<actual3, true, 4>
91+
type expected4 = Satisfies<ProgramState, s<blank, {
92+
count: 4;
6993
memory: {
7094
"00000000000000000000000000000000": "00101110"; // source and update match
7195
//"00000000000000000000000000000001": "11111111"; // the update cleared this value
7296
"00000000000000000000000000000100": "01010101"; // newly added by the update
7397
"00000000000000000000000000000111": "00000010"; // modified by the update
7498
"11111111111111111111111100000011": "00000001"; // the random other data only present in the source
99+
"10000000000000000000000000000000": "00001111"; // from the I32Store8
75100
}
76101
garbageCollection: 0;
77102
}>>
78-
type test1025 = Expect<Equal<actual1025, expected1025>>
103+
type test4 = Expect<Equal<actual4, expected4>>

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

+35-14
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,32 @@ export type IMemoryGrow = {
1414
export type ILoad = {
1515
kind: "Load"
1616
offset?: WasmValue
17-
subkind: string
17+
subkind:
18+
| 'I32Load'
19+
| 'I32Load8u'
20+
| 'I32Load8s'
21+
| 'I32Load16u'
22+
| 'I32Load16s'
23+
| 'I64Load'
24+
| 'I64Load8u'
25+
| 'I64Load8s'
26+
| 'I64Load16u'
27+
| 'I64Load16s'
28+
| 'I64Load32u'
29+
| 'I64Load32s'
1830
}
1931

2032
export type IStore = {
2133
kind: "Store"
2234
offset?: WasmValue
23-
subkind: string
35+
subkind:
36+
| "I32Store"
37+
| "I32Store8"
38+
| "I32Store16"
39+
| "I64Store"
40+
| "I64Store8"
41+
| "I64Store16"
42+
| "I64Store32"
2443
}
2544

2645
export type MemoryInstruction =
@@ -301,18 +320,20 @@ export type Store<
301320
infer address extends MemoryAddress,
302321
infer value extends WasmValue,
303322
]
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
323+
? State.GarbageCollection.increment<
324+
State.Stack.set<
325+
remaining,
326+
327+
instruction['subkind'] extends 'I32Store' ? StoreAll<address, value, instruction, state> :
328+
instruction['subkind'] extends 'I32Store8' ? Store8<address, value, instruction, state> :
329+
instruction['subkind'] extends 'I32Store16' ? Store16<address, value, instruction, state> :
330+
331+
instruction['subkind'] extends 'I64Store' ? StoreAll<address, value, instruction, state> :
332+
instruction['subkind'] extends 'I64Store8' ? Store8<address, value, instruction, state> :
333+
instruction['subkind'] extends 'I64Store16' ? Store16<address, value, instruction, state> :
334+
instruction['subkind'] extends 'I64Store32' ? Store32<address, value, instruction, state> :
335+
never
336+
>
316337
>
317338
: State.error<"stack exhausted", instruction, state>
318339
>

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -475,13 +475,10 @@ export namespace State {
475475
state extends ProgramState,
476476
force extends 'force' | 'schedule' = 'schedule',
477477

478-
// increment first, then check for collection
479-
_next extends ProgramState = increment<state>,
480-
481478
_shoudlCollect extends boolean =
482479
force extends 'force'
483480
? true
484-
: _next['garbageCollection'] extends SweepL1Every
481+
: state['garbageCollection'] extends SweepL1Every
485482
? true
486483
: false,
487484
> = Satisfies<ProgramState,
@@ -509,7 +506,7 @@ export namespace State {
509506
indirect: state['indirect'];
510507
results: state['results'];
511508
}
512-
: _next
509+
: state
513510
>
514511
}
515512

0 commit comments

Comments
 (0)