Skip to content

Commit 16fc786

Browse files
authored
perf(shared, server, next): make procedure handlers cheaper to type-check (−12% to −25% instantiations) (#2457)
Reduces TypeScript instantiations of the server builder chain, for both implement-first and contract-first apps. Types behave the same. ## Changes 1. **`Promisable` with an explicit `Promise`** (`packages/shared/src/types.ts`): `@orpc/shared` now defines `Promisable<T> = Promise<T> | T | PromiseLike<T>` itself instead of re-exporting type-fest's `T | PromiseLike<T>`. `ProcedureHandler` returns `Promisable<THandlerOutput>`, so before this change every async handler's `Promise<X>` was inferred structurally against `PromiseLike`. With an explicit `Promise<T>` member it matches directly. `PromiseLike` stays so handlers that return thenables (e.g. Knex query builders) still work. - Assignability is unchanged, because `Promise<T>` is already a `PromiseLike<T>`. - The order of the union members makes no difference to the result: all orderings measured the same, because TypeScript sorts union members by type id. - Dropping `PromiseLike` entirely would cut more (−20.9%), but it breaks thenable handlers, so this PR doesn't do it. 2. **Non-generic `.handler` after `.output()`** (`BuilderWithOutput`, `BuilderWithInputOutput` in `packages/server/src/builder-variants.ts`): `handler<T extends InferSchemaInput<TOutputSchema>>` never used `T` in its return type. It now takes `InferSchemaInput<TOutputSchema>` directly, the same as `ProcedureImplementer.handler`. Object literals with extra properties are still accepted, as before. 3. **Lint fix in `@orpc/next`** (`packages/next/src/deferred-interceptors.ts`): with `Promise` now explicit in `Promisable`, `no-floating-promises` flags the fire-and-forget callbacks inside `setTimeout`. They are now marked with `void`. Runtime behavior is unchanged. ## Measurements TS 7.0.2, `tsc --extendedDiagnostics --checkers 1` (the default 4 checkers double-count). Fixture: 500 generated router files × 6 procedures, `base.input(z.object(..)).output(z.object(..)).handler(async () => ...)`, half with `.use(mw)`. | Scenario | Before | After | Δ | |---|---|---|---| | Change 1, implement-first | 2,372,613 | 2,030,627 | −14.4% | | Change 1, contract-first (`implement(contract)` + handler per procedure) | 3,001,248 | 2,638,262 | −12.1% | | Change 2 alone, implement-first | — | — | −10.1% | | Change 2 alone, contract-first | — | — | no change | Spot check on a smaller fixture (200 files × 6 procedures, implement-first, both changes together): 1,141,366 → 853,878 (−25.2%). On a mixed fixture (200 files × 6 procedures, implement-first + contract-first), the shared `Promisable` and an inline union in `ProcedureHandler` measure the same (1,722,534 vs 1,722,560). Across the whole repo (`tsc -p .`) the shared version is about 0.4% higher (2,483,025 vs 2,472,806), since other `Promisable` call sites don't benefit. ## Tests Existing tests cover the change; `pnpm format`, `pnpm check` and `pnpm test` pass.
1 parent f1015a6 commit 16fc786

9 files changed

Lines changed: 19 additions & 16 deletions

File tree

‎packages/next/src/deferred-interceptors.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const onStartDeferred: typeof onStart = (callback, ...rest) => {
99
return onStart(
1010
(...args) => {
1111
setTimeout(() => {
12-
callback(...args)
12+
void callback(...args)
1313
}, 6)
1414
},
1515
...rest,
@@ -25,7 +25,7 @@ export const onSuccessDeferred: typeof onSuccess = (callback, ...rest) => {
2525
return onSuccess(
2626
(...args) => {
2727
setTimeout(() => {
28-
callback(...args)
28+
void callback(...args)
2929
}, 6)
3030
},
3131
...rest,
@@ -41,7 +41,7 @@ export const onErrorDeferred: typeof onError = (callback, ...rest) => {
4141
return onError(
4242
(...args) => {
4343
setTimeout(() => {
44-
callback(...args)
44+
void callback(...args)
4545
}, 6)
4646
},
4747
...rest,
@@ -57,7 +57,7 @@ export const onFinishDeferred: typeof onFinish = (callback, ...rest) => {
5757
return onFinish(
5858
(...args) => {
5959
setTimeout(() => {
60-
callback(...args)
60+
void callback(...args)
6161
}, 6)
6262
},
6363
...rest,

‎packages/server/src/builder-variants.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,11 @@ export interface BuilderWithOutput<
351351
*
352352
* @see {@link https://orpc.dev/docs/procedure | Procedure}
353353
*/
354-
'handler'<T extends InferSchemaInput<TOutputSchema>>(
354+
'handler'(
355355
handler: ProcedureHandler<
356356
MergedContext<TInitialContext, TInjectedContext>,
357357
InferSchemaOutput<InitialInputSchema>,
358-
T,
358+
InferSchemaInput<TOutputSchema>,
359359
ORPCErrorConstructorMap<TErrorMap>
360360
>,
361361
): DecoratedProcedure<
@@ -478,11 +478,11 @@ export interface BuilderWithInputOutput<
478478
*
479479
* @see {@link https://orpc.dev/docs/procedure | Procedure}
480480
*/
481-
'handler'<T extends InferSchemaInput<TOutputSchema>>(
481+
'handler'(
482482
handler: ProcedureHandler<
483483
MergedContext<TInitialContext, TInjectedContext>,
484484
InferSchemaOutput<TInputSchema>,
485-
T,
485+
InferSchemaInput<TOutputSchema>,
486486
ORPCErrorConstructorMap<TErrorMap>
487487
>,
488488
): DecoratedProcedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>

‎packages/shared/src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ export {
5252

5353
export type { AsyncCleanupFn, AsyncIteratorClassNextFn } from '@standard-server/shared'
5454

55-
export type { Arrayable, IsEqual, PartialDeep, Promisable, Writable } from 'type-fest'
55+
export type { Arrayable, IsEqual, PartialDeep, Writable } from 'type-fest'

‎packages/shared/src/interceptor.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { isAsyncIteratorObject } from '@standard-server/shared'
2-
import type { Promisable } from 'type-fest'
32

43
import { wrapAsyncIterator } from './iterator'
54
import { override } from './proxy'
65
import { wrapReadableStream } from './stream'
6+
import type { Promisable } from './types'
77
import type { PromiseWithError, ThrowableError } from './types'
88

99
export type InterceptableOptions = Record<string, any>

‎packages/shared/src/iterator.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { AsyncIteratorClass } from '@standard-server/shared'
2-
import type { Promisable } from 'type-fest'
32

43
import { once } from './function'
54
import { AsyncIdQueue } from './queue'
65
import type { StartSpanOptions } from './tracing'
76
import { recordSpanError, runInSpanContext, startSpan } from './tracing'
7+
import type { Promisable } from './types'
88
import type { PromiseWithError, ThrowableError } from './types'
99

1010
export interface WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn> {

‎packages/shared/src/stream.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { AsyncIteratorClass } from '@standard-server/shared'
2-
import type { Promisable } from 'type-fest'
32

43
import { once } from './function'
54
import { isPlainObject } from './object'
65
import { promiseWithResolvers } from './promise'
76
import type { StartSpanOptions } from './tracing'
87
import { recordSpanError, runInSpanContext, startSpan } from './tracing'
8+
import type { Promisable } from './types'
99
import type { ThrowableError } from './types'
1010

1111
export function replicateReadableStream<T>(

‎packages/shared/src/tracing.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { Promisable } from 'type-fest'
2-
31
import { isAbortError } from './error'
2+
import type { Promisable } from './types'
43

54
const TRACER_SYMBOL = /* @__PURE__ */ Symbol.for('ORPC_TRACER')
65

‎packages/shared/src/types.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export type DistributiveOmit<T, K extends PropertyKey> = {
1212
*/
1313
export type Public<T> = Pick<T, keyof T>
1414

15+
/**
16+
* A value, or a promise or thenable of it.
17+
*/
18+
export type Promisable<T> = Promise<T> | T | PromiseLike<T>
19+
1520
export type PromiseWithError<T, TError> = Promise<T> & { __error?: { type: TError } }
1621

1722
/**

‎packages/shared/src/value.test-d.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { Promisable } from 'type-fest'
2-
1+
import type { Promisable } from './types'
32
import type { Value } from './value'
43
import { value } from './value'
54

0 commit comments

Comments
 (0)