Optimizations: The speed size tradeoff (2024)

Everyone wants their program to be super fast and super small but it's usuallynot possible to have both characteristics. This section discusses thedifferent optimization levels that rustc provides and how they affect theexecution time and binary size of a program.

No optimizations

This is the default. When you call cargo build you use the development (AKAdev) profile. This profile is optimized for debugging so it enables debuginformation and does not enable any optimizations, i.e. it uses -C opt-level = 0.

At least for bare metal development, debuginfo is zero cost in the sense that itwon't occupy space in Flash / ROM so we actually recommend that you enabledebuginfo in the release profile -- it is disabled by default. That will let youuse breakpoints when debugging release builds.

[profile.release]# symbols are nice and they don't increase the size on Flashdebug = true

No optimizations is great for debugging because stepping through the code feelslike you are executing the program statement by statement, plus you can printstack variables and function arguments in GDB. When the code is optimized, tryingto print variables results in $0 = <value optimized out> being printed.

The biggest downside of the dev profile is that the resulting binary will behuge and slow. The size is usually more of a problem because unoptimizedbinaries can occupy dozens of KiB of Flash, which your target device may nothave -- the result: your unoptimized binary doesn't fit in your device!

Can we have smaller, debugger friendly binaries? Yes, there's a trick.

Optimizing dependencies

There's a Cargo feature named profile-overrides that lets youoverride the optimization level of dependencies. You can use that feature tooptimize all dependencies for size while keeping the top crate unoptimized anddebugger friendly.

Beware that generic code can sometimes be optimized alongside the crate where itis instantiated, rather than the crate where it is defined. If you create aninstance of a generic struct in your application and find that it pulls in codewith a large footprint, it may be that increasing the optimisation level of therelevant dependencies has no effect.

Here's an example:

# Cargo.toml[package]name = "app"# ..[profile.dev.package."*"] # +opt-level = "z" # +

Without the override:

$ cargo size --bin app -- -Aapp :section size addr.vector_table 1024 0x8000000.text 9060 0x8000400.rodata 1708 0x8002780.data 0 0x20000000.bss 4 0x20000000

With the override:

$ cargo size --bin app -- -Aapp :section size addr.vector_table 1024 0x8000000.text 3490 0x8000400.rodata 1100 0x80011c0.data 0 0x20000000.bss 4 0x20000000

That's a 6 KiB reduction in Flash usage without any loss in the debuggability ofthe top crate. If you step into a dependency then you'll start seeing those<value optimized out> messages again but it's usually the case that you wantto debug the top crate and not the dependencies. And if you do need to debug adependency then you can use the profile-overrides feature to exclude aparticular dependency from being optimized. See example below:

# ..# don't optimize the `cortex-m-rt` crate[profile.dev.package.cortex-m-rt] # +opt-level = 0 # +# but do optimize all the other dependencies[profile.dev.package."*"]codegen-units = 1 # better optimizationsopt-level = "z"

Now the top crate and cortex-m-rt are debugger friendly!

Optimize for speed

As of 2018-09-18 rustc supports three "optimize for speed" levels: opt-level = 1, 2 and 3. When you run cargo build --release you are using the releaseprofile which defaults to opt-level = 3.

Both opt-level = 2 and 3 optimize for speed at the expense of binary size,but level 3 does more vectorization and inlining than level 2. Inparticular, you'll see that at opt-level equal to or greater than 2 LLVM willunroll loops. Loop unrolling has a rather high cost in terms of Flash / ROM(e.g. from 26 bytes to 194 for a zero this array loop) but can also halve theexecution time given the right conditions (e.g. number of iterations is bigenough).

Currently there's no way to disable loop unrolling in opt-level = 2 and 3 soif you can't afford its cost you should optimize your program for size.

Optimize for size

As of 2018-09-18 rustc supports two "optimize for size" levels: opt-level = "s" and "z". These names were inherited from clang / LLVM and are not toodescriptive but "z" is meant to give the idea that it produces smallerbinaries than "s".

If you want your release binaries to be optimized for size then change theprofile.release.opt-level setting in Cargo.toml as shown below.

[profile.release]# or "z"opt-level = "s"

These two optimization levels greatly reduce LLVM's inline threshold, a metricused to decide whether to inline a function or not. One of Rust principles arezero cost abstractions; these abstractions tend to use a lot of newtypes andsmall functions to hold invariants (e.g. functions that borrow an inner valuelike deref, as_ref) so a low inline threshold can make LLVM missoptimization opportunities (e.g. eliminate dead branches, inline calls toclosures).

When optimizing for size you may want to try increasing the inline threshold tosee if that has any effect on the binary size. The recommended way to change theinline threshold is to append the -C inline-threshold flag to the otherrustflags in .cargo/config.toml.

# .cargo/config.toml# this assumes that you are using the cortex-m-quickstart template[target.'cfg(all(target_arch = "arm", target_os = "none"))']rustflags = [ # .. "-C", "inline-threshold=123", # +]

What value to use? As of 1.29.0 these are the inline thresholds that thedifferent optimization levels use:

  • opt-level = 3 uses 275
  • opt-level = 2 uses 225
  • opt-level = "s" uses 75
  • opt-level = "z" uses 25

You should try 225 and 275 when optimizing for size.

Optimizations: The speed size tradeoff (2024)
Top Articles
M-PESA Global Customer Service Terms & Conditions
Unsellable Homes: 7 Factors that Make a Home Harder to Sell
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Dmv In Anoka
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Umn Biology
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 5965

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.