Skip to content

Commit

Permalink
Added V8_COMPILE_CACHE_CACHE_DIR option
Browse files Browse the repository at this point in the history
Closes #23
  • Loading branch information
stelund authored and zertosh committed Oct 28, 2020
1 parent 071f71a commit 96a5d13
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ The ability to tap into V8 to produce/consume this cache was introduced in [Node

Set the environment variable `DISABLE_V8_COMPILE_CACHE=1` to disable the cache.

Cache directory is defined by environment variable `V8_COMPILE_CACHE_CACHE_DIR` or defaults to `<os.tmpdir()>/v8-compile-cache-<V8_VERSION>`.

## Internals

The caches are stored in `$TMP/v8-compile-cache/V8_VERSION`, where there are `.BLOB` and `.MAP` files corresponding to the entry module that required `v8-compile-cache`. The cache is _entry module specific_ because it is faster to load the entire code cache into memory at once, than it is to read it from disk on a file-by-file basis.
Cache files are suffixed `.BLOB` and `.MAP` corresponding to the entry module that required `v8-compile-cache`. The cache is _entry module specific_ because it is faster to load the entire code cache into memory at once, than it is to read it from disk on a file-by-file basis.

## Benchmarks

Expand Down
6 changes: 4 additions & 2 deletions bench/run.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/bin/bash

# for i in {1..5}; do node bench/require-yarn.js; done
set -eo pipefail

# rm -rf "$TMPDIR/v8-compile-cache"
V8_COMPILE_CACHE_CACHE_DIR=$(mktemp -d)
export V8_COMPILE_CACHE_CACHE_DIR=$V8_COMPILE_CACHE_CACHE_DIR
trap 'rm -r "$V8_COMPILE_CACHE_CACHE_DIR"' EXIT

THIS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
: "${NODE_BIN:=node}"
Expand Down
23 changes: 23 additions & 0 deletions test/getCacheDir-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ tap.test('getCacheDir (chakracore)', t => {
process: {
getuid: process.getuid,
versions: {chakracore: '1.2.3'},
env: {},
},
path,
os,
Expand All @@ -49,6 +50,7 @@ tap.test('getCacheDir (unknown)', t => {
getuid: process.getuid,
version: '1.2.3',
versions: {},
env: {},
},
path,
os,
Expand All @@ -62,3 +64,24 @@ tap.test('getCacheDir (unknown)', t => {

t.done();
});

tap.test('getCacheDir (env)', t => {
const cacheDir = vm.runInNewContext(
'(' + getCacheDir.toString() + ')();',
{
process: {
getuid: process.getuid,
versions: {},
env: {
V8_COMPILE_CACHE_CACHE_DIR: 'from env',
},
},
path,
os,
}
);

t.equal(cacheDir, 'from env');

t.done();
});
5 changes: 5 additions & 0 deletions v8-compile-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ function supportsCachedData() {
}

function getCacheDir() {
const v8_compile_cache_cache_dir = process.env.V8_COMPILE_CACHE_CACHE_DIR;
if (v8_compile_cache_cache_dir) {
return v8_compile_cache_cache_dir;
}

// Avoid cache ownership issues on POSIX systems.
const dirname = typeof process.getuid === 'function'
? 'v8-compile-cache-' + process.getuid()
Expand Down

0 comments on commit 96a5d13

Please sign in to comment.