Skip to content

Commit e4541d4

Browse files
andymac4182claudeeffect-bot
authored
perf(effect): run Metric.update, modify and value in one runtime step (#11057)
Co-authored-by: Andrew McClenaghan <1297073+andymac4182@users.noreply.github.com> Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com> Co-authored-by: Effect Bot <tech-ops@effectful.co>
1 parent 945fe8f commit e4541d4

3 files changed

Lines changed: 48 additions & 20 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Reduce the per-call allocation and run-loop cost of `Metric.update`, `Metric.modify`, `Metric.value` and `Metric.remove`.

‎packages/effect/src/Metric.ts‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as Duration from "./Duration.ts"
1818
import type { Effect } from "./Effect.ts"
1919
import type { Exit } from "./Exit.ts"
2020
import { constUndefined, constVoid, dual, identity } from "./Function.ts"
21+
import { withFiberSucceed } from "./internal/core.ts"
2122
import * as InternalEffect from "./internal/effect.ts"
2223
import { FiberRuntimeMetricsKey as InternalFiberRuntimeMetricsKey } from "./internal/fiberCachedKeys.ts"
2324
import * as InternalRecord from "./internal/record.ts"
@@ -2693,11 +2694,7 @@ export const timer = (name: string, options?: {
26932694
*/
26942695
export const value = <Input, State>(
26952696
self: Metric<Input, State>
2696-
): Effect<State> =>
2697-
InternalEffect.flatMap(
2698-
InternalEffect.context(),
2699-
(context) => InternalEffect.sync(() => self.valueUnsafe(context))
2700-
)
2697+
): Effect<State> => withFiberSucceed((fiber) => self.valueUnsafe(fiber.context))
27012698

27022699
/**
27032700
* Modifies the metric with the specified input.
@@ -2748,11 +2745,7 @@ export const modify: {
27482745
} = dual<
27492746
<Input>(input: Input) => <State>(self: Metric<Input, State>) => Effect<void>,
27502747
<Input, State>(self: Metric<Input, State>, input: Input) => Effect<void>
2751-
>(2, (self, input) =>
2752-
InternalEffect.flatMap(
2753-
InternalEffect.context(),
2754-
(context) => InternalEffect.sync(() => self.modifyUnsafe(input, context))
2755-
))
2748+
>(2, (self, input) => withFiberSucceed((fiber) => self.modifyUnsafe(input, fiber.context)))
27562749

27572750
/**
27582751
* Updates the metric with the specified input.
@@ -2813,8 +2806,7 @@ export const update: {
28132806
<Input, State>(self: Metric<Input, State>, input: Input) => Effect<void>
28142807
>(
28152808
2,
2816-
(self, input) =>
2817-
InternalEffect.contextWith((services) => InternalEffect.sync(() => self.updateUnsafe(input, services)))
2809+
(self, input) => withFiberSucceed((fiber) => self.updateUnsafe(input, fiber.context))
28182810
)
28192811

28202812
/**
@@ -2881,7 +2873,7 @@ export interface Registration {
28812873
* @since 4.0.6
28822874
*/
28832875
export const remove = <Input, State>(self: Metric<Input, State>): Effect<void> =>
2884-
InternalEffect.contextWith((context) => InternalEffect.sync(() => removeUnsafe(self, context)))
2876+
withFiberSucceed((fiber) => removeUnsafe(self, fiber.context))
28852877

28862878
/**
28872879
* Removes the series of a metric for the attributes in the provided `Context`
@@ -3176,9 +3168,8 @@ export const withAttributes: {
31763168
* @category snapshotting
31773169
* @since 2.0.0
31783170
*/
3179-
export const snapshot: Effect<ReadonlyArray<Metric.Snapshot>> = InternalEffect.map(
3180-
InternalEffect.context(),
3181-
(context) => snapshotUnsafe(context)
3171+
export const snapshot: Effect<ReadonlyArray<Metric.Snapshot>> = withFiberSucceed((fiber) =>
3172+
snapshotUnsafe(fiber.context)
31823173
)
31833174

31843175
/**
@@ -3236,8 +3227,8 @@ export const snapshot: Effect<ReadonlyArray<Metric.Snapshot>> = InternalEffect.m
32363227
* @category formatting
32373228
* @since 4.0.0
32383229
*/
3239-
export const dump: Effect<string> = InternalEffect.flatMap(InternalEffect.context(), (context) => {
3240-
const metrics = snapshotUnsafe(context)
3230+
export const dump: Effect<string> = withFiberSucceed((fiber) => {
3231+
const metrics = snapshotUnsafe(fiber.context)
32413232
if (metrics.length > 0) {
32423233
const maxNameLength = metrics.reduce((max, metric) => {
32433234
const length = metric.id.length
@@ -3266,9 +3257,9 @@ export const dump: Effect<string> = InternalEffect.flatMap(InternalEffect.contex
32663257
renderState(metric)
32673258
).join("\n")
32683259
).join("\n")
3269-
return InternalEffect.succeed(rendered)
3260+
return rendered
32703261
}
3271-
return InternalEffect.succeed("")
3262+
return ""
32723263
})
32733264

32743265
/**

‎packages/effect/test/Metric.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,38 @@ describe("Metric", () => {
3535
assert.deepStrictEqual(valueB, { count: 10, incremental: false })
3636
}))
3737

38+
it.effect("update, modify, value and remove use the context of each run", () =>
39+
Effect.gen(function*() {
40+
const counter = Metric.counter(nextId())
41+
const update = Metric.update(counter, 2)
42+
const modify = Metric.modify(counter, 3)
43+
const value = Metric.value(counter)
44+
const remove = Metric.remove(counter)
45+
const run = Effect.gen(function*() {
46+
const before = (yield* value).count
47+
yield* update
48+
yield* modify
49+
const after = (yield* value).count
50+
yield* remove
51+
return [before, after]
52+
})
53+
const registry: Metric.MetricRegistry = new Map()
54+
55+
for (const current of [new Map(), registry, registry]) {
56+
assert.deepStrictEqual(yield* run.pipe(Effect.provideService(Metric.MetricRegistry, current)), [0, 5])
57+
}
58+
}))
59+
60+
it.effect("reports a modify that throws as a defect", () =>
61+
Effect.gen(function*() {
62+
const error = new Error("boom")
63+
const counter = Metric.mapInput(Metric.counter(nextId()), (_: number): number => {
64+
throw error
65+
})
66+
67+
assert.deepStrictEqual(yield* Effect.exit(Metric.modify(counter, 1)), Exit.die(error))
68+
}).pipe(Effect.provideService(Metric.MetricRegistry, new Map())))
69+
3870
it.effect("keeps attributed metrics scoped to the active registry", () =>
3971
Effect.gen(function*() {
4072
const counter = Metric.counter(nextId()).pipe(Metric.withAttributes(attributes))

0 commit comments

Comments
 (0)