Effect.provideService builds a new dual wrapper on every call
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 25/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Ferma
- Stack tecnologico
- node.js, typescript
- Ambito
- backend, performance
Direzione di ricerca
Read packages/effect/src/internal/effect.ts at provideService and inspect the existing tests for its data-first, pipeable, and curried call forms. The issue says a pull request with the change and tests follows; check PR #11063 for the implementation and test locations. Done means avoiding the per-call wrapper allocation while preserving all three call forms and their behavior.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
What happens
Effect.provideService is a plain function that, when it is given two or three arguments, calls dual(3, (self, service, impl) => ...) and then applies the result to its own arguments. dual therefore runs on every call and returns a new function, which is used once and dropped, together with the arrow function passed to it. The data-first call allocates about 990 B, against about 660 B for the curried form Effect.provideService(key)(self, value), which does not take that path.
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 (context of 0 or 300 services) | main | with change |
|---|---|---|
Effect.sync (for scale), 0 services |
84 B, 61 ns | 84 B, 69 ns |
Effect.provideService(self, key, value), 0 services |
989 B, 219 ns | 661 B, 175 ns |
self.pipe(Effect.provideService(key, value)), 0 services |
1,101 B, 227 ns | 821 B, 207 ns |
Effect.provideService(key)(self, value), 0 services |
661 B, 169 ns | 662 B, 188 ns |
| same value already provided, 0 services | 988 B, 168 ns | 660 B, 144 ns |
Effect.sync (for scale), 300 services |
84 B, 69 ns | 83 B, 67 ns |
Effect.provideService(self, key, value), 300 services |
988 B, 171 ns | 660 B, 149 ns |
self.pipe(Effect.provideService(key, value)), 300 services |
1,100 B, 176 ns | 820 B, 166 ns |
Effect.provideService(key)(self, value), 300 services |
660 B, 147 ns | 659 B, 149 ns |
| same value already provided, 300 services | 987 B, 175 ns | 660 B, 140 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 provide-service.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)
}
provide-service.repro.ts
/**
* Effect.provideService: fixed cost of one call in each calling form.
*
* node --expose-gc --min-semi-space-size=1024 --max-semi-space-size=1024 provide-service.repro.ts
*/
import * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import { measureEffect } from "./harness.ts"
const N = 20000
class Request extends Context.Service<Request, { readonly id: number }>()("Request") {}
const step = Effect.sync(() => 1)
const request = { id: 1 }
const forRequest = Effect.provideService(Request)
const services = (count: number) => {
let context = Context.empty()
for (let i = 0; i < count; i++) context = Context.add(context, Context.Service<number>(`Service${i}`), i)
return <A, E>(self: Effect.Effect<A, E>) => Effect.provideContext(self, context)
}
for (const count of [0, 300]) {
const wrap = services(count)
const label = (name: string) => `${name}, ${count} services`
measureEffect(label("Effect.sync (for scale)"), N, () => step, wrap)
measureEffect(label("Effect.provideService(self, key, value)"), N, () => Effect.provideService(step, Request, request), wrap)
measureEffect(label("self.pipe(Effect.provideService(key, value))"), N, () => step.pipe(Effect.provideService(Request, request)), wrap)
measureEffect(label("Effect.provideService(key)(self, value)"), N, () => forRequest(step, request), wrap)
measureEffect(label("same value already provided"), N, () => Effect.provideService(step, Request, request), (self) =>
wrap(Effect.provideService(self, Request, request)))
}
Cause
provideService in packages/effect/src/internal/effect.ts: return dual(3, (self, service, impl) => provideServiceImpl(self, service, impl)).apply(this, arguments).
Possible fix
Test arguments.length directly: three arguments call provideServiceImpl, two return (self) => provideServiceImpl(self, key, value), and one keeps the existing curried form.
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.
- Lingua principale
- TypeScript
- Stelle
- 16.7k
- Fork
- 808
- Merge medio
- 21h 22m
- PR unite (30g)
- 486
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Effect-TS/effect
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
`@effect/ui-native` (Effect-TS/ui): `kit/VirtualList` `list` part widens `R` to `any`, poisoning enclosing componentsForse già presa @effect-bot l’ha presa oggi. Aperta
Effect-TS/effect#11097 · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
`@effect/ui-native` (Effect-TS/ui): Controlled `input` rejected-edit restore never calls `host.commit()`Forse già presa @effect-bot l’ha presa oggi. Aperta
Effect-TS/effect#11096 · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di Effect-TS/effect
Issue simili
-
check:failed streams:edit
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
iptv-org/iptv#60585 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 67/100
I maintainer di solito rispondono entro 1 giorno
-
[Bug]: Critical path skips a child span that ends exactly when the next one startsForse già presa @Piyush4801 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
jaegertracing/jaeger-ui#4865 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 90/100
anthropics/claude-code#112972 ·
-
Type: Improvement
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno