Skip to content

Commit

Permalink
Code Size: Document and use additional nightly features (#2604)
Browse files Browse the repository at this point in the history
* code size: doc and use additional nightly features

* add ci paths to run condition

* undo unrelated edit to benchmark workflow

* Add links to nightly feature docs
  • Loading branch information
WorldSEnder committed Apr 13, 2022
1 parent 469cc34 commit b580bd4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 44 deletions.
65 changes: 21 additions & 44 deletions .github/workflows/size-cmp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
pull_request:
branches: [master]
paths:
- .github/workflows/size-cmp.yml
- ci/collect_sizes.py
- "packages/**"
- "examples/**"
- "Cargo.toml"
Expand All @@ -30,8 +32,9 @@ jobs:
- name: Setup toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
toolchain: nightly
target: wasm32-unknown-unknown
components: rust-src
override: true
profile: minimal

Expand All @@ -54,6 +57,11 @@ jobs:

- name: Write optimisation flags for master
run: |
cat >> .cargo/config << EOF
[unstable]
build-std = ["std", "panic_abort"]
build-std-features = ["panic_immediate_abort"]
EOF
cat >> Cargo.toml << EOF
[profile.release]
lto = true
Expand All @@ -65,6 +73,11 @@ jobs:

- name: Write optimisation flags for pull request
run: |
cat >> .cargo/config << EOF
[unstable]
build-std = ["std", "panic_abort"]
build-std-features = ["panic_immediate_abort"]
EOF
cat >> Cargo.toml << EOF
[profile.release]
lto = true
Expand All @@ -77,55 +90,19 @@ jobs:
- name: Build master examples
run: find examples/*/index.html | xargs -I '{}' trunk build --release '{}' || exit 0
working-directory: yew-master
env:
RUSTUP_TOOLCHAIN: nightly

- name: Build pull request examples
run: find examples/*/index.html | xargs -I '{}' trunk build --release '{}' || exit 0
working-directory: current-pr
env:
RUSTUP_TOOLCHAIN: nightly

- name: Collect size information
run: |
from typing import Dict, List, Optional
import glob
import os
import json
def find_example_sizes(parent_dir: str) -> Dict[str, int]:
example_sizes: Dict[str, int] = {}
for example_dir in os.listdir(f"{parent_dir}/examples"):
path = f"{parent_dir}/examples/{example_dir}"
if not os.path.isdir(path):
continue
matches = glob.glob(f"{parent_dir}/examples/{example_dir}/dist/index*.wasm")
if not matches:
continue
path = matches[0]
example_sizes[example_dir] = os.path.getsize(path)
return example_sizes
master_sizes = find_example_sizes("yew-master")
pr_sizes = find_example_sizes("current-pr")
example_names = sorted(set([*master_sizes.keys(), *pr_sizes.keys()]))
joined_sizes = [(i, [master_sizes.get(i), pr_sizes.get(i)]) for i in example_names]
size_cmp_info = {
"sizes": joined_sizes,
"issue_number": ${{ github.event.number }},
}
with open(".SIZE_CMP_INFO", "w+") as f:
f.write(json.dumps(size_cmp_info))
shell: python3 {0}
run: python3 current-pr/ci/collect_sizes.py
env:
ISSUE_NUMBER: ${{ github.event.number }}

- name: Upload Artifact
uses: actions/upload-artifact@v2
Expand Down
40 changes: 40 additions & 0 deletions ci/collect_sizes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typing import Dict, List, Optional

import glob
import os
import json

def find_example_sizes(parent_dir: str) -> Dict[str, int]:
example_sizes: Dict[str, int] = {}

for example_dir in os.listdir(f"{parent_dir}/examples"):
path = f"{parent_dir}/examples/{example_dir}"

if not os.path.isdir(path):
continue

matches = glob.glob(f"{parent_dir}/examples/{example_dir}/dist/index*.wasm")

if not matches:
continue

path = matches[0]

example_sizes[example_dir] = os.path.getsize(path)

return example_sizes

master_sizes = find_example_sizes("yew-master")
pr_sizes = find_example_sizes("current-pr")

example_names = sorted(set([*master_sizes.keys(), *pr_sizes.keys()]))

joined_sizes = [(i, [master_sizes.get(i), pr_sizes.get(i)]) for i in example_names]

size_cmp_info = {
"sizes": joined_sizes,
"issue_number": os.environ["ISSUE_NUMBER"],
}

with open(".SIZE_CMP_INFO", "w+") as f:
f.write(json.dumps(size_cmp_info))
19 changes: 19 additions & 0 deletions website/docs/advanced-topics/optimizations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ opt-level = 'z'
lto = true
```

### Nightly Cargo configuration

You can also gain additional benefits from experimental nightly features of rust and
cargo. To use the nightly toolchain with `trunk`, set the `RUSTUP_TOOLCHAIN="nightly"` environment
variable. Then, you can configure unstable rustc features in your `.cargo/config.toml`.
Refer to the doc of [unstable features], specifically the section about [`build-std`] and
[`build-std-features`], to understand the configuration.

```toml, title=".cargo/config.toml"
[unstable]
# Requires the rust-src component. `rustup +nightly component add rust-src`
build-std = ["std", "panic_abort"]
build-std-features = ["panic_immediate_abort"]
```

[unstable features]: https://doc.rust-lang.org/cargo/reference/unstable.html
[`build-std`]: https://doc.rust-lang.org/cargo/reference/unstable.html#build-std
[`build-std-features`]: https://doc.rust-lang.org/cargo/reference/unstable.html#build-std-features

### wasm-opt

Further more it is possible to optimize size of `wasm` code.
Expand Down

1 comment on commit b580bd4

@github-actions
Copy link

Choose a reason for hiding this comment

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

Yew master branch benchmarks (Lower is better)

Benchmark suite Current: b580bd4 Previous: 469cc34 Ratio
yew-struct-keyed 01_run1k 208.092 201.4805 1.03
yew-struct-keyed 02_replace1k 227.942 226.128 1.01
yew-struct-keyed 03_update10th1k_x16 413.986 385.3185 1.07
yew-struct-keyed 04_select1k 69.8415 67.48349999999999 1.03
yew-struct-keyed 05_swap1k 99.485 95.467 1.04
yew-struct-keyed 06_remove-one-1k 31.756 34.0055 0.93
yew-struct-keyed 07_create10k 3377.112 3280.632 1.03
yew-struct-keyed 08_create1k-after1k_x2 488.0175 484.118 1.01
yew-struct-keyed 09_clear1k_x8 216.5645 201.358 1.08
yew-struct-keyed 21_ready-memory 1.457233428955078 1.457233428955078 1
yew-struct-keyed 22_run-memory 1.6982192993164062 1.6634407043457031 1.02
yew-struct-keyed 23_update5-memory 1.697723388671875 1.6979408264160156 1.00
yew-struct-keyed 24_run5-memory 1.9442062377929688 1.7101287841796875 1.14
yew-struct-keyed 25_run-clear-memory 1.3271331787109375 1.329509735107422 1.00
yew-struct-keyed 31_startup-ci 1741.864 1883.25 0.92
yew-struct-keyed 32_startup-bt 46.09600000000001 34.955999999999996 1.32
yew-struct-keyed 33_startup-mainthreadcost 319.6440000000001 278.26800000000014 1.15
yew-struct-keyed 34_startup-totalbytes 328.744140625 328.744140625 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.