Skip to content
Permalink
Browse files

lib: lazy load `v8` in error-serdes

Lazy loading `v8` in `lib/internal/error-serdes.js` reduces the number
of loaded modules by the bootstrap code for Worker threads by seven.

PR-URL: #26689
Refs: #26501 (comment)
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information...
richardlau authored and targos committed Mar 15, 2019
1 parent 1f4a5bc commit cc4a25a1a99cea46decfcc69fd069291d47c3d80
Showing with 4 additions and 8 deletions.
  1. +4 −1 lib/internal/error-serdes.js
  2. +0 −7 test/parallel/test-bootstrap-modules.js
@@ -1,7 +1,6 @@
'use strict';

const Buffer = require('buffer').Buffer;
const { serialize, deserialize } = require('v8');
const {
SafeSet,
Object,
@@ -85,7 +84,9 @@ function inspect(...args) {
return internalUtilInspect.inspect(...args);
}

let serialize;
function serializeError(error) {
if (!serialize) serialize = require('v8').serialize;
try {
if (typeof error === 'object' &&
ObjectPrototypeToString(error) === '[object Error]') {
@@ -110,7 +111,9 @@ function serializeError(error) {
Buffer.from(inspect(error), 'utf8')]);
}

let deserialize;
function deserializeError(error) {
if (!deserialize) deserialize = require('v8').deserialize;
switch (error[0]) {
case kSerializedError:
const { constructor, properties } = deserialize(error.subarray(1));
@@ -71,13 +71,8 @@ if (common.isMainThread) {
expectedModules.add('NativeModule internal/process/main_thread_only');
expectedModules.add('NativeModule internal/process/stdio');
} else {
expectedModules.add('Internal Binding heap_utils');
expectedModules.add('Internal Binding messaging');
expectedModules.add('Internal Binding serdes');
expectedModules.add('Internal Binding stream_wrap');
expectedModules.add('Internal Binding symbols');
expectedModules.add('Internal Binding uv');
expectedModules.add('Internal Binding v8');
expectedModules.add('Internal Binding worker');
expectedModules.add('NativeModule _stream_duplex');
expectedModules.add('NativeModule _stream_passthrough');
@@ -86,7 +81,6 @@ if (common.isMainThread) {
expectedModules.add('NativeModule _stream_writable');
expectedModules.add('NativeModule internal/error-serdes');
expectedModules.add('NativeModule internal/process/worker_thread_only');
expectedModules.add('NativeModule internal/stream_base_commons');
expectedModules.add('NativeModule internal/streams/buffer_list');
expectedModules.add('NativeModule internal/streams/destroy');
expectedModules.add('NativeModule internal/streams/end-of-stream');
@@ -97,7 +91,6 @@ if (common.isMainThread) {
expectedModules.add('NativeModule internal/worker/io');
expectedModules.add('NativeModule module');
expectedModules.add('NativeModule stream');
expectedModules.add('NativeModule v8');
expectedModules.add('NativeModule worker_threads');
}

0 comments on commit cc4a25a

Please sign in to comment.
You can’t perform that action at this time.