Skip to content

Commit 1225b0e

Browse files
committed
Revert "optimize the garbage collector (only inc when storing, and skip earlier)"
This reverts commit 7cb0761.
1 parent 7cb0761 commit 1225b0e

File tree

6 files changed

+46
-28
lines changed

6 files changed

+46
-28
lines changed

package.json

Lines changed: 1 addition & 1 deletion
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=16384 --max-old-space-size=8192 --max-semi-space-size=8192 ./node_modules/typescript/lib/tsc",
20+
"build": "node --stack-size=8100 --max-old-space-size=4096 --max-semi-space-size=4096 ./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

Lines changed: 1 addition & 1 deletion
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=16384 --max-old-space-size=8192 --max-semi-space-size=8192 --import tsx/esm ./evaluate/run.ts"
12+
"eval": "node --stack-size=8100 --max-old-space-size=4096 --max-semi-space-size=4096 --import tsx/esm ./evaluate/run.ts"
1313
},
1414
"devDependencies": {
1515
"http-server": "^14.1.1"

packages/ts-type-math/store.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ 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+
93103
export namespace Store {
94104
export type I32<
95105
a extends WasmValue

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -301,20 +301,18 @@ export type Store<
301301
infer address extends MemoryAddress,
302302
infer value extends WasmValue,
303303
]
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-
>
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
318316
>
319317
: State.error<"stack exhausted", instruction, state>
320318
>

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import { Equal, Expect } from "type-testing"
22
import { ProgramState } from "./types"
3+
import { State } from './state'
34
import type { Satisfies } from 'ts-type-math'
45
import { executeInstruction } from "./program"
56

7+
68
// end-to-end test for garbage collection
79

810
type actual1022 = Satisfies<ProgramState, {
911
count: 1022;
1012
results: [];
1113
stack: ["00000000000000000000010000000001", "00000000000000000000000000000000"];
1214
instructions: [
13-
{ kind: "Store"; subkind: "I32Store8" },
15+
{
16+
kind: "Store";
17+
subkind: "I32Store8";
18+
offset: "00000000000000000000000000000000";
19+
},
1420
{ kind: 'Const'; value: '10000000000000000000000000000000' },
1521
{ kind: 'Nop', ziltoid: 'theOmniscient' },
1622
];
@@ -23,7 +29,7 @@ type actual1022 = Satisfies<ProgramState, {
2329
"00000000000000000000010000000000": "01000001";
2430
};
2531
memory: {};
26-
garbageCollection: 1023;
32+
garbageCollection: 1022;
2733
indirect: {};
2834
memorySize: "";
2935
executionContexts: [];
@@ -46,9 +52,10 @@ type expected1023 = Satisfies<ProgramState, {
4652
globals: {};
4753
L1Cache: {
4854
"00000000000000000000010000000000": "01000001";
55+
"00000000000000000000010000000001": "00000000";
4956
};
5057
memory: {};
51-
garbageCollection: 1024;
58+
garbageCollection: 1023;
5259
indirect: {};
5360
memorySize: "";
5461
executionContexts: [];

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,10 @@ 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
410411
_Acc & { [k in address]: head } // note: this doesn't need Patch because we are building it up from scratch
411412
>
412-
: { [
413-
k in keyof _Acc as _Acc[k] extends Wasm.I8False
414-
? never
415-
: k
416-
]: _Acc[k] }
413+
: _Acc
417414
>
418415

419416
export type insert<
@@ -427,7 +424,7 @@ export namespace State {
427424
bytes
428425
>
429426
> = Satisfies<ProgramState,
430-
// State.debug<[_update],
427+
// State.debug<[_update], {
431428
{
432429
count: state['count'];
433430
stack: state['stack'];
@@ -478,10 +475,13 @@ export namespace State {
478475
state extends ProgramState,
479476
force extends 'force' | 'schedule' = 'schedule',
480477

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-
: state['garbageCollection'] extends CollectAt
484+
: _next['garbageCollection'] extends CollectAt
485485
? true
486486
: false,
487487
> = Satisfies<ProgramState,
@@ -495,7 +495,10 @@ export namespace State {
495495
instructions: state['instructions'];
496496
activeBranches: state['activeBranches'];
497497
L1Cache: {}; // clear the L1Cache
498-
memory: Patch<state['memory'], state['L1Cache']>; // update the memory with the L1Cache
498+
memory: Patch<
499+
state['memory'],
500+
TypeMath.GarbageCollect<state['L1Cache']>
501+
>; // update the memory with the L1Cache
499502
executionContexts: state['executionContexts'];
500503
funcs: state['funcs'];
501504
garbageCollection: 0; // reset
@@ -504,7 +507,7 @@ export namespace State {
504507
indirect: state['indirect'];
505508
results: state['results'];
506509
}
507-
: state
510+
: _next
508511
>
509512
}
510513

0 commit comments

Comments
 (0)