Cause.combine hashes every reason into a Map and then compares the result with the original cause, even for two reasons
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
Issue này chưa được đánh giá.
Mô tả
What happens
Cause.combine removes duplicate reasons by hashing every reason of both causes into a Map of buckets, then builds a new cause and calls Equal.equals(self, newCause) to decide whether to return self. Hashing a reason hashes its tag, its error and its annotations map. For the usual case of one or two reasons on each side this costs about 0.8 KB and 600 ns per call, most of it hashing. The runtime combines causes when a finalizer fails after a failure, when concurrent children fail together and when an interrupt is added to a failure.
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 | main | with change |
|---|---|---|
Cause.combine(fail, fail), different errors |
830 B, 610 ns | 313 B, 90 ns |
Cause.combine(fail, fail), same cause |
1,016 B, 644 ns | 352 B, 64 ns |
Cause.combine(fail, die) |
888 B, 634 ns | 288 B, 81 ns |
Cause.combine(fail, interrupt) |
816 B, 643 ns | 288 B, 89 ns |
Cause.combine(8 failures, fail) |
2,402 B, 1,300 ns | 1,785 B, 524 ns |
| failure, then a finalizer that also fails | 1,912 B, 1,279 ns | 1,543 B, 960 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 cause-combine.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)
}
cause-combine.repro.ts
/**
* Cause.combine: cost of joining two causes, directly and where the runtime does it.
*
* node --expose-gc --min-semi-space-size=1024 --max-semi-space-size=1024 cause-combine.repro.ts
*/
import * as Cause from "effect/Cause"
import * as Effect from "effect/Effect"
import { measureEffect, measureSync } from "./harness.ts"
const N = 20000
const run = <A, E>(self: Effect.Effect<A, E>) => self
const a = Cause.fail("a")
const b = Cause.fail("b")
const die = Cause.die(new Error("boom"))
const interrupt = Cause.interrupt(1)
const many = [1, 2, 3, 4, 5, 6, 7, 8].map((i) => Cause.fail(`e${i}`)).reduce(Cause.combine)
measureSync("Cause.combine(fail, fail), different errors", N, () => Cause.combine(a, b))
measureSync("Cause.combine(fail, fail), same cause", N, () => Cause.combine(a, a))
measureSync("Cause.combine(fail, die)", N, () => Cause.combine(a, die))
measureSync("Cause.combine(fail, interrupt)", N, () => Cause.combine(a, interrupt))
measureSync("Cause.combine(8 failures, fail)", N, () => Cause.combine(many, b))
const failing = Effect.fail("boom")
measureEffect("failure, then a finalizer that also fails", N, () =>
Effect.exit(Effect.ensuring(failing, Effect.die("finalizer"))), run)
measureEffect("Effect.all of 4 failures, concurrent", N / 4, () =>
Effect.exit(Effect.all([failing, failing, failing, failing], { concurrency: "unbounded" })), run)
measureEffect("Effect.all of 4 failures, mode: validate-like (all errors kept)", N / 4, () =>
Effect.exit(Effect.forEach([1, 2, 3, 4], (i) => Effect.fail(`e${i}`), { concurrency: "unbounded", discard: true })), run)
Cause
dedupeReasons and causeCombine in packages/effect/src/internal/core.ts.
Possible fix
Compare reasons pairwise with Equal.equals when there are at most eight in total, which needs no hashing, and keep the hash buckets above that. Have dedupeReasons report whether anything changed, so causeCombine returns self without a second structural comparison.
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.
- Ngôn ngữ chính
- TypeScript
- Star
- 16.7k
- Fork
- 808
- Merge trung bình
- 21 giờ 22 phút
- Pull request đã merge (30 ngày)
- 486
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của Effect-TS/effect
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
`@effect/ui-native` (Effect-TS/ui): `kit/VirtualList` `list` part widens `R` to `any`, poisoning enclosing componentsCó thể đã có người làm @effect-bot đã nhận hôm nay. Đang mở
Effect-TS/effect#11097 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`@effect/ui-native` (Effect-TS/ui): Controlled `input` rejected-edit restore never calls `host.commit()`Có thể đã có người làm @effect-bot đã nhận hôm nay. Đang mở
Effect-TS/effect#11096 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của Effect-TS/effect
Issue tương tự
-
check:failed streams:edit
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
iptv-org/iptv#60585 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug]: Critical path skips a child span that ends exactly when the next one startsCó thể đã có người làm @Piyush4801 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
jaegertracing/jaeger-ui#4865 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 90/100
anthropics/claude-code#112972 ·
-
Type: Improvement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày