mozilla / pluotsorbet

[ARCHIVED] PluotSorbet is a J2ME-compatible virtual machine written in JavaScript.
GNU General Public License v2.0
238 stars 46 forks source link

Snapshot Java Heap #1447

Open mbebenita opened 9 years ago

mbebenita commented 9 years ago

If we could save the state of a J2ME application, we could save quite a bit of time during startup by skipping all the initialization code.

I envision this working as follows:

Saving:

  1. Save the names of all currently loaded classes.
  2. From a root set of runtimes / loaded classes traverse the Java object graph and save it to a custom binary format. I don't know what's popular these days, but Adobe's AMF3 format would do the job here.

Restoring:

  1. Load list of class names and load classes (without triggering any static initializers).
  2. Restore object graph by deserializing binary format.
  3. Link up restored objects to their klass objects.
  4. Deal with all the nitty gritty details.

In this scheme, we would only serialize the object graph, not the entire state of the VM. This should be a great start.

marco-c commented 9 years ago

This is a subset of #1413.

We should identify a good point to snapshot. The best from a performance perspective would be after the BG MIDlet has finished its initialization, but I'm not sure we can actually do it (because the startup flow of the BG MIDlet could be different each time).

In my benchmark in https://github.com/mozilla/j2me.js/pull/1434#issuecomment-94961084, we spend ~3 seconds initializing platform code (right before the startApp method of the BG MIDlet is called), ~3 seconds before the FG MIDlet is started by the BG MIDlet and ~13 seconds after that (which probably includes time spent both in the BG MIDlet and in the FG MIDlet).

When restoring, we would also need to restore any data that is created/changed by JS natives (for example localized strings, state of the mutexes and so on).