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: implement flushCompileCache() #54971

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Sep 16, 2024

module: write compile cache to temporary file and then rename it

This works better in terms of avoiding race conditions.

Drive-by: throw instead of abort when module.enableCompileCache()
gets passed a non-string argument.

module: implement flushCompileCache()

This implements an API for users to intentionally flush the
accumulated compile cache instead of waiting until process
shutdown. It may be useful for application that loads dependencies
first and then either reload itself in other instances, or spawning
other instances that load an overlapping set of its dependencies -
in this case its useful to flush the cache early instead of waiting
until the shutdown of itself.

Currently flushing is triggered by either process
shutdown or user requests. In the future we should simply start the
writes right after module loading on a separate thread, and this method
only blocks until all the pending writes (if any) on the other thread
are finished. In that case, the off-thread writes should finish long
before any attempt of flushing is made so the method would then only
incur a negligible overhead from thread synchronization.

Fixes: #54770
Fixes: #54465

@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++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Sep 16, 2024
src/compile_cache.cc Outdated Show resolved Hide resolved
src/compile_cache.cc Show resolved Hide resolved
src/env.cc Outdated Show resolved Hide resolved
@richardlau richardlau added the semver-minor PRs that contain new features and should be released in the next minor version. label Sep 16, 2024
doc/api/module.md Outdated Show resolved Hide resolved
Copy link

codecov bot commented Sep 16, 2024

Codecov Report

Attention: Patch coverage is 73.23944% with 19 lines in your changes missing coverage. Please review.

Project coverage is 88.27%. Comparing base (c1afd2c) to head (b141f8c).
Report is 41 commits behind head on main.

Files with missing lines Patch % Lines
src/compile_cache.cc 65.00% 10 Missing and 4 partials ⚠️
src/node_modules.cc 81.25% 2 Missing and 1 partial ⚠️
src/env.cc 77.77% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #54971      +/-   ##
==========================================
+ Coverage   88.06%   88.27%   +0.21%     
==========================================
  Files         652      652              
  Lines      183545   183610      +65     
  Branches    35866    35848      -18     
==========================================
+ Hits       161639   162082     +443     
+ Misses      15150    14802     -348     
+ Partials     6756     6726      -30     
Files with missing lines Coverage Δ
lib/internal/modules/helpers.js 98.83% <100.00%> (+0.78%) ⬆️
lib/module.js 100.00% <100.00%> (ø)
src/compile_cache.h 100.00% <100.00%> (ø)
src/env.h 98.00% <ø> (ø)
src/env.cc 85.61% <77.77%> (+0.01%) ⬆️
src/node_modules.cc 78.10% <81.25%> (+0.09%) ⬆️
src/compile_cache.cc 77.54% <65.00%> (-2.46%) ⬇️

... and 66 files with indirect coverage changes

@RedYetiDev RedYetiDev added the module Issues and PRs related to the module subsystem. label Sep 16, 2024
This works better in terms of avoiding race conditions.
This implements an API for users to intentionally flush the
accumulated compile cache instead of waiting until process
shutdown. It may be useful for application that loads dependencies
first and then either reload itself in other instances, or spawning
other instances that load an overlapping set of its dependencies -
in this case its useful to flush the cache early instead of waiting
until the shutdown of itself.

Currently flushing is triggered by either process
shutdown or user requests. In the future we should simply start the
writes right after module loading on a separate thread, and this method
only blocks until all the pending writes (if any) on the other thread
are finished. In that case, the off-thread writes should finish long
before any attempt of flushing is made so the method would then only
incur a negligible overhead from thread synchronization.
@joyeecheung
Copy link
Member Author

joyeecheung commented Sep 17, 2024

Updated to remove the keepDeserializedCache option - we can add it back if someone actually has a use case for it. For now it doesn't seem to be actually useful to me - either you purge the module cache frequently then you probably already expect the code to change frequently and should opt out of the compile cache in the first place, or you don't purge the module cache then there's no point keeping the other tier of serialized code cache around. Might as well just remove it to keep things simple.

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Sep 17, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 17, 2024
@nodejs-github-bot
Copy link
Collaborator

@joyeecheung
Copy link
Member Author

@anonrig @nodejs/loaders @nodejs/cpp-reviewers can I get some reviews please?

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Sep 19, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 19, 2024
@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
c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
5 participants