uniffi-rs icon indicating copy to clipboard operation
uniffi-rs copied to clipboard

Add documentation examples of potential optimizations for released libraries

Open thunderbiscuit opened this issue 2 years ago • 5 comments

Hi there! Our team uses a custom release profile to reduce the size of the binaries and final libraries. I'm wondering if the Mozilla team has any special insights into what works best for minimizing the size of the libraries, and think there might be value in adding such examples and discussion of tradeoffs in the user guide.

This is our current release profile. Would love to hear what other folks use and potential improvements/tradeoffs!

[profile.release-smaller]
inherits = "release"
opt-level = 'z'     # Optimize for size.
lto = true          # Enable Link Time Optimization
codegen-units = 1   # Reduce number of codegen units to increase optimizations.
panic = 'abort'     # Abort on panic
strip = true        # Strip symbols from binary*

thunderbiscuit avatar Jul 03 '23 15:07 thunderbiscuit

TBH, I'm not aware of Mozilla looking at that over the last couple of years. We have very simple profiles - typically opt-level="s", lto = "thin" and debug = true - I think that latter is still hanging around from when we could get rust backtraces on error, so we should rethink that. panic is an interesting one - we do occasionally see panics, so if our mobile peers come knocking on our door, we do have a story for allowing them to not crash. It also means we can report more information into our crash reporter if (ironically) we make the report before crashing. However when used on Firefox Desktop, panic is set to abort there, along with a completely different profile which needs to take into account all Rust code in Desktop Firefox, of which uniffi'd components are a rounding error.

mhammond avatar Jul 05 '23 14:07 mhammond

Cool, good to know. Well I can say that this profile above cut our dylib file size in half for Kotlin, (12.2MB to 5.8MB) and I think just about that for Swift too. But it also feels like dark magic a little bit... I don't know enough about it to play around with all the flags we could use, hence my asking for a section in the docs on optimization.

Feel free to close this issue if it's not really on your radar for the docs. Cheers!

thunderbiscuit avatar Jul 06 '23 19:07 thunderbiscuit

Feel free to close this issue if it's not really on your radar for the docs. Cheers!

It really just means we haven't explored that for a couple of years so we really aren't qualified to write it, but I think it would make a great addition to the docs.

mhammond avatar Jul 07 '23 14:07 mhammond

I found this site useful for figuring out how to shrink release binaries:

https://github.com/johnthagen/min-sized-rust

notmandatory avatar Jul 11 '23 13:07 notmandatory

A similar profile to @thunderbiscuit works for us. We didn't find much benefit to z optimization (25% performance hit based on benchmarks for 0.1MB size decrease), and needed to set strip = "debuginfo" to have enough information around for uniffi's --library bindgen to work. All that brought our binary from ~7MB to ~1MB, and since our deployment strategy for a few languages is to bundle all the different platform binaries into one artifact, that's pretty useful.

skeet70 avatar Sep 27 '23 16:09 skeet70