From 9a1bc4ea7ecb15ecce643e56bb9015c790e391ea Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 30 Sep 2015 02:07:56 +0200 Subject: [PATCH 1/2] util: optimize promise introspection Use V8's builtin ObjectIsPromise() to check that the value is a promise before creating the promise mirror. Reduces garbage collector strain in the (common) non-promise case, which is beneficial when inspecting deep object graphs. PR-URL: https://github.com/nodejs/node/pull/3130 Reviewed-By: Evan Lucas Reviewed-By: Sakthipriyan Vairamani --- lib/util.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/util.js b/lib/util.js index c5d7bea7db352d..7b2bfd2b3c0d56 100644 --- a/lib/util.js +++ b/lib/util.js @@ -3,7 +3,9 @@ const uv = process.binding('uv'); const Buffer = require('buffer').Buffer; const internalUtil = require('internal/util'); + var Debug; +var ObjectIsPromise; const formatRegExp = /%[sdj%]/g; exports.format = function(f) { @@ -183,11 +185,21 @@ function getConstructorOf(obj) { } +function ensureDebugIsInitialized() { + if (Debug === undefined) { + const runInDebugContext = require('vm').runInDebugContext; + const result = runInDebugContext('[Debug, ObjectIsPromise]'); + Debug = result[0]; + ObjectIsPromise = result[1]; + } +} + + function inspectPromise(p) { - Debug = Debug || require('vm').runInDebugContext('Debug'); - var mirror = Debug.MakeMirror(p, true); - if (!mirror.isPromise()) + ensureDebugIsInitialized(); + if (!ObjectIsPromise(p)) return null; + const mirror = Debug.MakeMirror(p, true); return {status: mirror.status(), value: mirror.promiseValue().value_}; } From dbe48446b7981a60ac3e461056c7a888572b8e24 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 30 Sep 2015 02:13:31 +0200 Subject: [PATCH 2/2] deps: backport f782159 from v8's upstream Backport f78215962bf5de9d47c022e7baa3952d0bf6d17f from V8's upstream to speed up promise introspection. Original commit message: Remove obsolete try/catch from ObjectIsPromise(). Review URL: https://codereview.chromium.org/1367123003 Cr-Commit-Position: refs/heads/master@{#30966} PR-URL: https://github.com/nodejs/node/pull/3130 Reviewed-By: Evan Lucas Reviewed-By: Sakthipriyan Vairamani --- deps/v8/src/mirror-debugger.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/deps/v8/src/mirror-debugger.js b/deps/v8/src/mirror-debugger.js index dbdc68e68f88e2..d37776af386857 100644 --- a/deps/v8/src/mirror-debugger.js +++ b/deps/v8/src/mirror-debugger.js @@ -19,16 +19,9 @@ function ToggleMirrorCache(value) { } -// Wrapper to check whether an object is a Promise. The call may not work -// if promises are not enabled. -// TODO(yangguo): remove try-catch once promises are enabled by default. function ObjectIsPromise(value) { - try { - return IS_SPEC_OBJECT(value) && - !IS_UNDEFINED(%DebugGetProperty(value, builtins.$promiseStatus)); - } catch (e) { - return false; - } + return IS_SPEC_OBJECT(value) && + !IS_UNDEFINED(%DebugGetProperty(value, builtins.$promiseStatus)); }