Elm core libs in WebAssembly

The document about GC is really nice!

It reminds me of a neat idea that I have heard in various places.

Double-buffer the View

The view function has pretty interesting allocation behavior, and it may be possible to “double buffer” to avoid ever scanning, copying, or moving any Virtual DOM values.

Say there are two memory regions. You could have a situation like this:

  1. Calling view allocates into region 1. Do the first render.
  2. Calling view allocates into region 2. Do a diff and update DOM.

At this point, none of the virtual DOM values in region 1 are alive. You can just move the allocation pointer back to the beginning of region 1 and start writing over it!

  1. Calling view allocates into region 1. Do a diff and update DOM.
  2. Calling view allocates into region 2. Do a diff and update DOM.
  3. etc.

I think this is safe as long as the allocation regions are only used for values of type Html and Attribute. The presence of lazy means you may have values you want to keep around, so any closures from code like onInput (Changed "thing") would need to be in the normal heap and the mark phase would have to go through the two view regions.

My profiling always suggests that rendering is the expensive part, so this seems really promising to me. One neat side benefit is that you end up having fewer collections for normal heap. It should only contain values from update and subscriptions which should be a lot less.

I think the root idea here is from Mozilla people and we discussed it with Richard and Robin a long time ago. Anyway, perhaps it is interesting to you!

8 Likes