Uh oh!
There was an error while loading. Please reload this page.
lib: improve lazy loading - #14167
Conversation
The lazy assert should already be in place when calling any of the E functions.
refack
commented
Jul 11, 2017
I don't like it as is. It spreads the Object.defineProperty(this,'lasyErrors',{get(){constvalue=require('errors');Object.defineProperty(this,'lasyErrors',{value});returnvalue;}); |
Fishrock123
commented
Jul 11, 2017
Does this expense actually add up to anything significant in a realistic scenario? |
BridgeAR
commented
Jul 11, 2017
@refack I guess you mean something like the following? constlazy={};Object.defineProperty(lazy,'util',{configurable: true,get(){constvalue=require('util');Object.defineProperty(lazy,'util',{ value,enumerable: true});returnvalue;}});constutil=lazy.util;I know that spreading is not nice but if it's a function call or not is not much of a difference in that case. @Fishrock123 I'll run some benchmarks later on. I just checked the different lazy loading times them self and those are way faster. |
I agree that dynamically it's the same, the difference is in static analysis, if it's a function (or property) at the top it's easier to spot, visually or with tools. As for a property you can set it on the |
refack
commented
Jul 11, 2017
Actually constlazy={getutil(){constvalue=require('util');Object.defineProperty(this,'util',{ value,enumerable: true});returnvalue;},
...
} |
jasnell
commented
Jul 12, 2017
Overall I'm not a fan. I prefer to keep lazy loading only when absolutely necessary, mostly to avoid circular references caused by early loading. |
There was a problem hiding this comment.
Re #14167 (comment) comment this could be inlined.
refack
commented
Jul 12, 2017
Simpler which is definitely better 👍 |
Do not lazy load if not necessary and prevent function calls.
e62d447 to
a2e95aaCompareBridgeAR
commented
Jul 12, 2017
@refack there's still the property lookup and it is still faster to just check if the variable is defined or not. This was the way how lazy loading was used in other spots as well and with the simple |
Ack |
| var cluster; | ||
| var dns; | ||
| // Lazily loaded | ||
| var cluster = null; |
There was a problem hiding this comment.
This could probably be required up front as well but cluster is likely not required in many places that use net and therefore I kept it as is.
There was a problem hiding this comment.
So could you document that:
// `cluster` is only used by `listenInCluster` so for startup performance// reasons it's lazy loaded.TimothyGu
commented
Jul 13, 2017
LGTM. Aside from this PR, I'm not too big of a fan of using |
BridgeAR
commented
Jul 18, 2017
@refack I added the comment. I also moved a statement to prevent lazy loading under some conditions. |
refack
commented
Jul 19, 2017
The link to the CI: https://ci.nodejs.org/job/node-test-commit/11256/ ✔️ |
* internal/errors - assert should already be in place when calling any of the message generating functions. * No lazy load if not necessary. * Replace function calls with `if`s. PR-URL: nodejs#14167 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
refack
commented
Jul 19, 2017
Landed in b55ab01 |
addaleax
commented
Jul 24, 2017
This doesn’t land cleanly on 8.x; if you can, please follow the guide and raise a backport PR, if you don’t think it make sense let me know and we’ll add the |
jasnell
commented
Sep 20, 2017
Ping @BridgeAR .. re: backport. |
* internal/errors - assert should already be in place when calling any of the message generating functions. * No lazy load if not necessary. * Replace function calls with `if`s. PR-URL: nodejs#14167 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
* internal/errors - assert should already be in place when calling any of the message generating functions. * No lazy load if not necessary. * Replace function calls with `if`s. PR-URL: #14167 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
MylesBorins
commented
Oct 17, 2017
Should this be backported to |
BridgeAR
commented
Dec 11, 2017
I do not see the necessity to backport this. So I changed the labels accordingly. |
The function call plus the existence check is more expensive than inlining the lazy require.
A few lazyAssert calls were obsolete as the assert would have to be required before.
I also moved a few lazy requires in the specific code path as it would not be required at all in some cases.
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)
lib