Skip to content

Commit

Permalink
feat: shrink the wasm (#433)
Browse files Browse the repository at this point in the history
This patch does a number of things, all in the name of shrinking the
final wasm binary size. The first three are introducing features, two to
change the logging to optional, and one to use a smaller allocator (by
10k).

Additionally, it introduces using some tools to shrink the binary
further by removing more sections that are unneeded. These are used in
`wasm-build.sh`.

Fixes #432
  • Loading branch information
rockstar committed Apr 6, 2022
1 parent 648a3f3 commit b9b3476
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitattributes
@@ -0,0 +1 @@
Cargo.lock binary
54 changes: 39 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Expand Up @@ -8,7 +8,7 @@ description = "LSP support for the flux language"
repository = "https://github.com/influxdata/flux-lsp"

[package.metadata.wasm-pack.profile.release]
wasm-opt = ["-Oz", "--enable-mutable-globals"]
wasm-opt = ["-Oz"]

[profile.release]
opt-level = "z"
Expand All @@ -17,7 +17,6 @@ lto = true
[features]
default = []
strict = []
wasm_next = []

[lib]
name = "flux_lsp"
Expand All @@ -37,11 +36,11 @@ async-std = { version = "1.11.0", features = ["attributes"] }
async-trait = "0.1.53"
clap = { version = "3.1.8", features = ["derive"] }
combinations = "0.1.0"
console_error_panic_hook = "0.1.7"
console_error_panic_hook = { version = "0.1.7", optional = true }
console_log = { version = "0.2", optional = true }
env_logger = "0.9"
expect-test = "1.2.2"
flux = { git = "https://github.com/influxdata/flux", tag = "v0.162.0", features = ["lsp"] }
flux = { git = "https://github.com/influxdata/flux", tag = "v0.162.0", features = ["lsp"], default-features = false }
futures = "0.3.21"
js-sys = "0.3.56"
line-col = "0.2.1"
Expand All @@ -56,6 +55,7 @@ url = "2.2.2"
wasm-bindgen = { version = "0.2.79", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4.29"
web-sys = { version = "0.3.56", features = ["console"] }
wee_alloc = { version = "0.4.5", optional = true }

[dev-dependencies]
criterion = "0.3"
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Expand Up @@ -19,4 +19,8 @@ mod wasm;
#[macro_use]
extern crate pretty_assertions;

#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

pub use server::LspServer;
1 change: 1 addition & 0 deletions src/wasm.rs
Expand Up @@ -81,6 +81,7 @@ pub struct Lsp {

impl Default for Lsp {
fn default() -> Self {
#[cfg(feature = "console_log")]
console_error_panic_hook::set_once();

let (service, messages) =
Expand Down
10 changes: 8 additions & 2 deletions wasm-build.sh
Expand Up @@ -5,14 +5,14 @@ set -e
BUILD_MODE=${BUILD_MODE-release}

BUILD_FLAG=""
BUILD_MODE_ARGS=""
BUILD_MODE_ARGS="--features wee_alloc"
case $BUILD_MODE in
"release")
BUILD_FLAG="--release"
;;
"dev")
BUILD_FLAG="--dev"
BUILD_MODE_ARGS="--features console_log"
BUILD_MODE_ARGS="${BUILD_MODE_ARGS},console_log,console_error_panic_hook"
;;
*)
echo "Invalid build mode: ${BUILD_MODE}"
Expand Down Expand Up @@ -40,6 +40,12 @@ wasm-pack build \
--locked \
$BUILD_MODE_ARGS

# Strip producers header and some other optional bits.
wasm-strip pkg-node/flux-lsp-node_bg.wasm
wasm-opt -Oz -o pkg-node/flux-lsp-node_bg.wasm pkg-node/flux-lsp-node_bg.wasm
wasm-strip pkg-browser/flux-lsp-browser_bg.wasm
wasm-opt -Oz -o pkg-browser/flux-lsp-browser_bg.wasm pkg-browser/flux-lsp-browser_bg.wasm

cat pkg-node/package.json | sed s/@influxdata\\/flux-lsp\"/@influxdata\\/flux-lsp-node\"/g > pkg-node/package-new.json
mv pkg-node/package-new.json pkg-node/package.json

Expand Down

0 comments on commit b9b3476

Please sign in to comment.