perf(loader): use synchronous iitm hooks when available#8942
Conversation
Overall package sizeSelf size: 6.34 MB Dependency sizes| name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.2.0 | 104.26 kB | 843.44 kB | | opentracing | 0.14.7 | 194.81 kB | 194.81 kB | | dc-polyfill | 0.1.11 | 25.74 kB | 25.74 kB |🤖 This report was automatically generated by heaviest-objects-in-the-universe |
🎉 All green!🧪 All tests passed 🔗 Commit SHA: 39e7df0 | Docs | Datadog PR Page | Give us feedback! |
BenchmarksBenchmark execution time: 2026-06-25 16:13:28 Comparing candidate commit 39e7df0 in PR branch Found 2 performance improvements and 2 performance regressions! Performance is the same for 1948 metrics, 13 unstable metrics.
|
The startup variants load the fixture through CommonJS require, which goes through require-in-the-middle and never registers the iitm ESM loader. That loader is what the synchronous module.registerHooks work changes from an off-thread async loader to an in-thread sync one, so the suite reported no movement for that change: nothing put the loader on a measured path. with-tracer-everything-esm registers the loader via --import ../../../register.js and imports the same fixture through ESM, so every dependency and its transitive graph flow through the loader's resolve/load hooks. The fixture's index.mjs reads the same dependencies as index.js, so the dependency list stays single-sourced in package.json. Refs: #8942
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 36603d5fd3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 32305737e1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
The startup variants load the fixture through CommonJS require, which goes through require-in-the-middle and never registers the iitm ESM loader. That loader is what the synchronous module.registerHooks work changes from an off-thread async loader to an in-thread sync one, so the suite reported no movement for that change: nothing put the loader on a measured path. with-tracer-everything-esm registers the loader via --import ../../../register.js and imports the same fixture through ESM, so every dependency and its transitive graph flow through the loader's resolve/load hooks. The fixture's index.mjs reads the same dependencies as index.js, so the dependency list stays single-sourced in package.json. Refs: #8942
26fa3f4 to
5c1803c
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5c1803c141
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Do not silently fall back to the async loader on Node versions where the synchronous loader should work. If sync hook registration fails or returns false on those versions, surface the failure so regressions are visible in CI.
Preserve the async fallback when require(esm) is explicitly disabled with --no-require-module, because that mode prevents the CJS preloader from requiring loader-hook.mjs.
Validation: git diff --check; ./node_modules/.bin/eslint register.js packages/dd-trace/test/register.spec.js; ./node_modules/.bin/mocha packages/dd-trace/test/register.spec.js; npm run lint:codeowners:ci; node --import ./register.js -e "console.log('register ok')"
78100e6 to
39e7df0
Compare
The startup variants load the fixture through CommonJS require, which goes through require-in-the-middle and never registers the iitm ESM loader. That loader is what the synchronous module.registerHooks work changes from an off-thread async loader to an in-thread sync one, so the suite reported no movement for that change: nothing put the loader on a measured path. with-tracer-everything-esm registers the loader via --import ../../../register.js and imports the same fixture through ESM, so every dependency and its transitive graph flow through the loader's resolve/load hooks. The fixture's index.mjs reads the same dependencies as index.js, so the dependency list stays single-sourced in package.json. Refs: #8942
What does this PR do?
Uses
module.registerHooks()fromdd-trace/register.jswhen the runtime supports the synchronousimport-in-the-middleloader path. Unsupported runtimes keep the existingmodule.register('./loader-hook.mjs')async fallback.The sync path reuses the same loader include/exclude setup as the async path, preserves the ESM rewriter path for non-
requireloads, and skips loader-level rewriting for CommonJSrequireloads so the existing_compilerewriter handles CJS once instead of nesting wrappers.Motivation
Vitest Test Optimization with
isolate: truestarts many worker processes, and each process pays loader initialization cost. On supported Node versions, the synchronous iitm loader path reduces that worker startup overhead without adding Vitest-specific loader narrowing.Benchmark
Benchmarked with Node 24.17.0 and Vitest 4.1.9 (
latest) in a local demo project with 200 generated test files and 2 zero-delay tests per file, for 400 total tests. The suite usedisolate: true,pool: forks,npm test -- --reporter=dot, and a local HTTP intake to verify Test Optimization payloads. Each row reports the median of 7 measured runs after 1 warmup.Compared
origin/master(ea50dc683) against this PR (8030bd7b8).masterLocal intake validation stayed correct: every traced run reported all 400 test events to the Test Optimization intake.