Skip to content

Commit fff7df7

Browse files
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

‎packages/shared/src/tracing.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,28 @@ describe('tracing', () => {
178178
expect(fn).toHaveBeenCalledWith()
179179
})
180180

181+
describe('without tracer', () => {
182+
it('returns the promise from fn as is', () => {
183+
const promise = Promise.resolve('out')
184+
expect(runWithSpan('test', () => promise)).toBe(promise)
185+
})
186+
187+
it('wraps a sync value in a promise', async () => {
188+
const result = runWithSpan('test', () => 'out')
189+
expect(result).toBeInstanceOf(Promise)
190+
expect(await result).toBe('out')
191+
})
192+
193+
it('turns a sync throw into a rejected promise', async () => {
194+
const error = new Error('fail')
195+
const result = runWithSpan('test', () => {
196+
throw error
197+
})
198+
expect(result).toBeInstanceOf(Promise)
199+
await expect(result).rejects.toBe(error)
200+
})
201+
})
202+
181203
it('starts active span, runs function and ends span', async () => {
182204
const tracer = createMockTracer()
183205
const mockSpan = createMockSpan()

‎packages/shared/src/tracing.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,28 @@ export function toSpanAttributeValue(data: unknown): string {
162162
}
163163
}
164164

165-
export async function runWithSpan<T>(
165+
export function runWithSpan<T>(
166166
options: string | StartSpanOptions,
167167
fn: (span?: TracingSpan) => Promisable<T>,
168168
): Promise<T> {
169169
const tracer = getTracer()
170170

171171
if (!tracer) {
172-
return fn()
172+
try {
173+
return Promise.resolve(fn())
174+
} catch (e) {
175+
return Promise.reject(e)
176+
}
173177
}
174178

179+
return runWithTracerSpan(tracer, options, fn)
180+
}
181+
182+
async function runWithTracerSpan<T>(
183+
tracer: Tracer,
184+
options: string | StartSpanOptions,
185+
fn: (span?: TracingSpan) => Promisable<T>,
186+
): Promise<T> {
175187
const name = typeof options === 'string' ? options : options.name
176188
const parent = typeof options === 'string' ? undefined : options.parent
177189

0 commit comments

Comments
 (0)