[experiment] ropes perf & dedicated SSR codepath #135
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 optionsThese 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 turnimport "preact-render-to-string"
into a single-purpose "render my app to HTML", and then usepreact-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.