Skip to content

Commit

Permalink
schema-wasm: optimize for -Oz with LLVM and Binaryen
Browse files Browse the repository at this point in the history
This PR explores how much size can be trivially saved in WASM binaries
compared to how we currently build them, using `prisma-schema-wasm` as
the example. This is not necessarily useful for `prisma-schema-wasm`
but could be useful for the QE.

- Enable more aggressive size optimization level in LLVM (`z` instead of
  `s`) at some runtime performance cost.
- Further optimize the WASM binary for size using `wasm-opt` from
  Binaryen.

| optimization level     | size    | ratio  |
| ---------------------- | ------- | ------ |
| `-Os` (baseline)       | 2742189 | 100%   |
| `-Oz`                  | 2558271 | 93.29% |
| `-Oz` + `wasm-opt -Oz` | 2081699 | 75.91% |
  • Loading branch information
aqrln committed Nov 15, 2023
1 parent 2963919 commit fad616b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ lto = "fat"
codegen-units = 1
opt-level = 's' # Optimize for size.

[profile.wasm-release]
inherits = "release"
opt-level = 'z' # Aggressively optimize for size

[profile.profiling]
inherits = "release"
debug = true
4 changes: 2 additions & 2 deletions nix/prisma-schema-wasm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ in
{
packages.prisma-schema-wasm = stdenv.mkDerivation {
name = "prisma-schema-wasm";
nativeBuildInputs = with pkgs; [ git wasm-bindgen-cli toolchain ];
nativeBuildInputs = with pkgs; [ git wasm-bindgen-cli toolchain binaryen ];
inherit (self'.packages.prisma-engines) configurePhase src;

buildPhase = "cargo build --release --target=wasm32-unknown-unknown -p prisma-schema-build";
buildPhase = "cargo build --profile=wasm-release --target=wasm32-unknown-unknown -p prisma-schema-build";
installPhase = readFile "${scriptsDir}/install.sh";
};

Expand Down
7 changes: 6 additions & 1 deletion prisma-schema-wasm/scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ cp ./prisma-schema-wasm/package.json "$out"/
printf '%s\n' " -> Copying README.md"
cp ./prisma-schema-wasm/README.md "$out"/

printf '%s\n' " -> Optimizing WASM binary for size"
wasm-opt -Oz \
target/wasm32-unknown-unknown/wasm-release/prisma_schema_build.wasm \
-o target/prisma_schema_build.wasm

printf '%s\n' " -> Generating node package"
wasm-bindgen \
--target nodejs \
--out-dir "$out"/src \
target/wasm32-unknown-unknown/release/prisma_schema_build.wasm
target/prisma_schema_build.wasm

0 comments on commit fad616b

Please sign in to comment.