Skip to content

Commit

Permalink
lib: lazy load v8 in error-serdes
Browse files Browse the repository at this point in the history
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: nodejs#26689
Refs: nodejs#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 danbev committed Mar 19, 2019
1 parent e08ac04 commit 927f29d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lib/internal/error-serdes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const Buffer = require('buffer').Buffer;
const { serialize, deserialize } = require('v8');
const {
SafeSet,
Object,
Expand Down Expand Up @@ -84,7 +83,9 @@ function lazyUtil() {
return util;
}

let serialize;
function serializeError(error) {
if (!serialize) serialize = require('v8').serialize;
try {
if (typeof error === 'object' &&
ObjectPrototypeToString(error) === '[object Error]') {
Expand All @@ -109,7 +110,9 @@ function serializeError(error) {
Buffer.from(lazyUtil().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));
Expand Down
7 changes: 0 additions & 7 deletions test/parallel/test-bootstrap-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
}

Expand Down

0 comments on commit 927f29d

Please sign in to comment.