What does __inline__ mean ?

后端 未结 5 1846
独厮守ぢ
独厮守ぢ 2020-12-08 19:14

I am trying to learn C. Reading through some code, I came across a line like this:

__inline__ void () ...

What does the __inline__

相关标签:
5条回答
  • 2020-12-08 20:10

    Wikipedia / Inline Function

    In computer science, an inline function is a programming language construct used to tell a compiler it should perform in-line expansion on a particular function. In other words, the compiler will insert the complete body of the function in every place in the code where that function is used.

    GCC Manual § 5.36

    If you are writing a header file to be included in ISO C89 programs, write __inline__ instead of inline. See Alternate Keywords.

    0 讨论(0)
  • 2020-12-08 20:10

    basically, it's a hint to the compiler; it's a suggestion that where this function is used it might be possible to insert the code directly.

    the idea is that doing so could be more efficient, since it avoids the overhead of a function call; but, ultimately, the compiler uses a combination of code size, optimization options specified, and a little bit of voodoo to decide whether to actually respect this hint.

    the voodoo bit is most confusing part. :) don't necessarily rely on __inline__ (or the more standard inline) to work; optimize your algorithms or reorganize your code instead if you really care about speed.

    0 讨论(0)
  • 2020-12-08 20:14

    __inline__ is a non-standard extension. Typically, it tells the compiler: "inline this function", but being a non-standard extension we can't say with certainty unless we know which compiler this is on.

    To inline is to remove the function call and place it's contents directly where the call would be made. This often removes the overhead of calling a function. It is not always optimal, because of code bloat (code getting too big and not fitting into cache), so most compilers will ignore all inline directives and do what they feel is best. This is a good thing. We humans are very poor at that kind of stuff, and it's usually considered bad practice to tell the compiler how to do its job.

    Inlining is an important optimization, especially with the presence of helper functions. Imagine a function that returned the smaller of two ints:

    int min(int x, int y)
    {
        return (x < y) ? x : y;
    }
    

    If I used this function in my code, it would be an enormous waste of time to actually make a function call, here. If I had:

    int a = /* some calculation */;
    int b = /* some other calculation */;
    
    int minCalc = min(a, b);
    

    And the compiler inlined that function, the code would become:

    int a = /* some calculation */;
    int b = /* some other calculation */;
    
    int minCalc = (a < b) ? a : b;
    

    Which removes the overhead of calling a function. From here, even more optimizations can be made as the compiler gets to work directly with the code that would have normally been hidden behind a function call. As you can see, if I have a big function and I force the compiler to inline it everywhere, the code size could grow very large very fast, and would actually hinder execution speed.

    There is a standard inline keyword which was used to indicate to the compiler a function should be inlined, but nowadays most compilers don't even acknowledge it as a hint to inline the function.

    There is an important side-effect of inline, though, and this can be useful. If a function is marked as inline, multiple definitions of the same function across multiple translation units is not an error. Instead, a single function definition is selected and the others are thrown out, and assumed to be the same (it's up to you to make sure this is actually okay!). This allows you to define a function within a header file without risking ODR violation errors.

    0 讨论(0)
  • 2020-12-08 20:16

    Inline is a suggestion to the compiler to inline the function - instead of generating a block of code for the function and call instructions wherever it's used, it effectively cut-and-pastes the generated code wherever that function is called.

    It's typically only a suggestion - while it can increase performance if it's called in a tight loop, it typically increases memory usage which can have generally negative performance effects. The compiler will make up its own mind whether to inline a function or not - it might take "the programmer suggests inlining this" into account, but (on most compilers), simply declaring a function as inline doesn't guarantee it will be.

    0 讨论(0)
  • 2020-12-08 20:16

    The __inline__ declaration tells the compiler that wherever the function will be called to "inline" the function instead. Inlineing is similar to using a macro -- the code will be expanded out as if you had written it all right where you called the function.

    See the GNU docs for more information.

    0 讨论(0)
提交回复
热议问题