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

Add WASI support for server-side rendering. #3534

Open
wants to merge 68 commits into
base: master
Choose a base branch
from

Conversation

langyo
Copy link

@langyo langyo commented Nov 21, 2023

Description

I've been trying to use yew to render the page into the static HTML string on WASI. However, yew cannot distinguish the browser WASM target (wasm32-unknown-unknown with wasm-bindgen) and WASI target (wasm32-wasi), and it would choose wrong modules for wasm32-*.

In addition, since the current SSR implementation will create new tasks directly in the asynchronous context directly (based on prokio). It only allowed in a multi-threaded environment that it is not compatible with WASI. So I added a dedicated one for a single-threaded environment that rendering function to support single-threaded scenes.

To support this new feature, I have also made changes along with some other upstream dependencies. I've created some PRs for these upstream dependencies, and added a temporary patch entry to the root Cargo.toml for this PR. If the upstream branch can be processed, these temporary patch entries could be replaced.

I also wrote a short example for this new feature.

CC @futursolo 😉

Checklist

  • I have reviewed my own code
  • I have added tests

Copy link

github-actions bot commented Nov 21, 2023

Benchmark - core

Yew Master

vnode           fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ vnode_clone  2.547 ns      │ 2.645 ns      │ 2.573 ns      │ 2.577 ns      │ 100     │ 1000000000

Pull Request

vnode           fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ vnode_clone  3.035 ns      │ 3.371 ns      │ 3.066 ns      │ 3.07 ns       │ 100     │ 1000000000

Copy link

github-actions bot commented Nov 21, 2023

Visit the preview URL for this PR (updated for commit d42077a):

https://yew-rs--pr3534-wasi-support-test-o6il3hlo.web.app

(expires Thu, 23 May 2024 04:59:59 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Copy link

github-actions bot commented Nov 21, 2023

Benchmark - SSR

Yew Master

Benchmark Round Min (ms) Max (ms) Mean (ms) Standard Deviation
Baseline 10 308.839 309.934 309.091 0.309
Hello World 10 485.566 498.596 490.388 5.267
Function Router 10 1645.431 1667.069 1651.687 6.381
Concurrent Task 10 1005.296 1007.192 1006.150 0.526
Many Providers 10 1128.123 1175.336 1143.905 14.769

Pull Request

Benchmark Round Min (ms) Max (ms) Mean (ms) Standard Deviation
Baseline 10 308.774 310.309 309.155 0.455
Hello World 10 483.807 493.356 487.666 3.850
Function Router 10 1599.899 1619.124 1608.241 5.165
Concurrent Task 10 1005.784 1006.647 1006.159 0.248
Many Providers 10 1095.527 1192.815 1118.700 28.378

Copy link

github-actions bot commented Nov 21, 2023

Size Comparison

examples master (KB) pull request (KB) diff (KB) diff (%)
async_clock 97.653 97.623 -0.030 -0.031%
boids 167.853 167.810 -0.043 -0.026%
communication_child_to_parent 90.206 90.171 -0.035 -0.039%
communication_grandchild_with_grandparent 102.341 102.300 -0.041 -0.040%
communication_grandparent_to_grandchild 97.768 97.740 -0.027 -0.028%
communication_parent_to_child 86.565 86.521 -0.045 -0.052%
contexts 102.036 101.996 -0.040 -0.039%
counter 83.196 83.158 -0.038 -0.046%
counter_functional 83.631 83.594 -0.037 -0.044%
dyn_create_destroy_apps 86.564 86.531 -0.033 -0.038%
file_upload 97.627 97.589 -0.038 -0.039%
function_memory_game 163.858 163.824 -0.034 -0.021%
function_router 338.506 338.338 -0.168 -0.050%
function_todomvc 157.824 157.794 -0.030 -0.019%
futures 225.526 225.229 -0.297 -0.132%
game_of_life 105.197 105.159 -0.038 -0.036%
immutable 186.146 186.118 -0.028 -0.015%
inner_html 77.545 77.510 -0.035 -0.045%
js_callback 105.750 105.706 -0.044 -0.042%
keyed_list 193.396 193.360 -0.035 -0.018%
mount_point 80.534 80.497 -0.037 -0.046%
nested_list 111.160 111.125 -0.035 -0.032%
node_refs 87.884 87.843 -0.041 -0.047%
password_strength 1698.212 1698.175 -0.037 -0.002%
portals 90.809 90.775 -0.033 -0.037%
router 309.432 309.254 -0.178 -0.057%
simple_ssr 137.716 137.686 -0.030 -0.022%
ssr_router 375.773 375.559 -0.215 -0.057%
suspense 112.070 112.044 -0.026 -0.024%
timer 86.123 86.085 -0.038 -0.044%
timer_functional 94.877 94.832 -0.045 -0.047%
todomvc 138.982 138.946 -0.036 -0.026%
two_apps 83.251 83.215 -0.036 -0.043%
web_worker_fib 131.454 131.425 -0.029 -0.022%
web_worker_prime 181.591 181.551 -0.040 -0.022%
webgl 80.238 80.200 -0.038 -0.047%

✅ None of the examples has changed their size significantly.

@langyo
Copy link
Author

langyo commented Dec 15, 2023

I am commenting separately because the original comment cannot be quoted directly.

Ref #3534 (comment)

There's still a problem. The WASI environment cannot directly establish a server like other environments. Because this part has not been standardized yet. In other words, I can only print the SSR results directly to the stdout stream for the time being.

Originally I wanted to remove this demo, but I discovered this problem when checking other demos.

If it weren't for this reason, this change would be really elegant. 😉

@langyo langyo requested a review from ranile December 15, 2023 06:12
@lemmih
Copy link

lemmih commented Dec 15, 2023

I haven't reviewed this PR, but I can confirm that it works in my use-case. Landing this WASI support would be amazing.

@langyo
Copy link
Author

langyo commented Mar 28, 2024

This merge request has almost completed all scheduled work, but no one has continued to review it. Do you have time to help take a look?

@WorldSEnder @futursolo @hamza1311

Copy link
Member

@ranile ranile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the bump

examples/wasi_ssr_module/README.md Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought preview2 added support for it, no? I don't mind forcing WASI consumers to use wasm32-wasip2 target

Yew supports single thread mode for server-side rendering by `yew::LocalServerRenderer`. This mode would work in a single thread environment like WASI.

```rust
// Build it by `wasm32-wasi` target
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preview 2, perhaps? It's nightly only but maybe it's better to use that. I'm totally fine with making WASI examples to be nightly only in yew (no need to make the framework nightly only for it; the consumption is much better in p2)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preview 2, perhaps? It's nightly only but maybe it's better to use that. I'm totally fine with making WASI examples to be nightly only in yew (no need to make the framework nightly only for it; the consumption is much better in p2)

WASI's multi-threading proposal is still not stable even in preview2. If it were not for compatibility with the multi-threaded version of WASI, at least currently requiring users to use nightly channels to build WASI would do more harm than good.

Of course, I still support the new proposal of using WASI as soon as possible. When WASI's multi-threading capabilities stabilize at some point in the future, I'll improve the code.

Comment on lines 6 to 10
- ".github/workflows/main-checks.yml"
- "ci/**"
- "packages/**/*"
- "Cargo.toml"
- "Cargo.lock"
- '.github/workflows/main-checks.yml'
- 'ci/**'
- 'packages/**/*'
- 'Cargo.toml'
- 'Cargo.lock'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs to be resolved. Lots of unnecessary changes here

@langyo langyo requested a review from ranile March 28, 2024 17:36
@langyo
Copy link
Author

langyo commented Apr 10, 2024

Do you have time to take a look?😂 @hamza1311

@langyo langyo force-pushed the wasi-support-test branch 2 times, most recently from 9e24e93 to 71951bd Compare May 16, 2024 02:17
@langyo
Copy link
Author

langyo commented May 16, 2024

cc rustwasm/wasm-bindgen#3946

Until this fix of wasm-bindgen is released with a new version, CI wouldn't be fixed for a while.

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.

None yet

4 participants