Pros and cons of "inline"

First of all, I would like to state the facts that I know about inline so that you do not repeat them.

  • A built-in function is a special kind of function, the definition of which should be available in each translation unit in which the function is used.
  • This is a hint for the compiler (which can be ignored) to omit the function call and expand the body instead of the call.
  • The only thing I know about is that (2.) can make the code faster.
  • The only thing I know is that (1.) increases the connection, which is bad.

Now consider the patterns. If I have a template library, I need to provide definitions of function templates in each translation unit, right? Let me forget about the controversial "export" for a while, because in reality this does not solve the problem. So, I came to the conclusion that there is no reason not to create a built-in template function, because the only con inline that I know about is a priori.

Please correct me if I am wrong. Thanks in advance.

+4
source share
4 answers

The only thing I know about is that (2.) can make the code faster.

Could be an operational word. Built-in functions can speed up certain code codes, yes.

But the built-in function puts additional pressure on the instruction cache on most modern processors. If your function is too large to be installed in the L1 cache, it may work more slowly than making a function call (for which the CPU can optimize by pre-programming the function and its return site).

Embedding a function can also lead to excessive pressure on the L2 cache - if the built-in function is used an unusually large number of times, an additional code size increases the likelihood of misses in the cache, which leads to long delays and pipeline outages, as the CPU squeezes the thumbs while waiting for the memory bus will do something.

inline far from a silver bullet. Compilers with aggressive optimizations completely ignore the inline hint, because instead they will choose heuristic-based inline functions, such as code size or the presence of branches, regardless of the presence or absence of the inline .

The only thing I know is that (1.) increases the connection, which is bad.

This is what I have never heard. โ€œLinkingโ€ is a concept that I only heard when describing high-level code relationships. It is rather a problem of maintainability and commonality of the code. inline - the problem of generating low-level code.

Regarding templates, again, an aggressively optimizing compiler will be built in if its heuristics demonstrate an advantage for this.

However, there is a link level problem: you may need to declare a function or template inline (or static , depending on the situation) to eliminate duplicate characters during the link or limit the visibility of characters. This, of course, is not an optimization.

In general, do not bother using the inline keyword unless necessary.

+11
source

I think you should not worry about writing inline or not before defining template functions. I think that most compilers will create small functions anyway, whether you ask them or not. Some compilers can even do this at join time for functions defined in only one translation unit.

Imho, the only inline keyword point is the ability to define functions without templates inside the headers, for those compilers that are not built in during the link (or when you need a library for headers only).

+1
source

you do not need to specify a definition in each translation - only those that use them;)

It doesn't matter if you declare them inline or not. the compiler can confirm your use of the keyword (or other inline ads). the compiler can generate exported classes / functions from your template definitions. if someone asked me whether by default or not (knowing that they do not know the effect of using it), I would say โ€œdo not use itโ€ because the compiler understands your program better. Optimization is different among compilers.

Example:

  • let's say that the compiler builds some functions according to your request
  • extension expansion during optimization is usually excluded due to a certain number of instructions ... oops - the compiler simply pulled out the material that was supposed to be aligned from the line, because the body was too large with these templates, which are built in by default.
  • or you can get a very large binary

therefore, you attach one hand to the compiler when you are built in by default (provided, of course, this confirms your use of the keyword).

in general: if you do not know, trust the compiler (writers).

There are several cases where I declare inline by default (even if it just reduces the exported characters).

Other benefits of investing:

  • nesting can make less code / functions
  • insertion can reduce the number of characters exported
  • it can also allow the compiler to improve optimization because it can โ€œseeโ€ most of the program during optimization.
+1
source

Thanks to the built-in, it removes the overhead of creating and destroying a new stack frame and moving to another location in exe. This makes the code a little faster.

The downside is, of course, an increase in the size of the exe, because the function must be implemented where it is called.

EDIT

Embedding functions make sense only when called many times, and the size of the function is small. Otherwise, increasing the file size can make the program slower, and it really is not worth it.

0
source

All Articles