preact-render-to-string icon indicating copy to clipboard operation
preact-render-to-string copied to clipboard

[experiment] ropes perf & dedicated SSR codepath

Open developit opened this issue 5 years ago • 0 comments

Update: boo, I botched the benchmark and this is actually slower than master.

~I've been sitting on this one for a little bit, and finally got the time to hack it out.~

Here's the changes:

No longer reuses renderToString() as an internal method for recursion. This un-leaks the current private arguments, and enforces monomorphic recursive calls.

Use a "WIP String" reference. This probably looks weird at first, but it's a JS engine trick. V8 has 11 different String representations, and one of the most highly optimized is called a "rope" (other engines have similar representations). Ropes are "virtual" Strings that are made up of tiny single string pieces - all of those p.s += 'x' bits. They work a lot like a Buffer, which means they're extremely fast to build up.

In addition to appending String segments to a rope being cheap, there's also a performance benefit to dropping the return value of our recursive function. Instead of the engine having to constantly copy strings returned from renderToString(), there is only a single shared reference to the same string. This works really well in our current renderer because we always render depth-first and "top to bottom", so we're always able to append to the end of the WIP string.

Dropping pretty and other options These are obviously super useful options and we want to have them around - they power a bunch of Preact addons. However, they're almost exclusively used by preact-render-to-string/jsx, and that's already shipped as its own entry module. I'm proposing we turn import "preact-render-to-string" into a single-purpose "render my app to HTML", and then use preact-render-to-string/advanced or something along those lines as a path forward for all of the configurability bits. Or we could just leave it called /jsx and the name will just be slightly wrong from some use-cases.

developit avatar Feb 16 '20 03:02 developit