Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Lazy objects v0 for raw objects #1129

Closed
wants to merge 1 commit into from
Closed

Conversation

jeffreytan81
Copy link
Contributor

@jeffreytan81 jeffreytan81 commented Nov 2, 2017

Release Note: introduce lazy objects feature for raw objects in Prepack.
Introduce lazy objects feature for raw objects in Prepack with supported runtime.
Lazy object is a collaboration feature between the Prepack and new runtime that supports this feature. With this feature, object can be created with a special LazyObjectsRuntime.createLazyObject() API. The object created with this API can be a light-weight placeholder object. Once the lazy object is touched/used the first time, runtime is responsible to callback the registered initialization function to hydrate the object into a real object.
By doing this we will save both memory and object creation speed.

Currently, Prepack only supports lazy for raw objects.

The APIs that runtime needs to support:

  1. LazyObjectsRuntime.createLazyObject(<lazy_object_id>);
  2. LazyObjectsRuntime.setLazyObjectInitializer().

Under lazy objects mode, Prepack will serialize all lazy objects into two parts:

  1. All lazy objects are created via lightweight LazyObjectsRuntime.createLazyObject() call.
  2. Lazy objects' property assignments are delayed in a callback function which is registered with the runtime.

TODO:

  1. Test-runner support for lazy mode.
  2. Support more object types for this feature
  3. Remove "reference count === 1" objects' root and directly serialize into callback function.
  4. Figure out "reference count > 1" objects that are safe to remove root from global anonymous function and directly serialize into callback function.

Test Plan:

  1. "bin/prepack.js <foo.js> --lazyObjectsRuntime LazyObjectRuntime"
  2. test-runner introduced a lazy mode(later PR) can successfully run all tests.

@@ -101,6 +105,9 @@ export function getSerializerOptions({
trace,
};
if (additionalFunctions) result.additionalFunctions = additionalFunctions;
if (lazyObjectsRuntime != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer !== undefined

@@ -1202,7 +1212,6 @@ export class ResidualHeapSerializer {
}

_serializeAbstractValueHelper(val: AbstractValue): BabelNodeExpression {
invariant(val.kind !== "sentinel member expression", "invariant established by visitor");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this invariant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a redundant invariant from previous checkin.
In "_serializeAbstractValue()" function, there is already same invariant.

// Holds object's lazy initializer bodies.
// These bodies will be combined into a well-known callback after generator serialization is done and registered with the runtime.
_lazyObjectInitializers: Map<ObjectValue, SerializedBody>;
_currentSerializeLazyObject: ?ObjectValue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void | ObjectValue. No need to allow two kinds of undefinedness.

Copy link
Contributor

@NTillmann NTillmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach with the subclass is quite beautiful! And for a v0 it looks very solid :-)

Long term, I see an issue with the subclass approach when it comes to combining features, such as delayed initialization and lazy objects. Switching to the getTarget later might require re-architecting the modularity approach.

In any case, great work for the v0!

Copy link

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yinghuitan is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@hermanventer hermanventer deleted the LazyObjectsV0ForReview branch November 13, 2017 21:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants