Effect.all over a struct allocates an entry tuple and a mapped effect per field

オープン
#11,064 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@andymac4182 がすでに取り組んでいます。

2026年11月29日 から。

  • #11065 @andymac4182 による — オープン

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
20/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
node.js, typescript

調査の方向性

Read the struct branch at the end of packages/effect/src/internal/effect.ts and inspect the existing Effect.all tests. The reproduction files and Node.js command are included in the issue; compare struct allocation results with the reported measurements. Done means the struct form avoids the per-field allocations described while preserving its results and options, with tests covering the behavior. A linked pull request is already open.

索引モデルが issue の本文から書いたものです。

説明

What happens

Effect.all with a struct of effects turns the struct into an array of [key, effect] tuples, wraps every effect in Effect.map to produce a [key, value] tuple, runs them through forEach, and rebuilds the object with Object.fromEntries. That is two tuples, one mapped effect and one closure per field on top of what the tuple form does. Three fields cost about 2.8 KB against 1.2 KB for the same effects in a tuple; ten fields cost 5.3 KB against 1.7 KB.

Measurements

Measured on main at ddc5b58 (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. Timings were taken on a machine under other load and vary by about 20% between runs; the byte figures are stable.

Case main with change
Effect.all([a, b, c]) 1,244 B, 370 ns 1,244 B, 356 ns
Effect.all({ a, b, c }) 2,797 B, 839 ns 1,842 B, 522 ns
Effect.all({ a, b, c }, { mode: "result" }) 3,498 B, 1,044 ns 2,625 B, 765 ns
Effect.all({ a, b, c }, { concurrency: "unbounded" }) 4,302 B, 852 ns 3,003 B, 606 ns
Effect.all of 10 in a tuple 1,684 B, 746 ns 1,686 B, 734 ns
Effect.all of 10 in a struct 5,255 B, 1,672 ns 2,362 B, 901 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 effect-all-struct.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)
}
effect-all-struct.repro.ts
/**
 * Effect.all with a struct of effects, against the same effects in a tuple.
 *
 *   node --expose-gc --min-semi-space-size=1024 --max-semi-space-size=1024 effect-all-struct.repro.ts
 */
import * as Effect from "effect/Effect"
import { measureEffect } from "./harness.ts"

const N = 20000
const id = <A, E>(self: Effect.Effect<A, E>) => self
const step = Effect.sync(() => 1)
const struct = (count: number) => Object.fromEntries(Array.from({ length: count }, (_, i) => [`field${i}`, step]))
const three = struct(3)
const ten = struct(10)

measureEffect("Effect.all([a, b, c])", N, () => Effect.all([step, step, step]), id)
measureEffect("Effect.all({ a, b, c })", N, () => Effect.all(three), id)
measureEffect("Effect.all({ a, b, c }, { mode: \"result\" })", N, () => Effect.all(three, { mode: "result" }), id)
measureEffect("Effect.all({ a, b, c }, { concurrency: \"unbounded\" })", N, () => Effect.all(three, { concurrency: "unbounded" }), id)
measureEffect("Effect.all of 10 in a tuple", N, () => Effect.all(Object.values(ten)), id)
measureEffect("Effect.all of 10 in a struct", N, () => Effect.all(ten), id)

Cause

The struct branch at the end of all in packages/effect/src/internal/effect.ts.

Possible fix

Run forEach over the keys, returning each field's effect directly. forEach returns the values in key order, so the result object can be filled by index from the key array.

Context

Found by measuring the Effect APIs that a production Node.js service on effect 4.0.1 calls most, after sampling heap profiles of that service showed these call sites. A pull request with the change and tests follows.

主要言語
TypeScript
スター
16.7k
フォーク
808
平均マージ
21時間 22分
マージ済み PR(30日)
486

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Effect-TS/effect のほかの issue

Effect-TS/effect の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。