trunk icon indicating copy to clipboard operation
trunk copied to clipboard

Without `--release`, no wasm-opt is performed, despite custom profiles.

Open simbleau opened this issue 10 months ago • 2 comments

As discovered in #605,

Using a --cargo-profile which inherits from release does no wasm-optimization.

This is because the release flag has many opinions that shadow a custom profile, primarily around wasm optimization.

Example 1:

        let wasm_opt = attrs
            .get("data-wasm-opt")
            .map(|val| val.parse())
            .transpose()?
            .unwrap_or_else(|| {
                if cfg.release {
                    Default::default()
                } else {
                    WasmOptLevel::Off
                }
            });

Example 2:

    async fn wasm_opt_build(&self, wasm_name: &str) -> Result<()> {
        // If not in release mode, we skip calling wasm-opt.
        if !self.cfg.release {
            return Ok(());
        }

This shouldn't happen if a custom profile is applied, since I specified my own optimization level (i.e. opt-level = 'z') in a custom wasm-release profile. Currently, if I don't add --release, my custom profile's opt-level is skipped.

Thus, I'd propose the following:

  • Add a new --opt-level config, which will accept an opt level. Currently it is an attribute in documents, but doesn't have a CLI flag. Opt level should fall back to a sensible default if --release is used, but do nothing if a cargo profile is used.
  • If --opt-level is absent, and --release is not used, skip wasm-opt.

Hence, using a profiles should behave as such:

trunk build --release
  • Builds the release cargo profile
  • Runs wasm-opt with a default optimization level (current behavior)
trunk build --cargo-profile wasm-release
  • Builds the wasm-release cargo profile
  • Skips wasm-opt
trunk build --cargo-profile wasm-release --opt-level z
  • Builds the wasm-release cargo profile
  • Runs wasm-opt with a aggresize size ('z') optimization level

simbleau avatar Jan 05 '25 04:01 simbleau

An alternative I didn't mention is that we could attempt to read and inherit the optimization level from a given cargo-profile in the cargo manifest for packages (or workspace manifest, if a member).

Benefits:

  • No additional cli flag

Cons:

  • More complicated
  • What if the user wants a different wasm-opt level from a cargo opt level? Are they 1:1?

simbleau avatar Jan 05 '25 04:01 simbleau

IIRC a lot of features internally are triggered by the "release" flag. I think we should move away from this and have individual settings. And having --release only change the default values for those individual flags.

ctron avatar Jan 13 '25 08:01 ctron