5 ms·
How does Moon automatically detect static virtual nodes? What makes Moon faster than the alternatives? Finally, how does Moon avoid GC pressure with large trees
by pspeter3 9y ago
How does Moon automatically detect static virtual nodes? What makes Moon faster than the alternatives? Finally, how does Moon avoid GC pressure with large trees?
- nickthemagicman 9y agoif(virtual_node.type == static){do stuff;} Super easy.
- pspeter3 9y agoSo developers mark it manually?
- kbr 9y agoNo, that comment was a joke. (A really bad one). Moon's compiler will do all of the work.
- nickthemagicman 9y agoIt was a good joke.
- kbr 9y agoThe compiler treats everything as static by default. When the compiler detects something dynamic (such as a {{mustache}} template), it will mark the current node as dynamic, and the parent node as well. These propagate up the tree so Moon eventually has a render function that is extremely optimized and can skip everything static, and the diff will only hit the nodes that can change. Moon also has a stricter syntax for virtual DOM, allowing for less checks to be made at runtime compared to alternatives like HyperScript. This is made possible because Moon's compiler itself is in charge of creating the render functions, allowing it to give certain hints to the virtual DOM diffing engine. There is really no way to avoid GC when you have a virtual DOM. The virtual DOM trees are pretty light and have memory usage similar to that of React and Inferno.
- oever 9y ago> There is really no way to avoid GC when you have a virtual DOM. If you keep the virtual DOM in a typed array, there is not need for relying on the JavaScript garbage collection. Here's an example: https://github.com/vandenoever/baredom/blob/master/src/baredom/impl/Dom.js https://github.com/vandenoever/baredom/blob/master/src/bared...
- pspeter3 9y agoThanks, I'll have to check this out!
- kbr 9y agoThis is really cool! I'll definitely look into this and apply some of it to Moon.
- oever 9y agoBareDOM is a proof of concept, the code is not very clean, but I'm sure you can get the ideas. Since Moon has a specific way of using the DOM, you might be able to cut down on the number of array positions per node (currently 8).
- pspeter3 9y agoIs there any documentation on why to use a typed array
- yorwba 9y agoTyped arrays are essentially tightly packed chunks of memory that the GC won't walk. If you know what you're doing, you can use them to both save memory (shaving off the per-object overhead of JS objects) and reduce the time spent on GC collections (but you have to do your own memory management on the typed array). That the typed array contains only objects of a single data type probably also helps the JIT to optimize.
- 9y ago