How much are your inline annotations actually worth?

Discussion of chess software programming and technical issues.

Moderator: Ras

clayt
Posts: 29
Joined: Thu Jun 09, 2022 5:09 am
Full name: Clayton Ramsey

How much are your inline annotations actually worth?

Post by clayt »

I was testing around and came across some interesting results.

In Rust, a function can be annotated with

Code: Select all

#[inline]
and

Code: Select all

#[inline(always)]
to mark functions for inlining and aggressive inlining. I had originally been liberal with these annotations on pretty much every short function in my engine. However, I just finished a quick test and found that there was essentially no impact from the inline annotations on the resulting code, likely because the optimizer inlines everything that needs to be inlined anyway.

Have you all tried marking your functions for inlining? Did you see any performance improvements?
Tearth
Posts: 70
Joined: Thu Feb 25, 2021 5:12 pm
Location: Poland
Full name: Pawel Osikowski

Re: How much are your inline annotations actually worth?

Post by Tearth »

It kind of helps sometimes, but as you said, in 95% of cases compiler is smart enough to do it itself. And at the same time I've had bad experiences from the time when I was writing Cosette, where aggressive inlining in bad places was very harmful to performance (because more machine code = more pressure on the processor cache).
User avatar
lithander
Posts: 915
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: How much are your inline annotations actually worth?

Post by lithander »

Great question!

Leorik is written in C# where you have to put an attribute over methods you want to be inlined.

Code: Select all

[MethodImpl(MethodImplOptions.AggressiveInlining)]

When I search 300 positions to depth 15 it takes about a minute with the heavily inlined version:

Code: Select all

Searched 300 positions with (15)
462849301 nodes visited. Took 61,196 seconds!
7563K NPS.
Best move found in 293 / 300 positions!
...and this is the same workload but with all inlining hints removed:

Code: Select all

Searched 300 positions with (15)
462849301 nodes visited. Took 79,969 seconds!
5787K NPS.
Best move found in 293 / 300 positions!
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess
benb
Posts: 16
Joined: Sat Dec 12, 2020 5:29 am
Full name: Ben Bradley

Re: How much are your inline annotations actually worth?

Post by benb »

Does anyone look at the code in Compiler Explorer or a debugger to see which functions actually get inlined vs. which ones are "requested" for inlining? This of course will/should change with different optimization levels.

I learned a long time ago that such things in C (inline for functions and register for variables) are just "suggestions" to the compiler, and I haven't bothered using them since. Indeed, over the decades, compiler optimization has advanced substantially, and using these would be second-guessing an entity smarter than me.