You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When serializing any non-primitive value, it is looked up in the globals Map, to check if it's a global property or property of a NodeJS built-in module e.g. Object.is or require('path').join.
Functions are then checked against the specialFunctions Map to check if it's a bound function, or other special functions e.g. a require function.
The original rationale for not adding these "special functions" into globals is that specialFunctions is a WeakMap, and so does not prevent them being garbage collected.
However, it's wasteful to have 2 Map lookups for every function which is serialized.
Avoid this by either:
Making globals a WeakMap. Then add "special functions" into globals.
Add globals which are functions into specialFunctions instead of globals. So then a function being serialized only needs to be looked up once in the specialFunctions Map.
I don't think making globals a WeakMap would change much. All globals are strongly held anyway, and I think NodeJS's built-in modules are too.
Only exceptions I can see are:
undefined which is currently stored as a global, but can't be a WeakMap key.
Global Symbols, which I can't be WeakMap keys in NodeJS v18.
Global well-known Symbols (Symbol.for('...')) which can't be WeakMap keys in any version of NodeJS. But these shouldn't be added to globals anyway, so they're serialized as e.g. Symbol.for('nodejs.util.promisify.custom') instead of require('util').promisify.custom.
When serializing any non-primitive value, it is looked up in the
globals
Map, to check if it's a global property or property of a NodeJS built-in module e.g.Object.is
orrequire('path').join
.Functions are then checked against the
specialFunctions
Map to check if it's a bound function, or other special functions e.g. arequire
function.The original rationale for not adding these "special functions" into
globals
is thatspecialFunctions
is a WeakMap, and so does not prevent them being garbage collected.However, it's wasteful to have 2 Map lookups for every function which is serialized.
Avoid this by either:
globals
a WeakMap. Then add "special functions" intoglobals
.specialFunctions
instead ofglobals
. So then a function being serialized only needs to be looked up once in thespecialFunctions
Map.I don't think making
globals
a WeakMap would change much. All globals are strongly held anyway, and I think NodeJS's built-in modules are too.Only exceptions I can see are:
undefined
which is currently stored as a global, but can't be a WeakMap key.Symbol.for('...')
) which can't be WeakMap keys in any version of NodeJS. But these shouldn't be added to globals anyway, so they're serialized as e.g.Symbol.for('nodejs.util.promisify.custom')
instead ofrequire('util').promisify.custom
.livepack/lib/init/globals.js
Lines 123 to 133 in 13a778b
The text was updated successfully, but these errors were encountered: