Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-machines-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@typeonce/effect-machine": patch
---

Reduce retained machine memory by sharing immutable zero-input execution descriptors across process instances while preserving per-instance invoke state.
23 changes: 22 additions & 1 deletion src/internal/machineProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,11 @@ const makeProcessLogic: <
>
}

const initialProcessLogicCache = new WeakMap<
Machine.Any,
internalRuntime.ProcessLogic<any, any, any, any, any, any>
>()

export const toProcessLogic: <
const States extends Machine.StateSchemas,
const Events extends ReadonlyArray<Machine.TaggedSchema>,
Expand Down Expand Up @@ -814,7 +819,23 @@ export const toProcessLogic: <
| MachineSchemaDecodeError
| StartupError
| StoppedError
> = (machine, ...args) => makeProcessLogic(machine, { _tag: "Initial", args })
> = (machine, ...args) => {
if (args.length > 0) {
return makeProcessLogic(machine, { _tag: "Initial", args })
}
// The execution descriptor stores process-local invoke sessions by each
// runtime address and evaluates initialization/services on every start. A
// zero-argument descriptor is therefore safe to share for the lifetime of
// its immutable machine definition. Input-bearing and resumed starts retain
// their instance-specific entry values below.
const cached = initialProcessLogicCache.get(machine)
if (cached !== undefined) {
return cached as any
}
const logic = makeProcessLogic(machine, { _tag: "Initial", args })
initialProcessLogicCache.set(machine, logic as any)
return logic
}

const toResumedProcessLogic = (
machine: Machine.Any,
Expand Down
81 changes: 81 additions & 0 deletions test/Machine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3953,6 +3953,87 @@ describe("Machine", () => {
})
}))

it.effect("isolates invoked children across concurrent zero-input starts", () =>
Effect.gen(function*() {
const childStates = Machine.defineStates({ Idle })
const childMachine = Machine.make({
states: childStates.states,
events: [],
initial: () => childStates.initial.Idle(new Idle({ userId: "child" }))
})
const Child = Machine.child("shared-child", childMachine)
const parentStates = Machine.defineStates({ Loading })
const parentMachine = Machine.make({
states: parentStates.states,
events: [],
initial: () => parentStates.initial.Loading(new Loading({ requestId: "parent" }))
}).handle({
Loading: {
invoke: Machine.invokeMachine({ child: Child })
}
})

const [first, second] = yield* Effect.all(
[Machine.start(parentMachine), Machine.start(parentMachine)],
{ concurrency: "unbounded" }
)
const firstChild = yield* first.childChanges(Child).pipe(
Stream.filter(Option.isSome),
Stream.runHead,
Effect.map(Option.flatten)
)
const secondChild = yield* second.childChanges(Child).pipe(
Stream.filter(Option.isSome),
Stream.runHead,
Effect.map(Option.flatten)
)

assert(Option.isSome(firstChild))
assert(Option.isSome(secondChild))
assert.notStrictEqual(firstChild.value, secondChild.value)

yield* first.stop
assert.deepStrictEqual(yield* second.snapshot, {
status: "active",
state: { path: "Loading", value: new Loading({ requestId: "parent" }) }
})
assert.deepStrictEqual(yield* secondChild.value.snapshot, {
status: "active",
state: { path: "Idle", value: new Idle({ userId: "child" }) }
})
yield* second.stop
}))

it.effect("keeps input-bearing process descriptors instance-specific", () =>
Effect.gen(function*() {
const states = Machine.defineStates({ Idle })
const machine = Machine.make({
states: states.states,
events: [],
input: Input,
initial: (input) => states.initial.Idle(new Idle({ userId: input.userId }))
}).handle({
Idle: {}
})
const [first, second] = yield* Effect.all(
[
Machine.start(machine, { userId: "first" }),
Machine.start(machine, { userId: "second" })
],
{ concurrency: "unbounded" }
)

assert.deepStrictEqual(yield* first.state, {
path: "Idle",
value: new Idle({ userId: "first" })
})
assert.deepStrictEqual(yield* second.state, {
path: "Idle",
value: new Idle({ userId: "second" })
})
yield* Effect.all([first.stop, second.stop], { concurrency: "unbounded" })
}))

it.effect("invokeMachine rejects duplicate active child addresses", () =>
Effect.gen(function*() {
const childStates = Machine.defineStates({ Idle })
Expand Down