Inlining function could be faster because it:
- Eliminate the cost of calling function. The cost involves making changes to registers, pushing things to the stack, and then a jump in the code.
- Enable more optimization on the caller since the compiler has more info on how a function works. Depending on the cases, this would be the larger benefit.
The drawback, however, is larger code size, and that could lead to more icache miss.
For a function to be inlined in C++, it needs to be defined in the header, or link time optimization is enabled.
Compiler on average can decide on which function, or where a function should be inlined better than human, especially with profile guided optimization. However, there are cases that you might want to use these attribute manually, e.g. if you know for sure a function is always in a critical path, and it will not lead to code bloat; or if the path that you want to optimize for is not hot (executed less frequently compared to other paths).
Beside these attributes, there are other inline-related attributes that could be helpful, e.g., flatten
Note that you might see different results on Clang, due to how these attributes are interpreted.
References: