Skip to content

Commit 3535d13

Browse files
committed
precompile the v8 compile cache when building
1 parent 9e4af12 commit 3535d13

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,8 @@ RUN find /nodejs/node_modules -name "*.cjs.map" -delete
6161
RUN find /nodejs/node_modules -name "*.ts.map" -delete
6262
RUN find /nodejs/node_modules -name "*.md" -delete
6363

64+
# Warm up compile cache
65+
RUN node -e "require('/nodejs/node_modules/datadog-lambda-js/runtime/module_importer').initTracer()"
66+
6467
FROM scratch
6568
COPY --from=builder /nodejs /

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"hot-shots": "8.5.0",
4444
"promise-retry": "^2.0.1",
4545
"serialize-error": "^8.1.0",
46-
"shimmer": "1.2.1"
46+
"shimmer": "1.2.1",
47+
"v8-compile-cache": "^2.4.0"
4748
},
4849
"jest": {
4950
"verbose": true,

src/runtime/module_importer.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11

22
const { logDebug, updateDDTags } = require("../utils");
33

4+
function compileCache () {
5+
const { FileSystemBlobStore, NativeCompileCache } = require('v8-compile-cache').__TEST__
6+
7+
const cacheDir = __dirname
8+
const prefix = 'module_importer'
9+
const blobStore = new FileSystemBlobStore(cacheDir, prefix)
10+
11+
const nativeCompileCache = new NativeCompileCache()
12+
nativeCompileCache.setCacheStore(blobStore)
13+
nativeCompileCache.install()
14+
15+
process.once('exit', () => {
16+
if (blobStore.isDirty()) {
17+
blobStore.save()
18+
}
19+
nativeCompileCache.uninstall()
20+
})
21+
}
22+
423
// Currently no way to prevent typescript from auto-transpiling import into require,
524
// so we expose a wrapper in js
625
exports.import = function (path) {
726
return import(path);
827
}
928

1029
exports.initTracer = function () {
30+
compileCache()
31+
1132
// Looks for the function local version of dd-trace first, before using
1233
// the version provided by the layer
1334
const path = require.resolve("dd-trace", { paths: ["/var/task/node_modules", ...module.paths] });

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4075,6 +4075,11 @@ uuid@^9.0.1:
40754075
resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.1.tgz#e188d4c8853cc722220392c424cd637f32293f30"
40764076
integrity sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==
40774077

4078+
v8-compile-cache@^2.4.0:
4079+
version "2.4.0"
4080+
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.4.0.tgz#cdada8bec61e15865f05d097c5f4fd30e94dc128"
4081+
integrity sha512-ocyWc3bAHBB/guyqJQVI5o4BZkPhznPYUG2ea80Gond/BgNWpap8TOmLSeeQG7bnh2KMISxskdADG59j7zruhw==
4082+
40784083
v8-to-istanbul@^8.1.0:
40794084
version "8.1.1"
40804085
resolved "https://registry.yarnpkg.com/v8-to-istanbul/-/v8-to-istanbul-8.1.1.tgz#77b752fd3975e31bbcef938f85e9bd1c7a8d60ed"

0 commit comments

Comments
 (0)