Skip to content

Commit bf99a25

Browse files
Optimize initial target builder inference
1 parent ead8f37 commit bf99a25

2 files changed

Lines changed: 74 additions & 52 deletions

File tree

scripts/type-performance.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ const scenarios = [
141141
label: "machine.handle (4 successive calls)",
142142
file: "successive-handle.ts",
143143
control: "successive-handle-control",
144-
maxInstantiations: 168_000,
144+
maxInstantiations: 166_000,
145145
maxMarginalInstantiations: 147_000
146146
},
147147
{
@@ -155,8 +155,8 @@ const scenarios = [
155155
label: "machine exact input/output/error/services",
156156
file: "exact-channels.ts",
157157
control: "exact-channels-control",
158-
maxInstantiations: 143_000,
159-
maxMarginalInstantiations: 127_000
158+
maxInstantiations: 138_000,
159+
maxMarginalInstantiations: 123_000
160160
},
161161
{
162162
id: "adapter-readiness-control",

src/Machine.ts

Lines changed: 71 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,23 @@ type NodeMethod<
841841
> = Machine.NodeSchema<Node> extends never ? FromMethod<FromArguments, Result>
842842
: ((...args: Arguments) => Result) & FromMethod<FromArguments, Result>
843843

844+
type NodeMethodWithInitial<
845+
Node,
846+
Arguments extends ReadonlyArray<unknown>,
847+
Result,
848+
FromArguments extends ReadonlyArray<unknown>,
849+
Path extends string
850+
> = Machine.NodeSchema<Node> extends never ? {
851+
readonly from: FromCallable<FromArguments, Machine.StateConstruction<Result>>
852+
readonly initial: InitialTargetFactory<Node, Path>
853+
}
854+
:
855+
& ((...args: Arguments) => Result)
856+
& {
857+
readonly from: FromCallable<FromArguments, Machine.StateConstruction<Result>>
858+
readonly initial: InitialTargetFactory<Node, Path>
859+
}
860+
844861
interface InitialTargetMethod<
845862
Node,
846863
Path extends string
@@ -867,15 +884,19 @@ type NodeConstructionSelectorFromCallable<Node, Builder, Result> = Machine.NodeS
867884
}
868885
: ConstructionSelectorFromCallable<NodeMakeInput<Node>, Builder, Result>
869886

870-
type NestedTargetMethod<Node, Builder, Result> = Machine.NodeSchema<Node> extends never ? {
887+
type NestedTargetMethod<Node, Builder, Result, Path extends string> = Machine.NodeSchema<Node> extends never ? {
871888
readonly from: NodeConstructionSelectorFromCallable<Node, Builder, Result>
889+
readonly initial: InitialTargetFactory<Node, Path>
872890
}
873891
:
874892
& (<Selected extends ConstructionResult<Result>>(
875893
value: NodeValue<Node>,
876894
state: (builder: Builder) => Selected
877895
) => Selected)
878-
& { readonly from: NodeConstructionSelectorFromCallable<Node, Builder, Result> }
896+
& {
897+
readonly from: NodeConstructionSelectorFromCallable<Node, Builder, Result>
898+
readonly initial: InitialTargetFactory<Node, Path>
899+
}
879900

880901
type ConstructionSelectorFromCallable<Input, Builder, Result> = {} extends Input ? {
881902
<Selected extends ConstructionResult<Result>>(
@@ -1408,33 +1429,33 @@ type LocalTargetMethod<
14081429
Path extends string = Machine.JoinPath<Prefix, StateId>
14091430
> = States[StateId] extends infer Node ?
14101431
Node extends { readonly type: "parallel"; readonly states: infer Children extends Machine.StateSchemas } ?
1411-
& InitialTargetMethod<Node, Path>
1412-
& (Source extends Path | `${Path}.${string}` ? NestedTargetMethod<
1413-
Node,
1414-
LocalTargetBuilderWithPrefix<AllStates, Children, Path, Source>,
1415-
LocalTargetResultWithPrefix<AllStates, Children, Path>
1416-
>
1417-
: NodeMethod<
1418-
Node,
1419-
WithNodeValue<Node, [
1420-
states: (
1421-
builder: FullParallelBuilder<Children, Path>
1422-
) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>>
1423-
]>,
1424-
Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>,
1425-
WithNodeInput<Node, [
1426-
states: (
1427-
builder: FullParallelBuilder<Children, Path>
1428-
) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>, boolean>
1429-
]>
1430-
>)
1431-
: Node extends { readonly states: infer Children extends Machine.StateSchemas } ?
1432-
& InitialTargetMethod<Node, Path>
1433-
& NestedTargetMethod<
1432+
Source extends Path | `${Path}.${string}` ? NestedTargetMethod<
14341433
Node,
14351434
LocalTargetBuilderWithPrefix<AllStates, Children, Path, Source>,
1436-
LocalTargetResultWithPrefix<AllStates, Children, Path>
1435+
LocalTargetResultWithPrefix<AllStates, Children, Path>,
1436+
Path
14371437
>
1438+
: NodeMethodWithInitial<
1439+
Node,
1440+
WithNodeValue<Node, [
1441+
states: (
1442+
builder: FullParallelBuilder<Children, Path>
1443+
) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>>
1444+
]>,
1445+
Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>,
1446+
WithNodeInput<Node, [
1447+
states: (
1448+
builder: FullParallelBuilder<Children, Path>
1449+
) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>, boolean>
1450+
]>,
1451+
Path
1452+
>
1453+
: Node extends { readonly states: infer Children extends Machine.StateSchemas } ? NestedTargetMethod<
1454+
Node,
1455+
LocalTargetBuilderWithPrefix<AllStates, Children, Path, Source>,
1456+
LocalTargetResultWithPrefix<AllStates, Children, Path>,
1457+
Path
1458+
>
14381459
: NodeMethod<
14391460
Node,
14401461
WithNodeValue<Node, []>,
@@ -1524,34 +1545,35 @@ type BranchTargetMethod<
15241545
Path extends string = Machine.JoinPath<Prefix, StateId>
15251546
> = States[StateId] extends infer Node ?
15261547
Node extends { readonly type: "parallel"; readonly states: infer Children extends Machine.StateSchemas } ?
1527-
& InitialTargetMethod<Node, Path>
1528-
& (Source extends Path | `${Path}.${string}` ?
1529-
& NestedTargetMethod<
1530-
Node,
1531-
BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>,
1532-
BranchTargetResultWithPrefix<AllStates, Children, Path>
1533-
>
1534-
& BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>
1535-
: NodeMethod<
1548+
Source extends Path | `${Path}.${string}` ?
1549+
& NestedTargetMethod<
15361550
Node,
1537-
WithNodeValue<Node, [
1538-
states: (
1539-
builder: FullParallelBuilder<Children, Path>
1540-
) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>>
1541-
]>,
1542-
Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>,
1543-
WithNodeInput<Node, [
1544-
states: (
1545-
builder: FullParallelBuilder<Children, Path>
1546-
) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>, boolean>
1547-
]>
1548-
>)
1551+
BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>,
1552+
BranchTargetResultWithPrefix<AllStates, Children, Path>,
1553+
Path
1554+
>
1555+
& BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>
1556+
: NodeMethodWithInitial<
1557+
Node,
1558+
WithNodeValue<Node, [
1559+
states: (
1560+
builder: FullParallelBuilder<Children, Path>
1561+
) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>>
1562+
]>,
1563+
Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>,
1564+
WithNodeInput<Node, [
1565+
states: (
1566+
builder: FullParallelBuilder<Children, Path>
1567+
) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>, boolean>
1568+
]>,
1569+
Path
1570+
>
15491571
: Node extends { readonly states: infer Children extends Machine.StateSchemas } ?
1550-
& InitialTargetMethod<Node, Path>
15511572
& NestedTargetMethod<
15521573
Node,
15531574
BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>,
1554-
BranchTargetResultWithPrefix<AllStates, Children, Path>
1575+
BranchTargetResultWithPrefix<AllStates, Children, Path>,
1576+
Path
15551577
>
15561578
& BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>
15571579
: NodeMethod<

0 commit comments

Comments
 (0)