Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: integrate TypeScript into compile cache #56629

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Jan 16, 2025

This integrates TypeScript into the compile cache by caching the transpilation (either type-stripping or transforming) output in addition to the V8 code cache that's generated from the transpilation output.

Locally this speeds up loading with type stripping of benchmark/fixtures/strip-types-benchmark.ts by ~65% and loading with type transforms of
fixtures/transform-types-benchmark.ts by ~128%.

When comparing loading .ts and loading pre-transpiled .js on-disk with the compile cache enabled, previously .ts loaded 46% slower with type-stripping and 66% slower with transforms compared to loading .js files directly.
After this patch, .ts loads 12% slower with type-stripping and 22% slower with transforms compared to .js.

(Note that the numbers are based on microbenchmark fixtures and do not necessarily represent real-world workloads, though with bigger real-world files, the speed up should be more significant).

There are some TODOs left for avoiding the excessive UTF8 transcoding, which depends on swc-project/swc#9851 though the numbers are already good enough that I think that can be done as a follow-up..when swc actually supports it.

With compile cache enabled via export NODE_COMPILE_CACHE=/tmp:

                                                                                                                        confidence improvement accuracy (*)    (**)   (***)
ts/strip-typescript.js n=1000 filepath='/Users/joyee/projects/node/benchmark/fixtures/strip-types-benchmark.js'                         2.31 %       ±3.44%  ±4.62%  ±6.10%
ts/strip-typescript.js n=1000 filepath='/Users/joyee/projects/node/benchmark/fixtures/strip-types-benchmark.ts'                ***     65.37 %       ±3.99%  ±5.35%  ±7.03%
ts/transform-typescript.js n=1000 filepath='/Users/joyee/projects/node/benchmark/fixtures/transform-types-benchmark.js'                 0.37 %       ±1.37%  ±1.82%  ±2.37%
ts/transform-typescript.js n=1000 filepath='/Users/joyee/projects/node/benchmark/fixtures/transform-types-benchmark.ts'        ***    128.86 %       ±7.94% ±10.69% ±14.17%

Without compile cache (i.e. there should be no regression when it's not enabled):

                                                                                                                        confidence improvement accuracy (*)   (**)  (***)
ts/strip-typescript.js n=1000 filepath='/Users/joyee/projects/node/benchmark/fixtures/strip-types-benchmark.js'                        -0.94 %       ±4.40% ±5.86% ±7.63%
ts/strip-typescript.js n=1000 filepath='/Users/joyee/projects/node/benchmark/fixtures/strip-types-benchmark.ts'                        -0.58 %       ±1.50% ±1.99% ±2.59%
ts/transform-typescript.js n=1000 filepath='/Users/joyee/projects/node/benchmark/fixtures/transform-types-benchmark.js'                 1.54 %       ±2.41% ±3.20% ±4.17%
ts/transform-typescript.js n=1000 filepath='/Users/joyee/projects/node/benchmark/fixtures/transform-types-benchmark.ts'                 0.04 %       ±2.28% ±3.04% ±3.95%

Fixes: #54741

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. labels Jan 16, 2025
This integrates TypeScript into the compile cache by caching
the transpilation (either type-stripping or transforming) output
in addition to the V8 code cache that's generated from the
transpilation output.

Locally this speeds up loading with type stripping of
`benchmark/fixtures/strip-types-benchmark.ts` by ~65% and
loading with type transforms of
`fixtures/transform-types-benchmark.ts` by ~128%.

When comparing loading .ts and loading pre-transpiled .js on-disk
with the compile cache enabled, previously .ts loaded 46% slower
with type-stripping and 66% slower with transforms compared to
loading .js files directly.
After this patch, .ts loads 12% slower with type-stripping and
22% slower with transforms compared to .js.

(Note that the numbers are based on microbenchmark fixtures and
do not necessarily represent real-world workloads, though with
bigger real-world files, the speed up should be more significant).
@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Jan 16, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 16, 2025
@nodejs-github-bot
Copy link
Collaborator

Comment on lines +96 to +97
* @typedef {object} TypeScriptOptions
* @property {'transform'|'strip-only'} mode Mode.
Copy link
Member

Choose a reason for hiding this comment

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

Nit, so you can avoid repeating 'strip-only'|'transform' later

Suggested change
* @typedef {object} TypeScriptOptions
* @property {'transform'|'strip-only'} mode Mode.
* @typedef {'strip-only' | 'transform'} TypeScriptMode
*
* @typedef {object} TypeScriptOptions
* @property {TypeScriptMode} mode Mode.

@@ -108,6 +120,20 @@ function processTypeScriptCode(code, options) {
return transformedCode;
}

/**
* Get the type enum used for compile cache.
* @param {'strip-only'|'transform'} mode Mode of transpilation.
Copy link
Member

Choose a reason for hiding this comment

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

Per above.

Suggested change
* @param {'strip-only'|'transform'} mode Mode of transpilation.
* @param {TypeScriptMode} mode Mode of transpilation.

case CachedCodeType::kStrippedTypeScript:
return "StrippedTypeScript";
case CachedCodeType::kTransformedTypeScript:
return "TransformedTypeScript";
Copy link
Member

Choose a reason for hiding this comment

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

Does this flavor exist? I thought if we were transforming, we were adding source maps. Is there a way to transform without getting source maps?

Copy link

codecov bot commented Jan 16, 2025

Codecov Report

Attention: Patch coverage is 89.11565% with 16 lines in your changes missing coverage. Please review.

Project coverage is 89.21%. Comparing base (0e7ec5e) to head (0e02024).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/node_modules.cc 83.58% 3 Missing and 8 partials ⚠️
src/compile_cache.cc 87.50% 2 Missing and 2 partials ⚠️
lib/internal/modules/typescript.js 97.91% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main   #56629    +/-   ##
========================================
  Coverage   89.20%   89.21%            
========================================
  Files         662      662            
  Lines      191890   192027   +137     
  Branches    36928    36956    +28     
========================================
+ Hits       171176   171309   +133     
- Misses      13554    13555     +1     
- Partials     7160     7163     +3     
Files with missing lines Coverage Δ
src/compile_cache.h 100.00% <ø> (ø)
lib/internal/modules/typescript.js 99.48% <97.91%> (-0.52%) ⬇️
src/compile_cache.cc 81.10% <87.50%> (+3.29%) ⬆️
src/node_modules.cc 79.62% <83.58%> (+0.85%) ⬆️

... and 33 files with indirect coverage changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use the compilation cache when running typescript files through --experimental-transform-types
3 participants