# Elm core libs in WebAssembly

**URL:** <https://discourse.elm-lang.org/t/elm-core-libs-in-webassembly/4443>\
**Category:** Show and Tell\
**Created:** [October 7, 2019, 7:30pm UTC](https://discourse.elm-lang.org/t/elm-core-libs-in-webassembly/4443 "2019-10-07T19:30:31Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![evancz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/evancz/32/16_2.png) [@evancz](https://discourse.elm-lang.org/u/evancz)\
**Post date:** [October 8, 2019, 6:22pm UTC](https://discourse.elm-lang.org/t/elm-core-libs-in-webassembly/4443/7 "2019-10-08T18:22:38Z")

</div>

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!

---

_[View the full topic](https://discourse.elm-lang.org/t/elm-core-libs-in-webassembly/4443)._
