Skip to content

perf(build): avoid the descriptor round-trip when injecting the ssr flag - #24239

Open
camc314 wants to merge 1 commit into
vitejs:mainfrom
camc314:perf/inject-ssr-flag-fast-copy
Open

camc314 wants to merge 1 commit into
vitejs:mainfrom
camc314:perf/inject-ssr-flag-fast-copy

Conversation

@camc314

@camc314 camc314 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

injectSsrFlag runs for every plugin's resolveId, load and transform call in a build. Since #24067 (Vite 8.4.0), it copies hook options through Object.getOwnPropertyDescriptors and Object.defineProperties, so that Rolldown's non-enumerable lazy magicString and ast getters survive.

Details

On V8, the previous copy:

  • creates a descriptor object for every property, plus two intermediate objects;
  • leaves optimized code for the C++ Object.defineProperties builtin;
  • converts each descriptor back into an internal property descriptor and defines every property through the runtime.

The new copy walks Reflect.ownKeys(options):

if (descriptor.writable && descriptor.enumerable && descriptor.configurable && key !== '__proto__') {
  newOptions[key] = descriptor.value
} else {
  Object.defineProperty(newOptions, key, descriptor)
}

As a result:

  • plain data properties become keyed stores in generated code;
  • accessors, non-enumerable or read-only properties, and an own __proto__ key keep their exact descriptor;
  • ssr is 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):

Hook options Before After
resolveId 1,380 ns 232 ns
transform meta with lazy getters 1,592 ns 639 ns
load 598 ns 112 ns

In a profile of a production build of about 31k modules, injectSsrFlag took 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 ast test in packages/vite/src/node/__tests__/build.spec.ts covers the property that #24067 preserved.

… 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.
Copilot AI balanced review requested due to automatic review settings November 27, 2026 11:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions github-actions Bot added the bot: maybe Maybe a bot, LLM, or agent label Nov 27, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot: maybe Maybe a bot, LLM, or agent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants