if statement and calculation faster than constant

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
mvanthoor
Posts: 1784
Joined: Wed Jul 03, 2019 4:42 pm
Location: Netherlands
Full name: Marcel Vanthoor

Re: if statement and calculation faster than constant

Post 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).
Author of Rustic, an engine written in Rust.
Releases | Code | Docs | Progress | CCRL
User avatar
mvanthoor
Posts: 1784
Joined: Wed Jul 03, 2019 4:42 pm
Location: Netherlands
Full name: Marcel Vanthoor

Re: if statement and calculation faster than constant

Post 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.
Author of Rustic, an engine written in Rust.
Releases | Code | Docs | Progress | CCRL
MOBMAT
Posts: 385
Joined: Sat Feb 04, 2017 11:57 pm
Location: USA

Re: if statement and calculation faster than constant

Post 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.
i7-6700K @ 4.00Ghz 32Gb, Win 10 Home, EGTBs on PCI SSD
Benchmark: Stockfish15.1 NNUE x64 bmi2 (nps): 1277K
User avatar
mvanthoor
Posts: 1784
Joined: Wed Jul 03, 2019 4:42 pm
Location: Netherlands
Full name: Marcel Vanthoor

Re: if statement and calculation faster than constant

Post 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.
Author of Rustic, an engine written in Rust.
Releases | Code | Docs | Progress | CCRL