Metric.update, Metric.modify and Metric.value take two runtime steps and two closures per call
维护者通常 1 天内回复
评估
- 难度
- 1/5
- 预计耗时
- 1 小时以内
- 新手友好度
- 20/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- typescript
- 领域
- performance
调研方向
Start with packages/effect/src/Metric.ts, where the issue identifies update, modify, and value. Read those implementations, then run the reproduction harness described in the issue to compare allocations and runtime. The issue says a pull request with the change and tests follows; that pull request is already open, so the work is under way.
由索引模型根据 Issue 内容生成。
描述
What happens
Metric.update is contextWith((context) => sync(() => self.updateUnsafe(input, context))): one step to read the context, which returns a second effect that applies the update. Metric.modify and Metric.value do the same through flatMap(context(), ...). Each call therefore creates two effects and two closures and takes two turns of the run loop. An update of a known series costs about 460 B, against about 170 B for a single Effect.sync.
Measurements
Measured on main at 584c9e0 (4.1.7), Node.js 24.22, Apple silicon. Bytes are heap growth per operation with no collection during the loop, median of seven rounds.
| Case (series already registered) | main | with change |
|---|---|---|
Effect.sync (for scale) |
173 B, 82 ns | 173 B, 95 ns |
Metric.update(counter, 1) |
464 B, 118 ns | 220 B, 95 ns |
Metric.update(histogram, n) |
461 B, 129 ns | 237 B, 110 ns |
Metric.update(gauge, n) |
466 B, 114 ns | 248 B, 103 ns |
Metric.update of a counter with withAttributes |
587 B, 135 ns | 363 B, 116 ns |
Metric.modify(counter, 1) |
414 B, 148 ns | 245 B, 102 ns |
Metric.value(counter) |
446 B, 152 ns | 312 B, 107 ns |
Reproduction
Save the two files below in packages/effect/alloc-repro/ in a checkout of this repository and run:
node --expose-gc --min-semi-space-size=1024 --max-semi-space-size=1024 metric-update.repro.ts
harness.ts
/**
* Shared measurement helper: bytes and nanoseconds per operation.
* node --expose-gc --min-semi-space-size=1024 --max-semi-space-size=1024 <file>.ts
* Heap growth is read across N operations with a forced GC before and a young
* generation large enough that no collection runs in between: the
* operation count is cut so one round stays under BUDGET bytes.
*/
import * as Effect from "effect/Effect"
const gc = (globalThis as { gc?: () => void }).gc
if (gc === undefined) throw new Error("run with --expose-gc")
export const ROUNDS = 7
const BUDGET = 256 * 1024 * 1024
const sized = (n: number, probeBytes: number) => Math.max(50, Math.min(n, Math.floor(BUDGET / Math.max(probeBytes, 1))))
const median = (xs: Array<number>) => xs.toSorted((a, b) => a - b)[Math.floor(xs.length / 2)]
export const row = (label: string, bytes: number, ns: number) =>
console.log(`${label.padEnd(64)} ${bytes.toFixed(0).padStart(8)} B/op ${ns.toFixed(0).padStart(8)} ns/op`)
/** Measures a synchronous operation. */
export const measureSync = (label: string, n: number, op: (i: number) => unknown) => {
for (let i = 0; i < 5000; i++) op(i)
gc()
const probe = process.memoryUsage().heapUsed
for (let i = 0; i < 50; i++) op(i)
n = sized(n, (process.memoryUsage().heapUsed - probe) / 50)
const bytes: Array<number> = []
const nanos: Array<number> = []
for (let r = 0; r < ROUNDS; r++) {
gc()
const before = process.memoryUsage().heapUsed
const start = process.hrtime.bigint()
for (let i = 0; i < n; i++) op(i)
const end = process.hrtime.bigint()
bytes.push((process.memoryUsage().heapUsed - before) / n)
nanos.push(Number(end - start) / n)
}
row(label, median(bytes), median(nanos))
return median(bytes)
}
/** Measures an Effect run `n` times inside one fiber, so the fiber context is the one supplied by `wrap`. */
export const measureEffect = <E, R>(
label: string,
n: number,
op: (i: number) => Effect.Effect<unknown, E, R>,
wrap: (self: Effect.Effect<void, E, R>) => Effect.Effect<void, E, never>
) => {
const bytes: Array<number> = []
const nanos: Array<number> = []
const loop = (count: number) =>
Effect.gen(function*() {
for (let i = 0; i < count; i++) yield* op(i)
})
const program = Effect.gen(function*() {
yield* loop(5000)
gc()
const probe = process.memoryUsage().heapUsed
yield* loop(50)
n = sized(n, (process.memoryUsage().heapUsed - probe) / 50)
for (let r = 0; r < ROUNDS; r++) {
gc()
const before = process.memoryUsage().heapUsed
const start = process.hrtime.bigint()
yield* loop(n)
const end = process.hrtime.bigint()
bytes.push((process.memoryUsage().heapUsed - before) / n)
nanos.push(Number(end - start) / n)
}
})
Effect.runSync(wrap(program))
row(label, median(bytes), median(nanos))
return median(bytes)
}
metric-update.repro.ts
/**
* Metric.update: fixed cost of one update when the series is already known.
*
* node --expose-gc --min-semi-space-size=1024 --max-semi-space-size=1024 metric-update.repro.ts
*/
import * as Effect from "effect/Effect"
import * as Metric from "effect/Metric"
import { measureEffect } from "./harness.ts"
const N = 20000
const shared = { environment: "production", region: "us-west-2", service: "example", process_group: "worker" }
const requests = Metric.counter("requests")
const attributed = Metric.withAttributes(requests, { outcome: "ok", route: "/items" })
const twice = Metric.withAttributes(attributed, { method: "GET" })
const latency = Metric.histogram("latency", { boundaries: [1, 5, 10, 50, 100, 500] })
const gauge = Metric.gauge("inflight")
const run = <A, E>(self: Effect.Effect<A, E>) =>
self.pipe(Effect.provideService(Metric.CurrentMetricAttributes, shared), Effect.provideService(Metric.MetricRegistry, new Map()))
measureEffect("Effect.sync (for scale)", N, () => Effect.sync(() => 1), run)
measureEffect("Metric.update(counter, 1)", N, () => Metric.update(requests, 1), run)
measureEffect("Metric.update(histogram, n)", N, (i) => Metric.update(latency, i % 400), run)
measureEffect("Metric.update(gauge, n)", N, (i) => Metric.update(gauge, i), run)
measureEffect("Metric.update(counter with attributes, 1)", N, () => Metric.update(attributed, 1), run)
measureEffect("Metric.update(counter with attributes twice, 1)", N, () => Metric.update(twice, 1), run)
measureEffect("Metric.modify(counter, 1)", N, () => Metric.modify(requests, 1), run)
measureEffect("Metric.value(counter)", N, () => Metric.value(requests), run)
Cause
update, modify and value in packages/effect/src/Metric.ts.
Possible fix
Use one withFiber step that reads fiber.context and applies the update, returning Effect.void (or the value).
Context
Found from CPU and sampling heap profiles of a production Node.js service on effect 4.0.1. A pull request with the change and tests follows.
- 主要语言
- TypeScript
- 星标
- 16.7k
- 派生
- 808
- 平均合并
- 21 小时 22 分钟
- 30 天内合并 PR
- 486
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
Effect-TS/effect 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
`@effect/ui-native` (Effect-TS/ui): `kit/VirtualList` `list` part widens `R` to `any`, poisoning enclosing components可能已有人在做 @effect-bot 今天认领。 未关闭
Effect-TS/effect#11097 · 已指派 1 人 ·
维护者通常 1 天内回复
-
`@effect/ui-native` (Effect-TS/ui): Controlled `input` rejected-edit restore never calls `host.commit()`可能已有人在做 @effect-bot 今天认领。 未关闭
Effect-TS/effect#11096 · 已指派 1 人 ·
维护者通常 1 天内回复
相似的 Issue
-
check:failed streams:edit
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 67/100
维护者通常 1 天内回复
-
[Bug]: Critical path skips a child span that ends exactly when the next one starts可能已有人在做 @Piyush4801 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
jaegertracing/jaeger-ui#4865 · 1 条评论 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 90/100
anthropics/claude-code#112972 ·
-
Type: Improvement
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复