Skip to content

Commit 6da66ca

Browse files
andymac4182claudeeffect-bot
authored
perf(effect): stop provideService building a dual wrapper per call (#11063)
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 e4541d4 commit 6da66ca

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

‎.changeset/provide-service-call.md‎

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 allocations in data-first and data-last `Effect.provideService` calls.

‎packages/effect/src/internal/effect.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,13 +2514,15 @@ export const provideService: {
25142514
service: Context.Key<I, S>,
25152515
implementation: S
25162516
): Effect.Effect<A, E, Exclude<R, I>>
2517-
} = function(this: any) {
2518-
if (arguments.length === 1) {
2519-
return dual(2, (self, impl) => provideServiceImpl(self, arguments[0], impl)) as any
2520-
}
2521-
return dual(3, (self, service, impl) => provideServiceImpl(self, service, impl))
2522-
.apply(this, arguments as any) as any
2523-
}
2517+
} = function(first: any, second?: any, third?: any) {
2518+
switch (arguments.length) {
2519+
case 1:
2520+
return dual(2, (self: any, implementation: any) => provideServiceImpl(self, first, implementation))
2521+
case 2:
2522+
return (self: any) => provideServiceImpl(self, first, second)
2523+
}
2524+
return provideServiceImpl(first, second, third)
2525+
} as any
25242526

25252527
const provideServiceImpl = <A, E, R, I, S>(
25262528
self: Effect.Effect<A, E, R>,
@@ -2548,7 +2550,7 @@ export const provideServiceEffect: {
25482550
service: Context.Key<I, S>,
25492551
acquire: Effect.Effect<S, E2, R2>
25502552
): Effect.Effect<A, E | E2, Exclude<R, I> | R2> =>
2551-
flatMap(acquire, (implementation) => provideService(self, service, implementation))
2553+
flatMap(acquire, (implementation) => provideServiceImpl(self, service, implementation))
25522554
)
25532555

25542556
// ----------------------------------------------------------------------------

‎packages/effect/test/Effect.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,19 @@ describe("Effect", () => {
474474
Effect.provideService(ATag, "A")
475475
))
476476

477+
it.effect("provideService call forms", () =>
478+
Effect.gen(function*() {
479+
class Name extends Context.Service<Name, string>()("Name") {}
480+
const greet = Effect.map(Name, (name) => `hello ${name}`)
481+
const provide = Effect.provideService(Name)
482+
assert.deepStrictEqual([
483+
yield* Effect.provideService(greet, Name, "first"),
484+
yield* greet.pipe(Effect.provideService(Name, "last")),
485+
yield* provide(greet, "curried first"),
486+
yield* greet.pipe(provide("curried last"))
487+
], ["hello first", "hello last", "hello curried first", "hello curried last"])
488+
}))
489+
477490
describe("fromOption", () => {
478491
it.effect("from a some", () =>
479492
Option.some("A").pipe(

0 commit comments

Comments
 (0)