From c79fc10dcfd1c489a3609bba88413fe7b2f02043 Mon Sep 17 00:00:00 2001 From: Cameron Date: Fri, 27 Nov 2026 11:55:14 +0000 Subject: [PATCH] perf(build): avoid the descriptor round-trip when injecting the `ssr` flag Since #24067 (first released in Vite 8.4.0), `injectSsrFlag` copies the options of every plugin resolveId/load/transform call with: ```ts Object.defineProperties({}, { ...Object.getOwnPropertyDescriptors(options), ssr: { value: ssr, writable: true, enumerable: true, configurable: true }, }) ``` This keeps the non-enumerable lazy `magicString` and `ast` getters that Rolldown attaches to transform meta. A plain `{ ...options, ssr }` would drop non-enumerable properties and invoke the getters. However, the copy runs once per plugin for every hook call, so its cost scales with modules x plugins. In V8, this is an expensive way to copy a small object: - `Object.getOwnPropertyDescriptors` allocates one descriptor object for each own property, plus a holder object. - The object spread copies that holder into a second object. - `Object.defineProperties` is a C++ builtin. Each call leaves optimized code, converts every descriptor object back into an internal PropertyDescriptor, and defines each property through `JSReceiver::DefineOwnProperty`. None of these steps can use the inline caches that make ordinary property stores cheap in optimized code. The intermediate objects also add short-lived allocations, increasing scavenger (young-generation GC) work on a path that already allocates heavily. Copy the own keys one by one instead: - Writable, enumerable and configurable data properties are assigned directly. In practice, this covers almost every option key. V8 treats these as plain keyed stores in generated code and can follow the fresh object's cached hidden-class transitions because hook options have stable shapes. - Accessors such as `magicString` and `ast`, non-enumerable properties and read-only properties still use `Object.defineProperty` with their original descriptors. - An own `__proto__` key also uses `Object.defineProperty`, because assignment would call the `Object.prototype.__proto__` setter. - If `ssr` already exists, it is overwritten in place, preserving the position from the previous spread. Otherwise, it is appended. The resulting object is the same as before: - `Reflect.ownKeys` and `Object.getOwnPropertyDescriptors` use the same key set and order. - Every descriptor is preserved. - Getter functions are copied by identity and are not invoked. - The prototype is still `Object.prototype`. Per-call cost on Node 26.13.1 / V8 14.6, using the option shapes passed to each hook: ```ts resolveId options 1380 ns -> 232 ns transform meta (getters) 1592 ns -> 639 ns load options 598 ns -> 112 ns ``` In a CPU profile of a production build with about 31k modules on Vite 8.4.0, `injectSsrFlag` used 1.24 s of main-thread self time: 0.77 s for resolveId, 0.37 s for transform and 0.11 s for load. Based on the per-call measurements, this change should remove roughly 0.9 s. --- packages/vite/src/node/build.ts | 36 +++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 214cff3fa8dae7..2169b697a6dcbc 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -1547,14 +1547,34 @@ function injectSsrFlag>( let ssr = environment.config.consumer === 'server' // spreading the options would drop non-enumerable properties (e.g. the lazy // `magicString` and `ast` getters rolldown attaches to the transform meta), - // so copy the property descriptors instead - const newOptions = Object.defineProperties( - {}, - { - ...(options ? Object.getOwnPropertyDescriptors(options) : {}), - ssr: { value: ssr, writable: true, enumerable: true, configurable: true }, - }, - ) as T & { ssr?: boolean } + // so copy the property descriptors instead. Plain data properties are + // assigned directly because `Object.getOwnPropertyDescriptors` + + // `Object.defineProperties` is several times slower on this hot path. + const newOptions = {} as T & { ssr?: boolean } + let hasSsr = false + if (options) { + for (const key of Reflect.ownKeys(options)) { + if (key === 'ssr') { + newOptions.ssr = ssr + hasSsr = true + continue + } + const descriptor = Reflect.getOwnPropertyDescriptor(options, key)! + if ( + descriptor.writable && + descriptor.enumerable && + descriptor.configurable && + key !== '__proto__' + ) { + ;(newOptions as any)[key] = descriptor.value + } else { + Object.defineProperty(newOptions, key, descriptor) + } + } + } + if (!hasSsr) { + newOptions.ssr = ssr + } if ( isFutureDeprecationEnabled(