com-lihaoyi / scalatags

ScalaTags is a small XML/HTML construction library for Scala.
https://com-lihaoyi.github.io/scalatags/
MIT License
758 stars 117 forks source link

Add a fast, mutable version? #155

Closed lihaoyi closed 5 years ago

lihaoyi commented 7 years ago

Currently, you can re-use the same Tag or Frag objects, add more attributes and children, and re-render them multiple times. This means Tags and Frags are all immutable and nice and principled and stuff.

In practice, this doesn't happen that often, and doesn't really work with the JsDom backend anyway: with JsDom you can insert mutable dom.Element references into your Tags and Frags, and re-rendering the same Frag multiple times may misbehave since a dom.Element can only have a single position in the DOM.

As a result most of my Tags and Frags are one-shot values: rendered once and that's it.

We should be able to take advantage of the one-shot-ness: by performing the rendering immediately upon construction, we can save the cost of building a large, immutable Frag-tree just to throw it away. There may be some additional limitations (e.g. all for the Text backend Attrs may have to be applied before any child Frags, and all Cls would have to be applied consecutively) but overall it should be possible to make this work.

This should be able to give us a decent speedup in both JsDom and Text backends, on both Scala-JVM and Scala.js, which would then let me care even less about the performance of my Scalatags rendering code. Right now, I still have to be careful about e.g. rendering large templates during on-scroll handlers, but if it rendering was 2-3x faster I could maybe get by without caring at all.

sometao commented 7 years ago

good

fommil commented 7 years ago

Interning may help with memory overhead if nothing else. I have a project in the works that will make it trivial.