Repository navigation
Commit fff7df7
authored
perf(shared): skip extra promise in runWithSpan when no tracer is set (#2486)
## What
`runWithSpan` in `@orpc/shared` is no longer `async`. When no tracer is
set, it returns `fn()`'s promise as is: `Promise.resolve` for a sync
value, `Promise.reject` for a sync throw. The traced path is unchanged
and now lives in a private `async` helper.
## Why
`runWithSpan` wraps many steps per request: `call_procedure`,
`middleware.*`, `validate_input.*`, `validate_output.*`, `handler`,
`find_procedure`, `decode_input`, and encode, send and decode in the
client link. As an `async` function, it put the promise from `fn` inside
a second promise even with no tracer, which added 2-3 microtask turns
per call.
## Compatibility
- The public signature is still `Promise<T>`, so callers need no
changes.
- A sync throw from `fn` still becomes a rejected promise.
## Benchmarks (no tracer, best of two runs on a noisy machine)
| bench | before | after |
|---|---|---|
| procedure-call plain | 2.56M hz | 2.80M hz (+9%) |
| procedure-call validated | 1.57M | 1.80M (+15%) |
| procedure-call middlewares | 1.04M | 1.15M (+11%) |
| procedure-call full | 0.81M | 0.93M (+14%) |
| rpc-link-handler buffered | 23.1k | 23.2k (~flat) |
| rpc-link-handler event stream | 15.6k | 16.1k (+3%) |
## Tests
New no-tracer tests check three cases: the promise `fn` returns comes
back as is, a sync value is wrapped in a promise, and a sync throw
becomes a rejected promise.
Local runs:
- `pnpm check` passes.
- The root `vitest run` has one failure, in `openapi-inference` ("names
the nearest tsconfig…"). macOS writes temp paths as `/private/var` where
that test expects `/var`; it doesn't involve tracing.
- `packages/bun` tests didn't run because `bun` isn't installed locally.1 parent 0f896bc commit fff7df7
2 files changed
Lines changed: 36 additions & 2 deletions
File tree
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
178 | 178 | | |
179 | 179 | | |
180 | 180 | | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
181 | 203 | | |
182 | 204 | | |
183 | 205 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
165 | | - | |
| 165 | + | |
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
172 | | - | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
173 | 177 | | |
174 | 178 | | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
175 | 187 | | |
176 | 188 | | |
177 | 189 | | |
| |||
0 commit comments