Share this article:
Staying Inline
The idea of inlining code – placing the actual code of a small function at each call site – is a well known compiler optimization, which I have discussed before. This technique can provide significant performance improvements, due to the elimination of the call/return sequence. Also, stack usage is reduced. There is a possible cost in terms of increased program memory requirement.
It is reasonable to expect a good C or C++ compiler, when told to compile for speed, to perform inlining automatically. Some C compilers have extensions to give more control over this, but C++ has intrinsic support for inlining within the language …
C++ has a number of keywords that are additional to those in C. Among those is inline. This is a directive to the compiler requesting that the function is inlined. The compiler is at liberty to ignore the request, if the function is too big or inlining conflicts with optimization settings (i.e. if switches request small code instead of fast code). The inline keyword is also implemented in many modern C compilers, as a language extension, and works in the same way.
But C++ has some other inlining tricks. It is very common for functions, which are part of objects (class member functions) to be small and called with great frequency. Thus, such functions are strong contenders for inlining. They may be declared inline in the same way, using the keyword. However, there is another way to convey the inlining request to the compiler: place the actual code inside the class definition, instead of just declaring the function and defining it outside.
So, C++ has two different ways to declare a function to be inline:
In this code, both classes have an inline function called foo().
This begs a question: what is the difference, in terms of generated code, between these two ways to declare an inline?
Read more from Colin on SciTech Connect:
- Thanks for the Memory
- Software Product Quality: Belief or Proof?
- Embedded Optimization: Small or Fast?
- C, C++ and the Family Tree
- Agile Embedded
Colin’s most recent publication, Embedded Software: The Works is available now on the Elsevier Store. Save 30% on his book and other Newnes Press and embedded systems books. Use discount code “STC3014″ at checkout.
About the Author
Colin Walls (@Colin_Walls) is an embedded software technologist at Mentor Graphics (@mentor_graphics), the leading EDA software company.
You can read more about Colin and his work on embedded systems at The Colin Walls Blog at Mentor Graphics here. Connect with Colin online here:
7 thoughts on “Staying Inline”
Comments are closed.
Computing functionality is ubiquitous. Today this logic is built into almost any machine you can think of, from home electronics and appliances to motor vehicles, and it governs the infrastructures we depend on daily — telecommunication, public utilities, transportation.
Maintaining it all and driving it forward are professionals and researchers in computer science, across disciplines including:
- Computer Architecture and Computer Organization and Design
- Data Management, Big Data, Data Warehousing, Data Mining, and Business Intelligence (BI)
- Human Computer Interaction (HCI), User Experience (UX), User Interface (UI), Interaction Design and Usability
- Artificial intelligence (AI)
- Peter Pacheco’s An Introduction to Parallel Programming
- Carol Barnum’s Usability Testing Essentials
- Peterson and Davie’s Computer Networks
You are doing a good job