-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vm: introduce
cachedData
/produceCachedData
Introduce `cachedData`/`produceCachedData` options for `v8.Script`. Could be used to consume/produce V8's code cache for speeding up compilation of known code. PR-URL: #4777 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
- Loading branch information
Showing
4 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const vm = require('vm'); | ||
const Buffer = require('buffer').Buffer; | ||
|
||
const originalSource = '(function bcd() { return \'original\'; })'; | ||
|
||
// It should produce code cache | ||
const original = new vm.Script(originalSource, { | ||
produceCachedData: true | ||
}); | ||
assert(original.cachedData instanceof Buffer); | ||
|
||
assert.equal(original.runInThisContext()(), 'original'); | ||
|
||
// It should consume code cache | ||
const success = new vm.Script(originalSource, { | ||
cachedData: original.cachedData | ||
}); | ||
assert(!success.cachedDataRejected); | ||
|
||
assert.equal(success.runInThisContext()(), 'original'); | ||
|
||
// It should reject invalid code cache | ||
const reject = new vm.Script('(function abc() { return \'invalid\'; })', { | ||
cachedData: original.cachedData | ||
}); | ||
assert(reject.cachedDataRejected); | ||
assert.equal(reject.runInThisContext()(), 'invalid'); | ||
|
||
// It should throw on non-Buffer cachedData | ||
assert.throws(() => { | ||
new vm.Script('function abc() {}', { | ||
cachedData: 'ohai' | ||
}); | ||
}); |
96934cb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this patch. I found strange thing. I load cached data from file and reuse it when node process restart. The problem is that the cache data which size is under 8K is always rejected with sanity check error 'MAGIC_NUMBER_MISMATCH'. cached_data_buf from GetCachedData is wrong completly. Thanks.
96934cb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't reproduce what you're saying. Are you sure you're not mangling the data when writing it to disk, e.g., by passing an encoding to
fs.writeFile()
? If it's not that, can you open an issue and include a minimal test case?