Skip to content

Commit

Permalink
improve performance of Effect.forkIn
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Jan 17, 2025
1 parent 8653072 commit 98f02c2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-keys-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

improve performance of Effect.forkIn
36 changes: 19 additions & 17 deletions packages/effect/src/internal/effect/circular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import type * as Types from "../../Types.js"
import * as internalCause from "../cause.js"
import * as effect from "../core-effect.js"
import * as core from "../core.js"
import * as executionStrategy from "../executionStrategy.js"
import * as internalFiber from "../fiber.js"
import * as fiberRuntime from "../fiberRuntime.js"
import { globalScope } from "../fiberScope.js"
Expand Down Expand Up @@ -362,23 +361,26 @@ export const forkIn = dual<
>(
2,
(self, scope) =>
core.uninterruptibleMask((restore) =>
core.flatMap(scope.fork(executionStrategy.sequential), (child) =>
pipe(
restore(self),
core.onExit((exit) => child.close(exit)),
fiberRuntime.forkDaemon,
core.tap((fiber) =>
child.addFinalizer(() =>
core.fiberIdWith((fiberId) =>
Equal.equals(fiberId, fiber.id()) ?
core.void :
core.asVoid(core.interruptFiber(fiber))
)
)
core.withFiberRuntime((parent, parentStatus) => {
const scopeImpl = scope as fiberRuntime.ScopeImpl
const fiber = fiberRuntime.unsafeFork(self, parent, parentStatus.runtimeFlags, globalScope)
if (scopeImpl.state._tag === "Open") {
const finalizer = () =>
core.fiberIdWith((fiberId) =>
Equal.equals(fiberId, fiber.id()) ?
core.void :
core.asVoid(core.interruptFiber(fiber))
)
))
)
scopeImpl.state.finalizers.add(finalizer)
fiber.addObserver(() => {
if (scopeImpl.state._tag === "Closed") return
scopeImpl.state.finalizers.delete(finalizer)
})
} else {
fiber.unsafeInterruptAsFork(parent.id())
}
return core.succeed(fiber)
})
)

/** @internal */
Expand Down
3 changes: 2 additions & 1 deletion packages/effect/src/internal/fiberRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3207,7 +3207,8 @@ export const scopeTag = Context.GenericTag<Scope.Scope>("effect/Scope")
/* @internal */
export const scope: Effect.Effect<Scope.Scope, never, Scope.Scope> = scopeTag

interface ScopeImpl extends Scope.CloseableScope {
/** @internal */
export interface ScopeImpl extends Scope.CloseableScope {
state: {
readonly _tag: "Open"
readonly finalizers: Set<Scope.Scope.Finalizer>
Expand Down

0 comments on commit 98f02c2

Please sign in to comment.