Page 5 of 5

Re: if statement and calculation faster than constant

Posted: Mon Apr 13, 2020 7:47 pm
by mvanthoor
fabianVDW wrote: Mon Apr 13, 2020 5:12 pm Oh ok, thanks, I should try it without always aswell.
Maybe you'll already know, but for future posters I'll add this information.

If you want a function to inline when building for release and not inline when building for debugging, add these annotations:

Code: Select all

#[cfg_attr(debug_assertions, inline(never))]
#[cfg_attr(not(debug_assertions), inline(always))]
Note: it *has* to be the option "debug_assertions" which has to be either set or unset in Cargo.toml. There's also an option "debug" that adds debug symbols, but when you use it in the annotation, it's simply ignored; it looks like for an unknown/ignored option, the "not" version of the annotation is triggered, because when I use "debug", the function is always inlined. Don't know why "debug_assertions" works, and "debug" doesn't, because they're both profile settings in Cargo.toml.

The profiles I'm using are:

Code: Select all

[profile.dev]
opt-level = 1
lto = false
codegen-units = 1
panic = 'unwind'
debug = true
debug-assertions = true
rpath = false
incremental = false
overflow-checks = false

[profile.release]
opt-level = 3
lto = true
codegen-units = 1
panic = 'abort'
debug = false
rpath = false
debug-assertions = false
incremental = false
overflow-checks = false
Use optimization level 1. If you don't and use 0, the profiler will show you every function in the history of ever from the underlying C++ runtime (at least VTune does, for which I need to compile with the MSVC toolchain, which compiles against MSVC140.dll).

Re: if statement and calculation faster than constant

Posted: Mon Apr 13, 2020 7:50 pm
by mvanthoor
bob wrote: Mon Apr 13, 2020 6:55 pm Compile using the profile-guided-optimization option. It will handle that for you painlessly...
Thanks for the reminder :)

There are some steps involved:
https://doc.rust-lang.org/rustc/profile ... ation.html

I'm going to try this next weekend, and if it works well enough, use it for definitive releases.

Re: if statement and calculation faster than constant

Posted: Mon Apr 13, 2020 11:45 pm
by MOBMAT
if you are using MSVS, then the /Od command line option will disable inlines, along with a lot of other things.
It also enables any calls to assert, which you should have liberally sprinkled in your debug code. Calls to assert are ignored when you don't have debugging enabled thus saving you the time to add compile time #ifdef type code.

When I create a new project, I create two different builds, one for debugging and one for release. Debugging never does any optimization while release optimizes as much as possible.

Once I've set all the command line parameters correctly, I don't have to question myself about why something isn't behaving the way I expect it to.

Re: if statement and calculation faster than constant

Posted: Tue Apr 14, 2020 12:35 am
by mvanthoor
Hi :)

I don't use Visual Studio. I use VSCode.

Believie it or not, but I've written everything up to now with very few asserts. The only time I've used them is while developing a function, and after it works, I took them out.

I've been thinking to put them in again, but up to now, I've got no real reason.