GeezerVison is showing github.com — build: create V8 code cache after script is run · nodejs/node@62024b6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/cache.js +38-11Lines changed: 38 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,23 +9,50 @@ const { | 9 | 9 | NativeModule, internalBinding | 10 | 10 | } = require('internal/bootstrap/loaders'); | 11 | 11 |
| | 12 | +function getCodeCache(id) { | | 13 | + const cached = NativeModule.getCached(id); | | 14 | + if (cached && (cached.loaded || cached.loading)) { | | 15 | + return cached.script.createCachedData(); | | 16 | + } | | 17 | + | | 18 | + // The script has not been compiled and run | | 19 | + NativeModule.require(id); | | 20 | + return getCodeCache(id); | | 21 | +} | | 22 | + | | 23 | +const depsModule = Object.keys(NativeModule._source).filter( | | 24 | + (key) => NativeModule.isDepsModule(key) || key.startsWith('internal/deps') | | 25 | +); | | 26 | + | | 27 | +// Modules with source code compiled in js2c that | | 28 | +// cannot be compiled with the code cache | | 29 | +const cannotUseCache = [ | | 30 | + 'config', | | 31 | + 'sys', // deprecated | | 32 | + 'internal/v8_prof_polyfill', | | 33 | + 'internal/v8_prof_processor', | | 34 | + | | 35 | + 'internal/per_context', | | 36 | + | | 37 | + 'internal/test/binding', | | 38 | + // TODO(joyeecheung): update the C++ side so that | | 39 | + // the code cache is also used when compiling these | | 40 | + // two files. | | 41 | + 'internal/bootstrap/loaders', | | 42 | + 'internal/bootstrap/node' | | 43 | +].concat(depsModule); | | 44 | + | 12 | 45 | module.exports = { | | 46 | + cachableBuiltins: Object.keys(NativeModule._source).filter( | | 47 | + (key) => !cannotUseCache.includes(key) | | 48 | + ), | 13 | 49 | builtinSource: Object.assign({}, NativeModule._source), | | 50 | + getCodeCache, | 14 | 51 | codeCache: internalBinding('code_cache'), | 15 | 52 | compiledWithoutCache: NativeModule.compiledWithoutCache, | 16 | 53 | compiledWithCache: NativeModule.compiledWithCache, | 17 | 54 | nativeModuleWrap(script) { | 18 | 55 | return NativeModule.wrap(script); | 19 | 56 | }, | 20 | | - // Modules with source code compiled in js2c that | 21 | | - // cannot be compiled with the code cache | 22 | | - cannotUseCache: [ | 23 | | - 'config', | 24 | | - // TODO(joyeecheung): update the C++ side so that | 25 | | - // the code cache is also used when compiling these | 26 | | - // two files. | 27 | | - 'internal/bootstrap/loaders', | 28 | | - 'internal/bootstrap/node', | 29 | | - 'internal/per_context', | 30 | | - ] | | 57 | + cannotUseCache | 31 | 58 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/loaders.js +8-2Lines changed: 8 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 118 | 118 | this.exportKeys = undefined; | 119 | 119 | this.loaded = false; | 120 | 120 | this.loading = false; | | 121 | + this.script = null; // The ContextifyScript of the module | 121 | 122 | } | 122 | 123 |
| 123 | 124 | NativeModule._source = getBinding('natives'); | | 165 | 166 | return nativeModule.exports; | 166 | 167 | }; | 167 | 168 |
| | 169 | + NativeModule.isDepsModule = function(id) { | | 170 | + return id.startsWith('node-inspect/') || id.startsWith('v8/'); | | 171 | + }; | | 172 | + | 168 | 173 | NativeModule.requireForDeps = function(id) { | 169 | 174 | if (!NativeModule.exists(id) || | 170 | 175 | // TODO(TimothyGu): remove when DEP0084 reaches end of life. | 171 | | - id.startsWith('node-inspect/') || | 172 | | - id.startsWith('v8/')) { | | 176 | + NativeModule.isDepsModule(id)) { | 173 | 177 | id = `internal/deps/${id}`; | 174 | 178 | } | 175 | 179 | return NativeModule.require(id); | | 241 | 245 | codeCache[this.id], false, undefined | 242 | 246 | ); | 243 | 247 |
| | 248 | + this.script = script; | | 249 | + | 244 | 250 | // One of these conditions may be false when any of the inputs | 245 | 251 | // of the `node_js2c` target in node.gyp is modified. | 246 | 252 | // FIXME(joyeecheung): | |
Collapse file Copy file name to clipboardExpand all lines: test/code-cache/test-code-cache.js +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,9 +12,8 @@ const { | 12 | 12 | } | 13 | 13 | } = require('util'); | 14 | 14 | const { | 15 | | - builtinSource, | | 15 | + cachableBuiltins, | 16 | 16 | codeCache, | 17 | | - cannotUseCache, | 18 | 17 | compiledWithCache, | 19 | 18 | compiledWithoutCache | 20 | 19 | } = require('internal/bootstrap/cache'); | @@ -35,8 +34,7 @@ for (const key of loadedModules) { | 35 | 34 | `"${key}" should've been compiled with code cache`); | 36 | 35 | } | 37 | 36 |
| 38 | | -for (const key of Object.keys(builtinSource)) { | 39 | | - if (cannotUseCache.includes(key)) continue; | | 37 | +for (const key of cachableBuiltins) { | 40 | 38 | assert(isUint8Array(codeCache[key]) && codeCache[key].length > 0, | 41 | 39 | `Code cache for "${key}" should've been generated`); | 42 | 40 | } |
Collapse file Copy file name to clipboardExpand all lines: tools/generate_code_cache.js +7-18Lines changed: 7 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | // of `configure`. | 9 | 9 |
| 10 | 10 | const { | 11 | | - nativeModuleWrap, | 12 | | - builtinSource, | 13 | | - cannotUseCache | | 11 | + getCodeCache, | | 12 | + cachableBuiltins | 14 | 13 | } = require('internal/bootstrap/cache'); | 15 | 14 |
| 16 | | -const vm = require('vm'); | 17 | 15 | const fs = require('fs'); | 18 | 16 |
| 19 | 17 | const resultPath = process.argv[2]; | @@ -72,25 +70,16 @@ const cacheInitializers = []; | 72 | 70 | let totalCacheSize = 0; | 73 | 71 |
| 74 | 72 |
| 75 | | -for (const key of Object.keys(builtinSource)) { | 76 | | - if (cannotUseCache.includes(key)) continue; | 77 | | - const code = nativeModuleWrap(builtinSource[key]); | 78 | | - | 79 | | - // Note that this must corresponds to the code in | 80 | | - // NativeModule.prototype.compile | 81 | | - const script = new vm.Script(code, { | 82 | | - filename: `${key}.js`, | 83 | | - produceCachedData: true | 84 | | - }); | 85 | | - | 86 | | - if (!script.cachedData) { | | 73 | +for (const key of cachableBuiltins) { | | 74 | + const cachedData = getCodeCache(key); | | 75 | + if (!cachedData.length) { | 87 | 76 | console.error(`Failed to generate code cache for '${key}'`); | 88 | 77 | process.exit(1); | 89 | 78 | } | 90 | 79 |
| 91 | | - const length = script.cachedData.length; | | 80 | + const length = cachedData.length; | 92 | 81 | totalCacheSize += length; | 93 | | - const { definition, initializer } = getInitalizer(key, script.cachedData); | | 82 | + const { definition, initializer } = getInitalizer(key, cachedData); | 94 | 83 | cacheDefinitions.push(definition); | 95 | 84 | cacheInitializers.push(initializer); | 96 | 85 | console.log(`Generated cache for '${key}', size = ${formatSize(length)}` + | |
|