How to Optimize For Code Size?

I'm sure this has been asked before, but my initial searches don't seem to be finding what I'm looking for.

What are the tricks I can use to optimize my Rust app for code size? I'm interested in optimizing both the native and WASM builds.

With twiggy I'm seeing a lot of debug strings:

$ twiggy garbage wasmtest.wasm
 Bytes  │ Size % │ Garbage Item
────────┼────────┼──────────────────────────────────────────
  69991 ┊ 18.56% ┊ custom section '.debug_str'
  61917 ┊ 16.42% ┊ custom section '__wasm_bindgen_unstable'
  35788 ┊  9.49% ┊ custom section '.debug_info'
  22682 ┊  6.01% ┊ custom section '.debug_pubnames'
  10760 ┊  2.85% ┊ custom section '.debug_ranges'
   5288 ┊  1.40% ┊ custom section '.debug_line'
   1856 ┊  0.49% ┊ custom section '.debug_aranges'
   1394 ┊  0.37% ┊ custom section '.debug_abbrev'
    144 ┊  0.04% ┊ custom section '.debug_pubtypes'
     67 ┊  0.02% ┊ custom section 'producers'
     18 ┊  0.00% ┊ ... and 3 more
 209905 ┊ 55.66% ┊ Σ [13 Total Rows]
  25068 ┊  6.65% ┊ 31 potential false-positive data segments

Is there a way to get rid of those?

I've set panic = "abort" in my Cargo.toml, but is there a separate step I need to do to compile std with panic = "abort", as well?

The main techniques can be found in min-sized-rust.

For the specific issue you mentioned, those sections don't look like they are necessarily anything to do with panic handling, but are rather just sections containing debug information. You probably need to do something like stripping the executable, but I don't know enough about WASM to know if that's right or if there's tooling to do that for WASM.

1 Like

Ah, thank you!

I know I've seen that before, and I even already had the repo starred, I just forgot where it was. :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.