Repository navigation
Commit 16fc786
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
| 44 | + | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
| 60 | + | |
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
351 | 351 | | |
352 | 352 | | |
353 | 353 | | |
354 | | - | |
| 354 | + | |
355 | 355 | | |
356 | 356 | | |
357 | 357 | | |
358 | | - | |
| 358 | + | |
359 | 359 | | |
360 | 360 | | |
361 | 361 | | |
| |||
478 | 478 | | |
479 | 479 | | |
480 | 480 | | |
481 | | - | |
| 481 | + | |
482 | 482 | | |
483 | 483 | | |
484 | 484 | | |
485 | | - | |
| 485 | + | |
486 | 486 | | |
487 | 487 | | |
488 | 488 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | | - | |
| 55 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
| 2 | + | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
15 | 20 | | |
16 | 21 | | |
17 | 22 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| |||
0 commit comments