Repository navigation
Conversation
… flag Since vitejs#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.
Contributor
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
injectSsrFlagruns for every plugin'sresolveId,loadandtransformcall in a build. Since #24067 (Vite 8.4.0), it copies hook options throughObject.getOwnPropertyDescriptorsandObject.defineProperties, so that Rolldown's non-enumerable lazymagicStringandastgetters survive.Details
On V8, the previous copy:
Object.definePropertiesbuiltin;The new copy walks
Reflect.ownKeys(options):As a result:
__proto__key keep their exact descriptor;ssris overwritten in place when it already exists, and appended otherwise.The copied object has the same keys, key order, descriptors, getter identity and prototype as in Vite 8.4.0. The lazy getters are still not called.
Performance
Cost per call on Node 26.13.1 (V8 14.6):
resolveIdtransformmeta with lazy gettersloadIn a profile of a production build of about 31k modules,
injectSsrFlagtook 1.24 s of main-thread self time. This change should remove about 0.9 s of that.The existing
transform hook meta keeps rolldown magicString and asttest inpackages/vite/src/node/__tests__/build.spec.tscovers the property that #24067 preserved.