Checking how well my programs optimized

Hi all,

I was wondering what are my options for checking how well my programs are optimized. Ideally, I'd like to see a Cargo parameter for dumping some internal representation of programs that somehow shows how well my programs are optimized. One example case that I'm interested in is optimization of iter, filter, map, reduce etc. calls. For example, if I have:

blah.into_iter().map(|c| ...).filter(|c| ...).reduce(|c| ...);

(or collect() instead of reduce())

I'd like to see if this is optimized to a single-pass loop that doesn't do intermediate allocations (given the closures don't allocate) and doesn't allocate closures or actually call any closures.

Some of the other cases that I'm interested in are some unwrap() calls that are statically guaranteed to not fail. One example is if I call a function like this:

fn blah(i : u32) -> Option<...> {
    if i == 0 {
        None
    } else {
        Some(...)
    }
}

... blah(10).unwrap() ...

So I have lots of cases like these that I'd like to investigate a little bit. I can read the assembly but it's a bit too big and complicated for these purposes. It's hard to understand what's really going on at assembly level.

Thanks.

Not exactly sure what you want if you don't want to read the disassembly. I think you can output the LLVM IR and look at that if you want.

Otherwise it's possible to profile the code and see how fast it actually runs but I don't think that is what you really want?

Yes, with --emit=llvm-ir. This might be a bit easier to read than assembly, though it isn't always. (Is there any tool to intersperse IR with corresponding source lines, like objdump -S for assembly?)

llvm-dis -show-annotations?

1 Like

Thanks all. Is there an easy way to do this using Cargo? Ideally, I'd like to see all .ll files for dumped to a directory.

The way I do this is to just build using --release --verbose and copy'n'paste the commandline for file I'm interested interested in and add more things to the --emit=... part of the command line but if there is a better way I would be happy to use that :slight_smile: