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: #26689
Refs: #26501 (comment)
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
richardlau authored and targos committed Mar 27, 2019
1 parent 1f4a5bc commit cc4a25a
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 @@ -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]') {
Expand All @@ -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));
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 cc4a25a

Please sign in to comment.