From 3508783084de27f3a93cac77dba545d7b29052a9 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 4 Sep 2024 20:18:12 +0200 Subject: [PATCH] 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. PR-URL: https://github.com/nodejs/node/pull/54971 Fixes: https://github.com/nodejs/node/issues/54770 Fixes: https://github.com/nodejs/node/issues/54465 Reviewed-By: Yagiz Nizipli Reviewed-By: Matteo Collina --- doc/api/module.md | 23 +++++++++ lib/internal/modules/helpers.js | 2 + lib/module.js | 3 ++ src/compile_cache.cc | 7 +++ src/env.cc | 14 +++--- src/node_modules.cc | 21 +++++++++ test/fixtures/compile-cache-flush.js | 21 +++++++++ test/parallel/test-compile-cache-api-flush.js | 47 +++++++++++++++++++ 8 files changed, 130 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/compile-cache-flush.js create mode 100644 test/parallel/test-compile-cache-api-flush.js diff --git a/doc/api/module.md b/doc/api/module.md index 3bb25c3f313da1..665a64c0505b66 100644 --- a/doc/api/module.md +++ b/doc/api/module.md @@ -199,6 +199,13 @@ Compilation cache generated by one version of Node.js can not be reused by a dif version of Node.js. Cache generated by different versions of Node.js will be stored separately if the same base directory is used to persist the cache, so they can co-exist. +At the moment, when the compile cache is enabled and a module is loaded afresh, the +code cache is generated from the compiled code immediately, but will only be written +to disk when the Node.js instance is about to exit. This is subject to change. The +[`module.flushCompileCache()`][] method can be used to ensure the accumulated code cache +is flushed to disk in case the application wants to spawn other Node.js instances +and let them share the cache long before the parent exits. + ### `module.getCompileCacheDir()` + +> Stability: 1.1 - Active Development + +Flush the [module compile cache][] accumulated from modules already loaded +in the current Node.js instance to disk. This returns after all the flushing +file system operations come to an end, no matter they succeed or not. If there +are any errors, this will fail silently, since compile cache misses should not +interfer with the actual operation of the application. + ### Class: `module.SourceMap`