From c654e5400df6c9f6e8cbf2334a764b524b92881d Mon Sep 17 00:00:00 2001 From: Michael Arnaldi Date: Tue, 3 Sep 2024 11:34:49 +0200 Subject: [PATCH] FiberRefs perf work (#3518) --- .changeset/twelve-dingos-destroy.md | 6 + packages/effect/src/Fiber.ts | 35 ++++++ packages/effect/src/internal/core.ts | 9 +- .../effect/src/internal/defaultServices.ts | 19 ++- packages/effect/src/internal/fiberRefs.ts | 3 +- packages/effect/src/internal/fiberRuntime.ts | 116 +++++++++--------- packages/effect/src/internal/layer.ts | 6 +- .../effect/src/internal/managedRuntime.ts | 2 +- packages/effect/src/internal/runtime.ts | 2 +- packages/opentelemetry/src/internal/tracer.ts | 5 +- 10 files changed, 119 insertions(+), 84 deletions(-) create mode 100644 .changeset/twelve-dingos-destroy.md diff --git a/.changeset/twelve-dingos-destroy.md b/.changeset/twelve-dingos-destroy.md new file mode 100644 index 00000000000..e59d3d3a071 --- /dev/null +++ b/.changeset/twelve-dingos-destroy.md @@ -0,0 +1,6 @@ +--- +"@effect/opentelemetry": minor +"effect": minor +--- + +Cache some fiber references in the runtime to optimize reading in hot-paths diff --git a/packages/effect/src/Fiber.ts b/packages/effect/src/Fiber.ts index 15f8623a3f9..3f180463722 100644 --- a/packages/effect/src/Fiber.ts +++ b/packages/effect/src/Fiber.ts @@ -2,6 +2,8 @@ * @since 2.0.0 */ import type * as Cause from "./Cause.js" +import type { Context } from "./Context.js" +import type { DefaultServices } from "./DefaultServices.js" import type * as Effect from "./Effect.js" import type * as Either from "./Either.js" import type * as Exit from "./Exit.js" @@ -18,7 +20,10 @@ import type * as Option from "./Option.js" import type * as order from "./Order.js" import type { Pipeable } from "./Pipeable.js" import type * as RuntimeFlags from "./RuntimeFlags.js" +import type { Scheduler } from "./Scheduler.js" import type * as Scope from "./Scope.js" +import type { Supervisor } from "./Supervisor.js" +import type { AnySpan, Tracer } from "./Tracer.js" import type * as Types from "./Types.js" /** @@ -155,6 +160,36 @@ export interface RuntimeFiber extends Fiber, Fiber.R * resume immediately. Otherwise, the effect will resume when the fiber exits. */ unsafeInterruptAsFork(fiberId: FiberId.FiberId): void + + /** + * Gets the current context + */ + get currentContext(): Context + + /** + * Gets the current context + */ + get currentDefaultServices(): Context + + /** + * Gets the current scheduler + */ + get currentScheduler(): Scheduler + + /** + * Gets the current tracer + */ + get currentTracer(): Tracer + + /** + * Gets the current span + */ + get currentSpan(): AnySpan | undefined + + /** + * Gets the current supervisor + */ + get currentSupervisor(): Supervisor } /** diff --git a/packages/effect/src/internal/core.ts b/packages/effect/src/internal/core.ts index 5bc0e55a1df..fb4e9413182 100644 --- a/packages/effect/src/internal/core.ts +++ b/packages/effect/src/internal/core.ts @@ -51,7 +51,6 @@ import * as DeferredOpCodes from "./opCodes/deferred.js" import * as OpCodes from "./opCodes/effect.js" import * as _runtimeFlags from "./runtimeFlags.js" import { SingleShotGen } from "./singleShotGen.js" -import * as internalTracer from "./tracer.js" // ----------------------------------------------------------------------------- // Effect @@ -1684,7 +1683,7 @@ const fiberRefVariance = { /* @internal */ export const fiberRefGet = (self: FiberRef.FiberRef): Effect.Effect => - fiberRefModify(self, (a) => [a, a] as const) + withFiberRuntime((fiber) => exitSucceed(fiber.getFiberRef(self))) /* @internal */ export const fiberRefGetAndSet = dual< @@ -2966,7 +2965,7 @@ const deferredInterruptJoiner = ( // Context // ----------------------------------------------------------------------------- -const constContext = fiberRefGet(currentContext) +const constContext = withFiberRuntime((fiber) => exitSucceed(fiber.currentContext)) /* @internal */ export const context = (): Effect.Effect, never, R> => constContext as any @@ -3021,9 +3020,7 @@ export const mapInputContext = dual< /** @internal */ export const currentSpanFromFiber = (fiber: Fiber.RuntimeFiber): Option.Option => { - const span = fiber.getFiberRef(currentContext).unsafeMap.get(internalTracer.spanTag.key) as - | Tracer.AnySpan - | undefined + const span = fiber.currentSpan return span !== undefined && span._tag === "Span" ? Option.some(span) : Option.none() } diff --git a/packages/effect/src/internal/defaultServices.ts b/packages/effect/src/internal/defaultServices.ts index 18f96fc4b57..6d635766081 100644 --- a/packages/effect/src/internal/defaultServices.ts +++ b/packages/effect/src/internal/defaultServices.ts @@ -47,9 +47,14 @@ export const sleep = (duration: Duration.DurationInput): Effect.Effect => return clockWith((clock) => clock.sleep(decodedDuration)) } +/** @internal */ +export const defaultServicesWith = ( + f: (services: Context.Context) => Effect.Effect +) => core.withFiberRuntime((fiber) => f(fiber.currentDefaultServices)) + /** @internal */ export const clockWith = (f: (clock: Clock.Clock) => Effect.Effect): Effect.Effect => - core.fiberRefGetWith(currentServices, (services) => f(Context.get(services, clock.clockTag))) + defaultServicesWith((services) => f(services.unsafeMap.get(clock.clockTag.key))) /** @internal */ export const currentTimeMillis: Effect.Effect = clockWith((clock) => clock.currentTimeMillis) @@ -83,10 +88,7 @@ export const withConfigProvider = dual< export const configProviderWith = ( f: (configProvider: ConfigProvider.ConfigProvider) => Effect.Effect ): Effect.Effect => - core.fiberRefGetWith( - currentServices, - (services) => f(Context.get(services, configProvider.configProviderTag)) - ) + defaultServicesWith((services) => f(services.unsafeMap.get(configProvider.configProviderTag.key))) /** @internal */ export const config = (config: Config.Config) => configProviderWith((_) => _.load(config)) @@ -98,10 +100,7 @@ export const configOrDie = (config: Config.Config) => core.orDie(configPro /** @internal */ export const randomWith = (f: (random: Random.Random) => Effect.Effect): Effect.Effect => - core.fiberRefGetWith( - currentServices, - (services) => f(Context.get(services, random.randomTag)) - ) + defaultServicesWith((services) => f(services.unsafeMap.get(random.randomTag.key))) /** @internal */ export const withRandom = dual< @@ -151,7 +150,7 @@ export const choice = >( /** @internal */ export const tracerWith = (f: (tracer: Tracer.Tracer) => Effect.Effect): Effect.Effect => - core.fiberRefGetWith(currentServices, (services) => f(Context.get(services, tracer.tracerTag))) + defaultServicesWith((services) => f(services.unsafeMap.get(tracer.tracerTag.key))) /** @internal */ export const withTracer = dual< diff --git a/packages/effect/src/internal/fiberRefs.ts b/packages/effect/src/internal/fiberRefs.ts index 3b3e26b32c5..8d9ece313bb 100644 --- a/packages/effect/src/internal/fiberRefs.ts +++ b/packages/effect/src/internal/fiberRefs.ts @@ -33,8 +33,7 @@ export class FiberRefsImpl implements FiberRefs.FiberRefs { FiberRef.FiberRef, Arr.NonEmptyReadonlyArray > - ) { - } + ) {} pipe() { return pipeArguments(this, arguments) } diff --git a/packages/effect/src/internal/fiberRuntime.ts b/packages/effect/src/internal/fiberRuntime.ts index 7c985f31d1a..14eafde83fa 100644 --- a/packages/effect/src/internal/fiberRuntime.ts +++ b/packages/effect/src/internal/fiberRuntime.ts @@ -6,6 +6,7 @@ import * as Chunk from "../Chunk.js" import type * as Clock from "../Clock.js" import type { ConfigProvider } from "../ConfigProvider.js" import * as Context from "../Context.js" +import type { DefaultServices } from "../DefaultServices.js" import * as Deferred from "../Deferred.js" import type * as Duration from "../Duration.js" import type * as Effect from "../Effect.js" @@ -169,8 +170,8 @@ const contOpSuccess = { cont: core.RevertFlags, value: unknown ) => { - self.patchRuntimeFlags(self._runtimeFlags, cont.patch) - if (_runtimeFlags.interruptible(self._runtimeFlags) && self.isInterrupted()) { + self.patchRuntimeFlags(self.currentRuntimeFlags, cont.patch) + if (_runtimeFlags.interruptible(self.currentRuntimeFlags) && self.isInterrupted()) { return core.exitFailCause(self.getInterruptedCause()) } else { return core.exitSucceed(value) @@ -278,8 +279,6 @@ export class FiberRuntime implements Fiber.RuntimeFi private _fiberRefs: FiberRefs.FiberRefs private _fiberId: FiberId.Runtime - public _runtimeFlags: RuntimeFlags.RuntimeFlags - private _queue = new Array() private _children: Set> | null = null private _observers = new Array<(exit: Exit.Exit) => void>() @@ -289,28 +288,31 @@ export class FiberRuntime implements Fiber.RuntimeFi private _asyncBlockingOn: FiberId.FiberId | null = null private _exitValue: Exit.Exit | null = null private _steps: Array = [] - public _supervisor: Supervisor.Supervisor - public _scheduler: Scheduler - private _tracer: Tracer.Tracer + private _isYielding = false + + public currentRuntimeFlags: RuntimeFlags.RuntimeFlags public currentOpCount: number = 0 - private isYielding = false + public currentSupervisor!: Supervisor.Supervisor + public currentScheduler!: Scheduler + public currentTracer!: Tracer.Tracer + public currentSpan!: Tracer.AnySpan | undefined + public currentContext!: Context.Context + public currentDefaultServices!: Context.Context constructor( fiberId: FiberId.Runtime, fiberRefs0: FiberRefs.FiberRefs, runtimeFlags0: RuntimeFlags.RuntimeFlags ) { - this._runtimeFlags = runtimeFlags0 + this.currentRuntimeFlags = runtimeFlags0 this._fiberId = fiberId this._fiberRefs = fiberRefs0 - this._supervisor = this.getFiberRef(currentSupervisor) - this._scheduler = this.getFiberRef(currentScheduler) if (_runtimeFlags.runtimeMetrics(runtimeFlags0)) { const tags = this.getFiberRef(core.currentMetricLabels) fiberStarted.unsafeUpdate(1, tags) fiberActive.unsafeUpdate(1, tags) } - this._tracer = Context.get(this.getFiberRef(defaultServices.currentServices), tracer.tracerTag) + this.refreshRefCache() } /** @@ -342,7 +344,7 @@ export class FiberRuntime implements Fiber.RuntimeFi get runtimeFlags(): Effect.Effect { return this.ask((state, status) => { if (FiberStatus.isDone(status)) { - return state._runtimeFlags + return state.currentRuntimeFlags } return status.runtimeFlags }) @@ -528,7 +530,7 @@ export class FiberRuntime implements Fiber.RuntimeFi * log annotations and log level) may not be up-to-date. */ getFiberRefs(): FiberRefs.FiberRefs { - this.setFiberRef(currentRuntimeFlags, this._runtimeFlags) + this.setFiberRef(currentRuntimeFlags, this.currentRuntimeFlags) return this._fiberRefs } @@ -570,9 +572,12 @@ export class FiberRuntime implements Fiber.RuntimeFi } refreshRefCache() { - this._tracer = Context.get(this.getFiberRef(defaultServices.currentServices), tracer.tracerTag) - this._supervisor = this.getFiberRef(currentSupervisor) - this._scheduler = this.getFiberRef(currentScheduler) + this.currentDefaultServices = this.getFiberRef(defaultServices.currentServices) + this.currentTracer = this.currentDefaultServices.unsafeMap.get(tracer.tracerTag.key) + this.currentSupervisor = this.getFiberRef(currentSupervisor) + this.currentScheduler = this.getFiberRef(currentScheduler) + this.currentContext = this.getFiberRef(core.currentContext) + this.currentSpan = this.currentContext.unsafeMap.get(tracer.spanTag.key) } /** @@ -653,7 +658,7 @@ export class FiberRuntime implements Fiber.RuntimeFi * **NOTE**: This method must be invoked by the fiber itself. */ drainQueueLaterOnExecutor() { - this._scheduler.scheduleTask( + this.currentScheduler.scheduleTask( this.run, this.getFiberRef(core.currentSchedulingPriority) ) @@ -764,7 +769,7 @@ export class FiberRuntime implements Fiber.RuntimeFi } reportExitValue(exit: Exit.Exit) { - if (_runtimeFlags.runtimeMetrics(this._runtimeFlags)) { + if (_runtimeFlags.runtimeMetrics(this.currentRuntimeFlags)) { const tags = this.getFiberRef(core.currentMetricLabels) const startTimeMillis = this.id().startTimeMillis const endTimeMillis = Date.now() @@ -866,7 +871,7 @@ export class FiberRuntime implements Fiber.RuntimeFi this, this._exitValue !== null ? FiberStatus.done : - FiberStatus.suspended(this._runtimeFlags, this._asyncBlockingOn!) + FiberStatus.suspended(this.currentRuntimeFlags, this._asyncBlockingOn!) ) return EvaluationSignalContinue } @@ -882,10 +887,10 @@ export class FiberRuntime implements Fiber.RuntimeFi * **NOTE**: This method must be invoked by the fiber itself. */ evaluateEffect(effect0: Effect.Effect) { - this._supervisor.onResume(this) + this.currentSupervisor.onResume(this) try { let effect: Effect.Effect | null = - _runtimeFlags.interruptible(this._runtimeFlags) && this.isInterrupted() ? + _runtimeFlags.interruptible(this.currentRuntimeFlags) && this.isInterrupted() ? core.exitFailCause(this.getInterruptedCause()) : effect0 while (effect !== null) { @@ -895,7 +900,7 @@ export class FiberRuntime implements Fiber.RuntimeFi const op = yieldedOpChannel.currentOp! yieldedOpChannel.currentOp = null if (op._op === OpCodes.OP_YIELD) { - if (_runtimeFlags.cooperativeYielding(this._runtimeFlags)) { + if (_runtimeFlags.cooperativeYielding(this.currentRuntimeFlags)) { this.tell(FiberMessage.yieldNow()) this.tell(FiberMessage.resume(core.exitVoid)) effect = null @@ -907,7 +912,7 @@ export class FiberRuntime implements Fiber.RuntimeFi effect = null } } else { - this._runtimeFlags = pipe(this._runtimeFlags, _runtimeFlags.enable(_runtimeFlags.WindDown)) + this.currentRuntimeFlags = pipe(this.currentRuntimeFlags, _runtimeFlags.enable(_runtimeFlags.WindDown)) const interruption = this.interruptAllChildren() if (interruption !== null) { effect = core.flatMap(interruption, () => exit) @@ -926,7 +931,7 @@ export class FiberRuntime implements Fiber.RuntimeFi } } } finally { - this._supervisor.onSuspend(this) + this.currentSupervisor.onSuspend(this) } } @@ -981,7 +986,7 @@ export class FiberRuntime implements Fiber.RuntimeFi patchRuntimeFlags(oldRuntimeFlags: RuntimeFlags.RuntimeFlags, patch: RuntimeFlagsPatch.RuntimeFlagsPatch) { const newRuntimeFlags = _runtimeFlags.patch(oldRuntimeFlags, patch) ;(globalThis as any)[internalFiber.currentFiberURI] = this - this._runtimeFlags = newRuntimeFlags + this.currentRuntimeFlags = newRuntimeFlags return newRuntimeFlags } @@ -1016,7 +1021,7 @@ export class FiberRuntime implements Fiber.RuntimeFi pushStack(cont: core.Continuation) { this._stack.push(cont) if (cont._op === "OnStep") { - this._steps.push({ refs: this.getFiberRefs(), flags: this._runtimeFlags }) + this._steps.push({ refs: this.getFiberRefs(), flags: this.currentRuntimeFlags }) } } @@ -1052,10 +1057,7 @@ export class FiberRuntime implements Fiber.RuntimeFi } [OpCodes.OP_TAG](op: core.Primitive & { _op: OpCodes.OP_SYNC }) { - return core.map( - core.fiberRefGet(core.currentContext), - (context) => Context.unsafeGet(context, op as unknown as Context.Tag) - ) + return core.sync(() => Context.unsafeGet(this.currentContext, op as unknown as Context.Tag)) } ["Left"](op: core.Primitive & { _op: "Left" }) { @@ -1144,22 +1146,22 @@ export class FiberRuntime implements Fiber.RuntimeFi switch (cont._op) { case OpCodes.OP_ON_FAILURE: case OpCodes.OP_ON_SUCCESS_AND_FAILURE: { - if (!(_runtimeFlags.interruptible(this._runtimeFlags) && this.isInterrupted())) { + if (!(_runtimeFlags.interruptible(this.currentRuntimeFlags) && this.isInterrupted())) { return internalCall(() => cont.effect_instruction_i1(cause)) } else { return core.exitFailCause(internalCause.stripFailures(cause)) } } case "OnStep": { - if (!(_runtimeFlags.interruptible(this._runtimeFlags) && this.isInterrupted())) { + if (!(_runtimeFlags.interruptible(this.currentRuntimeFlags) && this.isInterrupted())) { return core.exitSucceed(core.exitFailCause(cause)) } else { return core.exitFailCause(internalCause.stripFailures(cause)) } } case OpCodes.OP_REVERT_FLAGS: { - this.patchRuntimeFlags(this._runtimeFlags, cont.patch) - if (_runtimeFlags.interruptible(this._runtimeFlags) && this.isInterrupted()) { + this.patchRuntimeFlags(this.currentRuntimeFlags, cont.patch) + if (_runtimeFlags.interruptible(this.currentRuntimeFlags) && this.isInterrupted()) { return core.exitFailCause(internalCause.sequential(cause, this.getInterruptedCause())) } else { return core.exitFailCause(cause) @@ -1179,14 +1181,14 @@ export class FiberRuntime implements Fiber.RuntimeFi return internalCall(() => op.effect_instruction_i0( this as FiberRuntime, - FiberStatus.running(this._runtimeFlags) as FiberStatus.Running + FiberStatus.running(this.currentRuntimeFlags) as FiberStatus.Running ) ) } ["Blocked"](op: core.Primitive & { _op: "Blocked" }) { const refs = this.getFiberRefs() - const flags = this._runtimeFlags + const flags = this.currentRuntimeFlags if (this._steps.length > 0) { const frames: Array = [] const snap = this._steps[this._steps.length - 1] @@ -1196,7 +1198,7 @@ export class FiberRuntime implements Fiber.RuntimeFi frame = this.popStack() } this.setFiberRefs(snap.refs) - this._runtimeFlags = snap.flags + this.currentRuntimeFlags = snap.flags const patchRefs = FiberRefsPatch.diff(snap.refs, refs) const patchFlags = _runtimeFlags.diff(snap.flags, flags) return core.exitSucceed(core.blocked( @@ -1208,7 +1210,7 @@ export class FiberRuntime implements Fiber.RuntimeFi newFiber.setFiberRefs( FiberRefsPatch.patch(newFiber.id(), newFiber.getFiberRefs())(patchRefs) ) - newFiber._runtimeFlags = _runtimeFlags.patch(patchFlags)(newFiber._runtimeFlags) + newFiber.currentRuntimeFlags = _runtimeFlags.patch(patchFlags)(newFiber.currentRuntimeFlags) return op.effect_instruction_i1 }) )) @@ -1227,7 +1229,7 @@ export class FiberRuntime implements Fiber.RuntimeFi [OpCodes.OP_UPDATE_RUNTIME_FLAGS](op: core.Primitive & { _op: OpCodes.OP_UPDATE_RUNTIME_FLAGS }) { const updateFlags = op.effect_instruction_i0 - const oldRuntimeFlags = this._runtimeFlags + const oldRuntimeFlags = this.currentRuntimeFlags const newRuntimeFlags = _runtimeFlags.patch(oldRuntimeFlags, updateFlags) // One more chance to short circuit: if we're immediately going // to interrupt. Interruption will cause immediate reversion of @@ -1237,7 +1239,7 @@ export class FiberRuntime implements Fiber.RuntimeFi return core.exitFailCause(this.getInterruptedCause()) } else { // Impossible to short circuit, so record the changes - this.patchRuntimeFlags(this._runtimeFlags, updateFlags) + this.patchRuntimeFlags(this.currentRuntimeFlags, updateFlags) if (op.effect_instruction_i1) { // Since we updated the flags, we need to revert them const revertFlags = _runtimeFlags.diff(newRuntimeFlags, oldRuntimeFlags) @@ -1271,13 +1273,13 @@ export class FiberRuntime implements Fiber.RuntimeFi [OpCodes.OP_ASYNC](op: core.Primitive & { _op: OpCodes.OP_ASYNC }) { this._asyncBlockingOn = op.effect_instruction_i1 - this.initiateAsync(this._runtimeFlags, op.effect_instruction_i0) + this.initiateAsync(this.currentRuntimeFlags, op.effect_instruction_i0) yieldedOpChannel.currentOp = op return YieldedOp } [OpCodes.OP_YIELD](op: core.Primitive & { op: OpCodes.OP_YIELD }) { - this.isYielding = false + this._isYielding = false yieldedOpChannel.currentOp = op return YieldedOp } @@ -1307,17 +1309,17 @@ export class FiberRuntime implements Fiber.RuntimeFi this.currentOpCount = 0 // eslint-disable-next-line no-constant-condition while (true) { - if ((this._runtimeFlags & OpSupervision) !== 0) { - this._supervisor.onEffect(this, cur) + if ((this.currentRuntimeFlags & OpSupervision) !== 0) { + this.currentSupervisor.onEffect(this, cur) } if (this._queue.length > 0) { - cur = this.drainQueueWhileRunning(this._runtimeFlags, cur) + cur = this.drainQueueWhileRunning(this.currentRuntimeFlags, cur) } - if (!this.isYielding) { + if (!this._isYielding) { this.currentOpCount += 1 - const shouldYield = this._scheduler.shouldYield(this) + const shouldYield = this.currentScheduler.shouldYield(this) if (shouldYield !== false) { - this.isYielding = true + this._isYielding = true this.currentOpCount = 0 const oldCur = cur cur = core.flatMap(core.yieldNow({ priority: shouldYield }), () => oldCur) @@ -1330,7 +1332,7 @@ export class FiberRuntime implements Fiber.RuntimeFi } // @ts-expect-error - cur = this._tracer.context( + cur = this.currentTracer.context( () => { if (version.getCurrentVersion() !== (cur as core.Primitive)[EffectTypeId]._V) { return core.dieMessage( @@ -1604,12 +1606,12 @@ export const addFinalizer = ( core.withFiberRuntime( (runtime) => { const acquireRefs = runtime.getFiberRefs() - const acquireFlags = runtime._runtimeFlags + const acquireFlags = runtime.currentRuntimeFlags return core.flatMap(scope, (scope) => core.scopeAddFinalizerExit(scope, (exit) => core.withFiberRuntime((runtimeFinalizer) => { const preRefs = runtimeFinalizer.getFiberRefs() - const preFlags = runtimeFinalizer._runtimeFlags + const preFlags = runtimeFinalizer.currentRuntimeFlags const patchRefs = FiberRefsPatch.diff(preRefs, acquireRefs) const patchFlags = _runtimeFlags.diff(preFlags, acquireFlags) const inverseRefs = FiberRefsPatch.diff(acquireRefs, preRefs) @@ -2100,7 +2102,7 @@ export const forEachConcurrentDiscard = ( const results = new Array() const interruptAll = () => fibers.forEach((fiber) => { - fiber._scheduler.scheduleTask(() => { + fiber.currentScheduler.scheduleTask(() => { fiber.unsafeInterruptAsFork(parent.id()) }, 0) }) @@ -2122,10 +2124,10 @@ export const forEachConcurrentDiscard = ( const fiber = unsafeForkUnstarted( runnable, parent, - parent._runtimeFlags, + parent.currentRuntimeFlags, fiberScope.globalScope ) - parent._scheduler.scheduleTask(() => { + parent.currentScheduler.scheduleTask(() => { if (interruptImmediately) { fiber.unsafeInterruptAsFork(parent.id()) } @@ -2186,7 +2188,7 @@ export const forEachConcurrentDiscard = ( startOrder.push(fiber) fibers.add(fiber) if (interrupted) { - fiber._scheduler.scheduleTask(() => { + fiber.currentScheduler.scheduleTask(() => { fiber.unsafeInterruptAsFork(parent.id()) }, 0) } @@ -2356,7 +2358,7 @@ export const unsafeMakeChildFiber = ( childFiberRefs, core.currentContext as unknown as FiberRef.FiberRef> ) - const supervisor = childFiber._supervisor + const supervisor = childFiber.currentSupervisor supervisor.onStart( childContext, diff --git a/packages/effect/src/internal/layer.ts b/packages/effect/src/internal/layer.ts index a1c61b6dba1..ca60bd5414f 100644 --- a/packages/effect/src/internal/layer.ts +++ b/packages/effect/src/internal/layer.ts @@ -1270,17 +1270,17 @@ const provideSomeRuntime = dual< const oldContext = fiber.getFiberRef(core.currentContext) const oldRefs = fiber.getFiberRefs() const newRefs = FiberRefsPatch.patch(fiber.id(), oldRefs)(patchRefs) - const oldFlags = fiber._runtimeFlags + const oldFlags = fiber.currentRuntimeFlags const newFlags = runtimeFlags.patch(patchFlags)(oldFlags) const rollbackRefs = FiberRefsPatch.diff(newRefs, oldRefs) const rollbackFlags = runtimeFlags.diff(newFlags, oldFlags) fiber.setFiberRefs(newRefs) - fiber._runtimeFlags = newFlags + fiber.currentRuntimeFlags = newFlags return fiberRuntime.ensuring( core.provideSomeContext(restore(self), Context.merge(oldContext, rt.context)), core.withFiberRuntime((fiber) => { fiber.setFiberRefs(FiberRefsPatch.patch(fiber.id(), fiber.getFiberRefs())(rollbackRefs)) - fiber._runtimeFlags = runtimeFlags.patch(rollbackFlags)(fiber._runtimeFlags) + fiber.currentRuntimeFlags = runtimeFlags.patch(rollbackFlags)(fiber.currentRuntimeFlags) return core.void }) ) diff --git a/packages/effect/src/internal/managedRuntime.ts b/packages/effect/src/internal/managedRuntime.ts index 5df50e6642a..a647fabc579 100644 --- a/packages/effect/src/internal/managedRuntime.ts +++ b/packages/effect/src/internal/managedRuntime.ts @@ -26,7 +26,7 @@ function provide( (rt) => core.withFiberRuntime((fiber) => { fiber.setFiberRefs(rt.fiberRefs) - fiber._runtimeFlags = rt.runtimeFlags + fiber.currentRuntimeFlags = rt.runtimeFlags return core.provideContext(effect, rt.context) }) ) diff --git a/packages/effect/src/internal/runtime.ts b/packages/effect/src/internal/runtime.ts index bf93d5b7eb8..2494b3f1a2e 100644 --- a/packages/effect/src/internal/runtime.ts +++ b/packages/effect/src/internal/runtime.ts @@ -74,7 +74,7 @@ export const unsafeFork = (runtime: Runtime.Runtime) => ) } - const supervisor = fiberRuntime._supervisor + const supervisor = fiberRuntime.currentSupervisor // we can compare by reference here as _supervisor.none is wrapped with globalValue if (supervisor !== _supervisor.none) { diff --git a/packages/opentelemetry/src/internal/tracer.ts b/packages/opentelemetry/src/internal/tracer.ts index 2e0d13d3405..5e0cf9c935c 100644 --- a/packages/opentelemetry/src/internal/tracer.ts +++ b/packages/opentelemetry/src/internal/tracer.ts @@ -3,7 +3,6 @@ import * as Cause from "effect/Cause" import * as Context from "effect/Context" import * as Effect from "effect/Effect" import type { Exit } from "effect/Exit" -import * as FiberRef from "effect/FiberRef" import * as Layer from "effect/Layer" import * as Option from "effect/Option" import * as EffectTracer from "effect/Tracer" @@ -143,9 +142,7 @@ export const make = Effect.map(Tracer, (tracer) => ) }, context(execution, fiber) { - const currentSpan = fiber.getFiberRef(FiberRef.currentContext).unsafeMap.get(EffectTracer.ParentSpan.key) as - | EffectTracer.AnySpan - | undefined + const currentSpan = fiber.currentSpan if (currentSpan === undefined) { return execution()