Defer dynamic export of type members until first access #192
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously, when dynamically invoking .NET APIs from JS (as in the
semantic-kernel
example) loading of types within an assembly/namespace was deferred, but whenever a type was loaded, marshalling code was generated and compiled for all members of the type, and all types referenced by any of the members, cascading on to the referenced types' members and so on. This meant for a nontrivial application thousands of types and members could be processed at startup time, the vast majority of which were never actually used by the app.This change defers the marshalling code generation/compilation for members (constructors, properties, and methods) of loaded types, until the first time each individual member is accessed. And then, there is no need to load referenced types until that time either.
This change has two major benefits:
Summary of changes:
TypeExporter
when exporting properties, initially define the property with getter/setter callbacks that load the actual property getter/setter and then redefine the property before calling it.JSCallbackOverload
to support deferred loading of overloaded constructor/method callbacks, using aLazy<T>
to invoke the loader once (which generates and compiles the marshalling code).TypeExporter
, useJSCallbackOverload
to defer loading of constructors and methods.Related diagnostic improvements:
TRACE
andDEBUG
environment variables to use a consistentNODE_API_
prefix, and updateREADME-dev.md
.NODE_API_DELAYLOAD
variable, enabled by default. (I'm not sure it's needed, but it's possible it could cause some unforeseen problems.)