Code Size: Document and use additional nightly features (#2604)

* 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
This commit is contained in:
WorldSEnder 2022-04-13 10:30:33 +02:00 committed by GitHub
parent 469cc341c3
commit b580bd4c2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 44 deletions

View File

@ -5,6 +5,8 @@ on:
pull_request: pull_request:
branches: [master] branches: [master]
paths: paths:
- .github/workflows/size-cmp.yml
- ci/collect_sizes.py
- "packages/**" - "packages/**"
- "examples/**" - "examples/**"
- "Cargo.toml" - "Cargo.toml"
@ -30,8 +32,9 @@ jobs:
- name: Setup toolchain - name: Setup toolchain
uses: actions-rs/toolchain@v1 uses: actions-rs/toolchain@v1
with: with:
toolchain: stable toolchain: nightly
target: wasm32-unknown-unknown target: wasm32-unknown-unknown
components: rust-src
override: true override: true
profile: minimal profile: minimal
@ -54,6 +57,11 @@ jobs:
- name: Write optimisation flags for master - name: Write optimisation flags for master
run: | run: |
cat >> .cargo/config << EOF
[unstable]
build-std = ["std", "panic_abort"]
build-std-features = ["panic_immediate_abort"]
EOF
cat >> Cargo.toml << EOF cat >> Cargo.toml << EOF
[profile.release] [profile.release]
lto = true lto = true
@ -65,6 +73,11 @@ jobs:
- name: Write optimisation flags for pull request - name: Write optimisation flags for pull request
run: | run: |
cat >> .cargo/config << EOF
[unstable]
build-std = ["std", "panic_abort"]
build-std-features = ["panic_immediate_abort"]
EOF
cat >> Cargo.toml << EOF cat >> Cargo.toml << EOF
[profile.release] [profile.release]
lto = true lto = true
@ -77,55 +90,19 @@ jobs:
- name: Build master examples - name: Build master examples
run: find examples/*/index.html | xargs -I '{}' trunk build --release '{}' || exit 0 run: find examples/*/index.html | xargs -I '{}' trunk build --release '{}' || exit 0
working-directory: yew-master working-directory: yew-master
env:
RUSTUP_TOOLCHAIN: nightly
- name: Build pull request examples - name: Build pull request examples
run: find examples/*/index.html | xargs -I '{}' trunk build --release '{}' || exit 0 run: find examples/*/index.html | xargs -I '{}' trunk build --release '{}' || exit 0
working-directory: current-pr working-directory: current-pr
env:
RUSTUP_TOOLCHAIN: nightly
- name: Collect size information - name: Collect size information
run: | run: python3 current-pr/ci/collect_sizes.py
from typing import Dict, List, Optional env:
ISSUE_NUMBER: ${{ github.event.number }}
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}
- name: Upload Artifact - name: Upload Artifact
uses: actions/upload-artifact@v2 uses: actions/upload-artifact@v2

40
ci/collect_sizes.py Normal file
View File

@ -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))

View File

@ -120,6 +120,25 @@ opt-level = 'z'
lto = true 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 ### wasm-opt
Further more it is possible to optimize size of `wasm` code. Further more it is possible to optimize size of `wasm` code.