Skip to content

Commit 4e320c8

Browse files
committed
fix: fixed size optimizations on website
1 parent 946e326 commit 4e320c8

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

Cargo.toml

+4-8
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ members = [
1313
]
1414
resolver = "2"
1515

16-
# [patch.crates-io]
17-
# sycamore = { git = "https://github.com/arctic-hen7/sycamore" }
18-
# sycamore-router = { git = "https://github.com/arctic-hen7/sycamore" }
19-
# sycamore-router-macro = { git = "https://github.com/arctic-hen7/sycamore" }
20-
# sycamore-macro = { git = "https://github.com/arctic-hen7/sycamore" }
21-
# sycamore-core = { git = "https://github.com/arctic-hen7/sycamore" }
22-
# sycamore-reactive = { git = "https://github.com/arctic-hen7/sycamore" }
23-
# sycamore-web = { git = "https://github.com/arctic-hen7/sycamore" }
16+
[profile.release]
17+
lto = true
18+
opt-level = "s"
19+
codegen-units = 1

docs/next/en-US/reference/deploying.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,29 @@ With JavaScript, you can 'chunk' your app into many different files that are loa
1414

1515
If you're getting into real strife with your bundle sizes though, you can, theoretically, split out your app into multiple components by literally building different parts of your website as different apps. This should be an absolute last resort though, and we have never come across an app that was big enough to need this. (Remember that Perseus will still give your users a page very quickly, it's just the interactivity that might take a little longer --- as in a few milliseconds longer.)
1616

17-
However, there are some easy things you can do to make your Wasm bundles much smaller. TODO
17+
However, there are some easy things you can do to make your Wasm bundles much smaller. The first are applied in `Cargo.toml` to the release profile, which allows you to tweak compilation settings in release-mode (used in `perseus deploy`). (Note: if your app is inside a workspace, this has to go at the root of the workspace.)
18+
19+
```toml
20+
[profile.release]
21+
lto = true
22+
opt-level = "z"
23+
codegen-units = 1
24+
```
25+
26+
The first of these lets LLVM inline and prune functions more aggressively, leading to `perseus deploy` taking longer, but producing a faster and smaller app. The second is Cargo's optimization level, which is usually set to 3 for release builds, optimizing aggressively for speed. However, on the web, we get better 'speed' out of smaller sizes as explained above, so we optimize aggressively for size (note that sometimes optimizing normally for size with `s` can actually be better, so you should try both). The third of these is another one that makes compilation take (much) longer with `perseus deploy`, but that decrease speed by letting LLVM basically do more work.
27+
28+
If you're only ever going to export your app, this is fine, but, if you ever use a server, then this will be a problem, as these size-focused optimizations will apply to your server too, slowing everything down again! Unfortunately, Cargo doesn't yet support [target-specific profiles](https://github.com/rust-lang/cargo/issues/4897), so we need to hack our way around this. TODO
29+
30+
The next thing you can do is switch to `wee_alloc`, an alternative allocator designed for the web that produces less efficient, but smaller bundles. Again though, that lower efficiency is barely noticeable, while every kilobyte you can shave off the bundle's size leads to a notably faster load speed. Importantly, you still want to retain that efficiency on the server, so it's very important to only use `wee_alloc` on the browser-side, which you can do by adding the following to the very top of your `lib.rs`:
31+
32+
```rust
33+
#[cfg(target_arch = "wasm32")]
34+
#[global_allocator]
35+
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
36+
```
37+
38+
To make this work, you should also add the following to your `Cargo.toml` under the `[target.'cfg(target_arch = "wasm32")'.dependencies]` section (for browser-only dependencies):
39+
40+
```toml
41+
wee_alloc = "0.4"
42+
```

website/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ walkdir = "2"
2828
pulldown-cmark = "0.8"
2929

3030
[target.'cfg(target_arch = "wasm32")'.dependencies]
31+
wee_alloc = "0.4"
3132

3233
[lib]
3334
name = "lib"

website/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#[cfg(target_arch = "wasm32")]
2+
#[global_allocator]
3+
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
4+
15
mod components;
26
mod error_pages;
37
mod templates;

0 commit comments

Comments
 (0)