Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[experiment] ropes perf & dedicated SSR codepath #135

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

developit
Copy link
Member

@developit developit commented Feb 16, 2020

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 developit changed the title ~40% faster SSR, and ~40% smaller size [experiment] ropes/stringbuilder performance & dedicated SSR codepath Feb 16, 2020
@developit developit changed the title [experiment] ropes/stringbuilder performance & dedicated SSR codepath [experiment] ropes perf & dedicated SSR codepath Feb 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant