if statement and calculation faster than constant

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
User avatar
mvanthoor
Posts: 606
Joined: Wed Jul 03, 2019 2:42 pm
Full name: Marcel Vanthoor

Re: if statement and calculation faster than constant

Post by mvanthoor » Mon Apr 13, 2020 5:47 pm

fabianVDW wrote:
Mon Apr 13, 2020 3: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).

User avatar
mvanthoor
Posts: 606
Joined: Wed Jul 03, 2019 2:42 pm
Full name: Marcel Vanthoor

Re: if statement and calculation faster than constant

Post by mvanthoor » Mon Apr 13, 2020 5:50 pm

bob wrote:
Mon Apr 13, 2020 4: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.

MOBMAT
Posts: 327
Joined: Sat Feb 04, 2017 10:57 pm
Location: USA

Re: if statement and calculation faster than constant

Post by MOBMAT » Mon Apr 13, 2020 9:45 pm

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.
i7-6700K @ 4.00Ghz (using 6 threads), EGTBs on PCI SSD
Benchmark: Stockfish 11 64 bmi2 (nps): 2067669

User avatar
mvanthoor
Posts: 606
Joined: Wed Jul 03, 2019 2:42 pm
Full name: Marcel Vanthoor

Re: if statement and calculation faster than constant

Post by mvanthoor » Mon Apr 13, 2020 10:35 pm

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.

Post Reply