Starlight-JS / starlight

JS engine in Rust
https://teletype.in/@starlight-js
Mozilla Public License 2.0
513 stars 9 forks source link

Support ssr for web #27

Open sessionboy opened 3 years ago

sessionboy commented 3 years ago

I have been looking forward to the emergence of rust-ssr( js + react + rust + ssr-render ), hoping starlight can help me achieve this.

playXE commented 3 years ago

Hello! I'm not familiar with react and ssr. Can you explain what is this and how Starlight could help there? Note that Starlight right now only has support for ES5.1 and subset of ES6+ standards.

sessionboy commented 3 years ago

Hi guy! React is a Facebook open source web framework. The full name of ssr is server-side rendering. It is a technology for rendering web code (html+css+js) in the back-end environment. It is currently only implemented by nodejs because the nodejs is based on v8. Consistent with js syntax.

Rust cannot execute js code, unless a js engine like v8 is developed.

We can implement ssr with the help of rust's js engine.

Compared with nodejs, rust can bring better performance, security, multi-threading, high concurrency and other advantages, making the web to a higher level.

V8 is a complex and huge project, we cannot use it directly, so we need lightweight js engines such as starlight and quickjs to implement ssr in rust.

If you want to know the specific implementation, you can check this project: fast-vue-ssr, it is not perfect at present, because it is based on the quickjs engine, this is a poor performance js engine.

Note that Starlight right now only has support for ES5.1 and subset of ES6+ standards

Great, maybe I can try some experiments. How does starlight compare to v8? I hope it is light enough and comparable in performance to v8.

playXE commented 3 years ago

@sessionboy oh nice! React seems like a great framework.

How does starlight compare to v8? I hope it is light enough and comparable in performance to v8.

It is not as fast as JSC or V8 or even SpiderMonkey but it is the fastest JS engine out there that is written in Rust. I can say for sure that Starlight is faster than JITless SpiderMonkey and sometimes faster than JITless V8 (e.g when TCO is used, V8 does not have tail calls while Starlight does). It might be slower in apps that have large GC heaps and might need to perform GC often. As for now I do not have incremental or concurrent collectors but they will be implemented in the future for sure. (There's only MiGC and MallocGC options available right now). There's inline caching and structures (aka hidden classes) implemented so Starlight is pretty fast and memory efficent when handling JS objects.

Compared with nodejs, rust can bring better performance, security, multi-threading, high concurrency and other advantages, making the web to a higher level.

Problem is that when I started developing Starlight I sacrificed some safety rules for performance. For example GC shadow stack which is zero-cost way to track GC roots. Internally it is a lot of unsafe but outisde API uses Rust borrow checker to ensure that roots do not exit their scope and all this stuff.

V8 is a complex and huge project, we cannot use it directly, so we need lightweight js engines such as starlight and quickjs to implement ssr in rust.

I can't say that Starlight is that lightweight, but its binary seems to be smaller than Boa or V8. When stripped it is the same size as JavaScriptCore. For additional lightweightness I implemented startup snapshots so creating runtime instance is fast. With runtime snapshots you can freeze your heap into binary blob and then deserialize heap from it (Not interpreter state!).

There's also some problems with concurrency right now. There's no synchronization in runtime atm and it is fully single-threaded (except symbol table which is shared between threads and VM instances). In the future I'll add trap based safepoints for synchronizing multiple threads for GC or other workloads.

Another note about performance. I slowly develop baseline JIT on my local host using MIR and c2mir as backends. :) It is very W.I.P and does not work but when its done it could improve performance by ~2x (it will use some of information collected by the interpreter).

devsnek commented 3 years ago

Have you looked at this? https://docs.rs/rusty_v8/0.22.1/rusty_v8/index.html

sessionboy commented 3 years ago

@playXE Thank you for your answer, let me know a lot. Indeed, the current starlight can’t satisfy my idea, I look forward to the further growth of starlight.

@devsnek I know it, but usually I think v8 is complicated and huge, so I didn't try to use it. Maybe I should do some experiments, thank you for your reminder.