The VersionedDOM enables to have multiple versions of the DOM nodes of a document at the same time in the memory. With VersionedDOM, the WEB content could modify DOM tree while rendering the older versions of DOM tree in the background at the same time, or concurrently. It means to solve the problem of running bother layout engine, renderer and JS code of WEB content, all in the main thread. With VersionedDOM, the layout engine and the renderer could be off-the-main-thread, so we could leverage multi-cores better in high-end devices.
The key idea of VersionedDOM is versioned pointer, a pointer that is verion awared. That means it is dereferenced according the version of current, it is modified with a version. Modifying in a new version don't affect the old ones. That enable the layout engine and renderer engine working on an older version while the WEB content creating a new version based on the older one.
To make the implementation more easier, versioned pointers assume there is one thread at most to modify the DOM three and create a new version at any moment, other threads can read the older versions at the same time. So, the web content/main thread is the modifier in our case, and layout and renderer threads are readers.
The versioned pointers are implemented as C++ templates to override operators of assignment, casting, ... etc. With the help of the template classes of versioned pointers, the modifiers would be changed minor.
The scope of the code of readers is far smaller than the modifier. So, it is more easy to change the way of accessing pointers at readers side. The readers are only the layout engine and the renderer.
- versioned pointer
- Hold pointers for objects
- const verioned pointer
- For read-only
- read-write versioned pointer
- For modifying the object, as a trigger to clone the object for the current version.
Copy On Write
DOM objects are copy-on-write; a.k.a. COW, for modification of attributes other than versioned pointers. A new version is start by a shadow of the previous version, an object are copied/cloned from the previous version at first time of being modified for the current version. Then all changes are applied on the new instance. With COW, the reader and modifier are not interactive, to make VersionedDOM simple and easy.
Every versioned pointer is associated with the version manager of the document. The version manager controls versions. It tells the modifier what the current version is. The modifier don't need to know what the current version is, the version pointers would get the version number from its version manager directly. But, for the readers, a version number, should not be the current version, should be given to retrieve the value of a versioned pointer.
The version readers are created with a given version number. It provides the facility of reading the values of versioned pointers with the specified version.
COW means memory copy. It may cause the problem of massive changes on the DOM tree. But, fortunately, for most cases, WEB content changes little.