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 3 commits 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, as it's not exactly linear - it just skips transpilation altogether if it's already cached, so the more complex the file is, the more will be saved).

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
@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

lib/internal/modules/typescript.js Outdated Show resolved Hide resolved
lib/internal/modules/typescript.js Outdated Show resolved Hide resolved
src/compile_cache.cc Show resolved Hide resolved
Copy link

codecov bot commented Jan 16, 2025

Codecov Report

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

Project coverage is 89.21%. Comparing base (9ec7bed) to head (6bc4178).
Report is 4 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 98.30% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main   #56629    +/-   ##
========================================
  Coverage   89.20%   89.21%            
========================================
  Files         662      662            
  Lines      191934   192079   +145     
  Branches    36944    36970    +26     
========================================
+ Hits       171218   171360   +142     
+ Misses      13552    13549     -3     
- Partials     7164     7170     +6     
Files with missing lines Coverage Δ
src/compile_cache.h 100.00% <ø> (ø)
lib/internal/modules/typescript.js 97.74% <98.30%> (+0.14%) ⬆️
src/compile_cache.cc 81.10% <87.50%> (+3.29%) ⬆️
src/node_modules.cc 79.62% <83.58%> (+0.85%) ⬆️

... and 37 files with indirect coverage changes

Copy link
Member

@marco-ippolito marco-ippolito left a comment

Choose a reason for hiding this comment

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

LGTM, just a comment

lib/internal/modules/typescript.js Show resolved Hide resolved
Comment on lines +503 to +512
Isolate* isolate = args.GetIsolate();
CHECK(args[0]->IsString()); // TODO(joyeecheung): accept buffer.
CHECK(args[1]->IsString());
CHECK(args[2]->IsUint32());
Local<Context> context = isolate->GetCurrentContext();
Environment* env = Environment::GetCurrent(context);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Isolate* isolate = args.GetIsolate();
CHECK(args[0]->IsString()); // TODO(joyeecheung): accept buffer.
CHECK(args[1]->IsString());
CHECK(args[2]->IsUint32());
Local<Context> context = isolate->GetCurrentContext();
Environment* env = Environment::GetCurrent(context);
Environment* env = Environment::GetCurrent(args);
CHECK(args[0]->IsString()); // TODO(joyeecheung): accept buffer.
CHECK(args[1]->IsString());
CHECK(args[2]->IsUint32());

Then access env->isolate() and env->context() ... I know we're not super consistent on this but would be nice to work towards more consistency

Copy link
Member Author

@joyeecheung joyeecheung Jan 20, 2025

Choose a reason for hiding this comment

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

If we are going to have more consistency, we should move away from using env->context() as that makes the binding unusable for e.g. shadow realms, and prefer to do what's done here (i.e. use the current context, which makes it usable for shadow realms).

Copy link
Member

Choose a reason for hiding this comment

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

Are these types of issues documented somewhere? Style guide updated? Shadow realm related concerns documented?

Copy link
Member Author

@joyeecheung joyeecheung Jan 22, 2025

Choose a reason for hiding this comment

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

I don't think "the right way to get context for situation X" is specifically documented, though it should be inferrable from the notes added in #47932 - if you want the binding to be reused in a different realm, don't use env->context() when you mean "current context". There are still legit reasons to use env->context() (when you actually mean the main context), but it's not here.

Comment on lines +558 to +564
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Environment* env = Environment::GetCurrent(context);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Environment* env = Environment::GetCurrent(context);
Environment* env = Environment::GetCurrent(args);

Then use env->isolate() before...

Copy link
Member Author

Choose a reason for hiding this comment

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

Similar to before, we should refrain from using env->isolate() and env->context() at least for new code..

src/node_modules.cc Outdated Show resolved Hide resolved
@@ -34,6 +41,7 @@ struct CompileCacheEntry {
// Copy the cache into a new store for V8 to consume. Caller takes
// ownership.
v8::ScriptCompiler::CachedData* CopyCache() const;
const char* type_name() const;
Copy link
Member

Choose a reason for hiding this comment

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

nit: std::string_view type_name() const; ?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is returning string literals for debug logs, doesn't that just adds an extra overhead for all of them?

@marco-ippolito marco-ippolito added request-ci Add this label to start a Jenkins CI on a PR. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. labels Jan 21, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 21, 2025
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

src/node_modules.cc Outdated Show resolved Hide resolved
@jasnell
Copy link
Member

jasnell commented Jan 22, 2025

Tests appear to be failing due to the PR needing to be rebased.

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 22, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 22, 2025
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. 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
5 participants