diff --git a/.evergreen.yml b/.evergreen.yml index 4f13b43..a0edaa6 100644 --- a/.evergreen.yml +++ b/.evergreen.yml @@ -14,7 +14,7 @@ functions: set -e set -x - export NODE_VERSION=20.5.0 + export NODE_VERSION=20.13.0 bash .evergreen/install-node.sh install: - command: shell.exec @@ -106,7 +106,7 @@ tasks: - func: install - func: test vars: - node_version: "20.5.0" + node_version: "20.13.0" - name: check commands: - func: checkout diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index bc4e2ce..293c59b 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -13,7 +13,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest] - node-version: [14.x, 16.x, 18.x, 19.x] + node-version: [14.x, 16.x, 18.x, 20.x] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 diff --git a/resources/main-template.cc b/resources/main-template.cc index 6aa297a..c63b118 100644 --- a/resources/main-template.cc +++ b/resources/main-template.cc @@ -18,6 +18,7 @@ #include #endif #include // injected code may refer to std::underlying_type +#include using namespace node; using namespace v8; @@ -34,6 +35,12 @@ using namespace v8; #define NODE_VERSION_SUPPORTS_EMBEDDER_SNAPSHOT 1 #endif +// 20.13.0 has https://github.com/nodejs/node/pull/52595 for better startup snapshot +// initialization performance. +#if NODE_VERSION_AT_LEAST(20, 13, 0) +#define NODE_VERSION_SUPPORTS_STRING_VIEW_SNAPSHOT 1 +#endif + // Snapshot config is supported since https://github.com/nodejs/node/pull/50453 #if NODE_VERSION_AT_LEAST(20, 12, 0) && !defined(BOXEDNODE_SNAPSHOT_CONFIG_FLAGS) #define BOXEDNODE_SNAPSHOT_CONFIG_FLAGS (SnapshotFlags::kWithoutCodeCache) @@ -81,6 +88,9 @@ void MarkTime(const char* category, const char* label) { Local GetBoxednodeMainScriptSource(Isolate* isolate); Local GetBoxednodeCodeCacheBuffer(Isolate* isolate); std::vector GetBoxednodeSnapshotBlobVector(); +#ifdef NODE_VERSION_SUPPORTS_STRING_VIEW_SNAPSHOT +std::optional GetBoxednodeSnapshotBlobSV(); +#endif void GetTimingData(const FunctionCallbackInfo& info) { Isolate* isolate = info.GetIsolate(); @@ -230,11 +240,18 @@ static int RunNodeInstance(MultiIsolatePlatform* platform, ArrayBufferAllocator::Create(); #ifdef BOXEDNODE_CONSUME_SNAPSHOT - std::vector snapshot_blob_vec = boxednode::GetBoxednodeSnapshotBlobVector(); - boxednode::MarkTime("Node.js Instance", "Decoded snapshot"); assert(EmbedderSnapshotData::CanUseCustomSnapshotPerIsolate()); - node::EmbedderSnapshotData::Pointer snapshot_blob = - EmbedderSnapshotData::FromBlob(snapshot_blob_vec); + node::EmbedderSnapshotData::Pointer snapshot_blob; +#ifdef NODE_VERSION_SUPPORTS_STRING_VIEW_SNAPSHOT + if (const auto snapshot_blob_sv = boxednode::GetBoxednodeSnapshotBlobSV()) { + snapshot_blob = EmbedderSnapshotData::FromBlob(snapshot_blob_sv.value()); + } +#endif + if (!snapshot_blob) { + std::vector snapshot_blob_vec = boxednode::GetBoxednodeSnapshotBlobVector(); + boxednode::MarkTime("Node.js Instance", "Decoded snapshot"); + snapshot_blob = EmbedderSnapshotData::FromBlob(snapshot_blob_vec); + } boxednode::MarkTime("Node.js Instance", "Read snapshot"); Isolate* isolate = NewIsolate(allocator, loop, platform, snapshot_blob.get()); #elif NODE_VERSION_AT_LEAST(14, 0, 0) diff --git a/src/helpers.ts b/src/helpers.ts index cee4fc5..2b14801 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -113,6 +113,17 @@ export async function createUncompressedBlobDefinition (fnName: string, source: ${Uint8Array.prototype.toString.call(source) || '0'} }; +#ifdef NODE_VERSION_SUPPORTS_STRING_VIEW_SNAPSHOT + std::optional ${fnName}SV() { + return { + { + reinterpret_cast(&${fnName}_source_[0]), + ${source.length} + } + }; + } +#endif + std::vector ${fnName}Vector() { return std::vector( reinterpret_cast(&${fnName}_source_[0]), @@ -155,6 +166,12 @@ export async function createCompressedBlobDefinition (fnName: string, source: Ui return dst;`} } +#ifdef NODE_VERSION_SUPPORTS_STRING_VIEW_SNAPSHOT + std::optional ${fnName}SV() { + return {}; + } +#endif + ${blobTypedArrayAccessors(fnName, source.length)} `; } diff --git a/test/index.ts b/test/index.ts index 723db64..f0fafbf 100644 --- a/test/index.ts +++ b/test/index.ts @@ -218,7 +218,7 @@ describe('basic functionality', () => { it(`works with snapshot support (compressBlobs = ${compressBlobs})`, async function () { this.timeout(2 * 60 * 60 * 1000); // 2 hours await compileJSFileAsBinary({ - nodeVersionRange: '^21.6.2', + nodeVersionRange: '^20.13.0', sourceFile: path.resolve(__dirname, 'resources/snapshot-echo-args.js'), targetFile: path.resolve(__dirname, `resources/snapshot-echo-args${exeSuffix}`), useNodeSnapshot: true,