Skip to content

Commit 5491cf8

Browse files
authored
build(swc): enable Wasm benchmarks (jantimon#24)
- Migrate to workspace to allow testing in isolation - Add benchmarks
1 parent 1a0006f commit 5491cf8

File tree

11 files changed

+603
-134
lines changed

11 files changed

+603
-134
lines changed

swc/Cargo.lock

Lines changed: 171 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swc/Cargo.toml

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
[package]
2-
name = "swc-plugin-css-variable"
3-
version = "0.1.0"
4-
edition = "2021"
5-
6-
[lib]
7-
crate-type = ["cdylib"]
1+
[workspace]
2+
members = [
3+
"swc-plugin-css-variable",
4+
"transform",
5+
]
86

97
[profile.release]
108
# Uncomment to achieve smallest possible binary size (may require more experimentation).
@@ -13,23 +11,3 @@ crate-type = ["cdylib"]
1311
# codegen-units = 1
1412
# lto = true
1513
# opt-level = "z"
16-
17-
[dependencies]
18-
serde = {version="1.0.152", features = [
19-
"derive",
20-
]}
21-
serde_json = "1.0.91"
22-
swc_core = {version = "0.58.0", features = [
23-
"ecma_plugin_transform",
24-
"ecma_ast",
25-
"common",
26-
]}
27-
pathdiff = "0.2.1"
28-
regex = "1.7.1"
29-
lazy_static = "1.4.0"
30-
xxhash-rust = { version = "0.8.6", features = ["xxh32"] }
31-
base62 = "2.0.2"
32-
33-
[dev-dependencies]
34-
swc_ecma_parser = "0.124.2"
35-
swc_ecma_transforms_testing = "0.119.0"

swc/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# swc-plugin-css-variable
2+
3+
This is the SWC counterpart to the babel plugin of this library.
4+
5+
## Structure
6+
7+
This is a workspace with two crates:
8+
9+
- `swc-plugin-css-variable` is the outer shell and only contains what's
10+
necessary to interface with SWC, which is essentially the main function that
11+
is decorated with `#[plugin_transform]`
12+
- `transform` has everything else, most importantly the visitor implementation
13+
14+
The reason for the split is to enable benchmarking inside Wasm runtimes. If the
15+
code under test (the visitor) were in the same crate as `#[plugin_transform]`,
16+
code would be generated that requires functions to be linked in for accessing
17+
the environment and emitting diagnostics. Normally these are provided at
18+
runtime by SWC, but for only running benchmarks we don't want to have to
19+
fake them.
20+
21+
## Benchmarking
22+
23+
The benchmarks can be run easily with `cargo bench`. Note that this builds and
24+
runs native code on your machine, which may not exhibit the exact same
25+
performance characteristics as a Wasm module in a runtime, as is the case for
26+
SWC transformations.
27+
28+
To account for this, it's also possible to build a benchmark executable in Wasm
29+
format and execute it in your runtime of choice. Since SWC internally uses
30+
[Wasmer](https://wasmer.io), it makes sense to install this one. Then, it's
31+
just a matter of
32+
33+
```console
34+
$ cargo build --bench bench_main --release --target wasm32-wasi
35+
# Get the name of the Wasm module we just built
36+
$ ls -t target/wasm32-wasi/release/deps/*.wasm | head -n 1
37+
target/wasm32-wasi/release/deps/bench_main-9530540cc15e2e67.wasm
38+
# Execute the benchmark
39+
$ wasmer target/wasm32-wasi/release/deps/bench_main-9530540cc15e2e67.wasm -- --bench
40+
```
41+
42+
With this you get the most accurate runtime behavior and can observe the
43+
difference to native code. However, you'll most likely find it negligible, with
44+
Wasm being just slightly slower and everything else scaling mostly linearly. A
45+
typical example:
46+
47+
- `wasmer: visitor styledPage time: [6.6177 µs 6.6316 µs 6.6495 µs]`
48+
- `native: visitor styledPage time: [5.4191 µs 5.4736 µs 5.5150 µs]`
49+
50+
For most changes that aren't Wasm-specific, it's therefore often good enough to
51+
just run the benchmarks normally with `cargo bench`.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "swc-plugin-css-variable"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
# cdylib is for dynamic system library and necessary to produce the Wasm plugin
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
lazy_static = "1.4.0"
12+
pathdiff = "0.2.1"
13+
regex = "1.7.1"
14+
serde_json = "1.0.91"
15+
swc_core = { version = "0.58.0", features = ["ecma_plugin_transform"] }
16+
17+
transform = { path = "../transform" }

0 commit comments

Comments
 (0)