Function Inlining in C++ | Modern Cpp Series Ep. 109

preview_player
Показать описание

►Lesson Description: In this lesson I talk about an important topic in optimization which is function inlining. This optimization can enable other optimizations by embedding the code from a function call directly at the call site, and otherwise avoid the overhead of setting up a function call (through the activation context or 'frame'). That said, there is a trade-off which it comes to the code size, as well as the debugability of a program. Compilers will take the 'inline' keyword as a hint, and otherwise do their best to inline code where performance may be improved. Again, function inlining often enables code optimizations, but should be used cautiously -- though I will show you how to force inlining if you are certain with compiler attributes in this lesson.

►Please like and subscribe to help the channel!
Рекомендации по теме
Комментарии
Автор

I love the pros and cons. Now I know that if I might want to reduce executer size I might turn inline functions back to regular ones.

henrijohansson
Автор

I can see inline being a performance loss in some corner cases due to the effect on the L1i cache if a function is called several times in close proximity and after branching (in both branches) a function could stay in cache while many inlined copies would be constantly loaded and evicted.
This would certainly be very dependent on the specific code with specific hardware and need testing due to all of the unknowns.

mytech
Автор

sir, i think you are underrated , and the quality of your content his highly professional and to the point, thank you for such nice explanations

sachinjain
Автор

inline in C++ works differently to C language
inline specifier in C means to the compiler "inline this function when it is possible to do that".
but in inline in C++ language means to the compiler "don't complain about two declarations of a function "or variable in C++17" in two different translation units, and only choose one address of one function to be exposed in the external in linkage table".

and the meaning of "C inline" in C++ is only left to the optimization of the compiler, if you used -O3 optimization level you will get it inlined.
Or by using some attributes to force the compiler to inline it, but it isn't C++ standard it is only a feature in gcc, as __forceinline is an attribute in MSVC for the same purpose.

CPP_malloc
Автор

I very much appreciate your videos about C++. Thank you very much for providing such helpful content. Perhaps you could occasionally make another video (or even a series) about your vim setup that includes tips on configuring vim for C++?

mdatab
Автор

Hi Mike, thanks for this great c++ series, looking forward for more content.
In the mean time I will start watching your other series, currently I am interested in
"C++ Software Design and Design Patterns",
"Modern C++ (cpp) Concurrency",
"Introduction to OpenGL",
"SDL2 Simple Directmedia Layer" and
"SFML - Simple Fast Media Library"

saifsuleiman
Автор

I suggest a video in the C language series about C11 threads, the POSIX pthread library and c11 threads vs POSIX pthread.

dogdog
Автор

Isn't this what the pre-compiler and macro's are for? I feel pain for knowing what inline is now.

CorpoWolf
Автор

A clear explanation, and again, having the chance to view the things happening in assembly language is a big plus, great content all of yours. I now use constexpr for all my functions with GCC-13 and -std=c++23, even for lambdas, it does the inlining plus the compile-time (if possible). Mike, are you available (I mean consulting services) for code reviews? I would like to have a quotation for a specific case in one of my open-source projects

MartinCordova
Автор

Wooohooo... Compiler Explorer always makes me feel good .. I already knew about inline function `hint` to the compiler but what was the key takeaway for me was the attribute always inline that was something new... Thanks for the video... What's your plan for this year in this channel? Have you given a thought on Doing something in Rust?

monty
Автор

Hi, what resources I can read to understand that assembly code on the right? Thank you.

mister-ace
Автор

Would you consdier making a series on something say, "the internal workings of C++ with the compiler explorer/objdump"? Explaining to us the internal workings of the C++ langauge? If basic asm is needed, I can learn that.

_w_
Автор

Do you also have Linux system programming using C++ in your pipeline for this year?

abhishek_karki
Автор

Which version of C++was this feature introduced

windowsbuilderthegreat