C++ Weekly - Ep 136 - How `inline` Might Affect The Optimizer

preview_player
Показать описание
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟

T-SHIRTS AVAILABLE!

WANT MORE JASON?

SUPPORT THE CHANNEL

GET INVOLVED

JASON'S BOOKS

► C++23 Best Practices

► C++ Best Practices

JASON'S PUZZLE BOOKS

► Object Lifetime Puzzlers Book 1

► Object Lifetime Puzzlers Book 2

► Object Lifetime Puzzlers Book 3

► Copy and Reference Puzzlers Book 1

► Copy and Reference Puzzlers Book 2

► Copy and Reference Puzzlers Book 3

► OpCode Puzzlers Book 1


RECOMMENDED BOOKS

AWESOME PROJECTS

O'Reilly VIDEOS

Рекомендации по теме
Комментарии
Автор

In C and C++, inline can make things faster or slower, depending on surrounding code, and which parts of the code you're performance testing. Main issue is I-cache pressure and code size (TLB usage, page faults, etc., not stuff you find in the ISA) in general. If you need it, a non-standard __forceinline keyword is available as __forceinline on MSVC, and on GCC. Note to budding C++ programmers: measure, measure, measure. The compiler is making guesses, educated guesses, but guesses nonetheless. If you truly care about performance, it's something you need to measure.

majormalfunction
Автор

My understanding is that the inline keyword is for allowing you to put the function definition in the .h file rather than the .cpp file so that all compilation units that include the headers will be able to inline it rather than forcing it to not inline because the doesn't have access to the function code because it's in a different translation unit. If you do this without the inline keyword, then that means that you'll violate the one-definition-rule of having the same symbol in multiple translation units. By marking the function inline, it tells the linker that it's ok to have multiple definitions for that function. Of course, this is a somewhat moot point if you are using link-time-optimization since it's able to grab the functions and inline them across compilation units, or if you're using the build process where all your cpp files get #included into a single file. I kind of wish this video went more into detail on that aspect since otherwise it's "the inline keyword doesn't really do anything"

FryGuy
Автор

CPP Core Guidelines:
"F.5: If a function is very small and time-critical, declare it inline
Reason

Some optimizers are good at inlining without hints from the programmer, but don’t rely on it. Measure! Over the last 40 years or so, we have been promised compilers that can inline better than humans without hints from humans. We are still waiting. Specifying inline encourages the compiler to do a better job."

What now?

Sonnentau
Автор

Thanks, this is good information for single a translation unit.
But real world applikations are made from multiple translation unit.
I think that the advice still applies in that case with Whole Program Optimization (or link time code generation) where the linker can do cross compilation unit inlining.
As per the advice in the video, I think that inline is/will become as relevant for performance as the keyword register

mogenshansen
Автор

6:13 Isn't constexpr having no effect here simply because we did not feed the function with constexpr arguments?

All in all very interesting video, this optimization view seems crazy useful to reason about what compiler is doing to my code, thanks.

zxfguiklnm
Автор

how does it actually calculate the inline threshold?

imnotapollo
Автор

I wasn't using inline. I forgot about what you said at the start of this video. There wasn't any issue as long as my library was header only and only one .cpp file. Today I decided to use 2 cpp files and included the same headers to both. And linker errors. :) So I had to spend the afternoon finding free functions in my headers that were missing inline. heh.

Sebanisu
Автор

This is all in the same translation unit but does it also automatically inline across translation units without constexpr / inline specification? Or even when you use the exported attribute? Or when using Visual Studio's compiler?

gast
Автор

I recently found that inline (at least with gcc) could be used to silence unused function warnings. (Skipping the details of the use case, I had two different implementations of a function [overloads], but only one was being used due to conditional compilation) I liked that I could silence the warning without introducing yet more preprocessor directives.

jaylebreak
Автор

I'm worried the threshold came from:

Clang Dev A: "Hey, B, I need a leet threshold"
Clang Dev B: "1337? <chuckle>"
Clang Dev A: "Too big"
Clang Dev B: "337?"
Clang Dev A: "<clickity clack> git add . && git commit -m perfect && git push"

JasonMercer
Автор

at Compiler Explorer MSVCC accepts static inline main :O

SeppyYT
Автор

hi, this video is not in cpp weekly playlist

myhardware
Автор

"Inline should be used when you need to deal with linking, if you're going to have a definition like if you're going to have a definition of a (file?) in multiple translation units and then are linking it". This only applies when you aim for header-only libraries, which is not generally the main necessity of inline.

Inline allows the definition of a function to reside in many translation units, so they can be potentially inlined, that's its main goal, hence the name. For what mostly? performance. You don't normally have the body of a function in multiple translation units, unless there's a reason: potential inlining, for performance. That's why you normally inline things.

"Conservative inline speaking" wins you no points. Inline is just generally misunderstood, and there's no reason to go the opposite way just to appear "reasonable".

Автор

I had the notion that _inline_ was a hint for the compiler that you "would like" to have a function inlined but that it was up to the compiler to decide whether it'd be ultimately inlined or not. Since then, in my mind, I always thought of inline as a useless near obsolete feature.

DamianReloaded
Автор

A lot of people have the idea that inline can be good. But I have rarely seen anyone talking about how no online attribute can be used to improve performance

andersfylling
Автор

What is the 'std=c++1z', what version is that?

valizeth
Автор

I became a $10 patron and didn't see my name on the list :-/...

vorpal