CS253

CS253: Software Development with C++

Spring 2018

Inlining

See this page as a slide show

Inlining

CS253 Inlining

Overview

Simple Class

% cp ~cs253/Examples/Inline/* .
% head *.h *.cc
==> Numbers.h <==
class Numbers {
  public:
    static int two();
};

==> Numbers.cc <==
#include "Numbers.h"
int Numbers::two() {
    return 2;
}

==> main.cc <==
#include "Numbers.h"
int main() {
    return Numbers::two();
}

Compile it

% cp ~cs253/Examples/Inline/* .
% g++ -Wall -c main.cc
% g++ -Wall -c Numbers.cc
% g++ -Wall main.o Numbers.o
% objdump --no-show-raw-insn -Cd | sed -n '/<main>/,/^$/p'
00000000004005ad <main>:
  4005ad:	push   %rbp
  4005ae:	mov    %rsp,%rbp
  4005b1:	callq  4005b8 <Numbers::two()>
  4005b6:	pop    %rbp
  4005b7:	retq   

main() called Numbers::two(). This is not surprising.

Optimization

There are several optimization arguments to g++:

Compile it with optimization

% cp ~cs253/Examples/Inline/* .
% g++ -Wall -O3 -c main.cc
% g++ -Wall -O3 -c Numbers.cc
% g++ -Wall -O3 main.o Numbers.o
% objdump --no-show-raw-insn -Cd | sed -n '/<main>/,/^$/p'
00000000004004c0 <main>:
  4004c0:	jmpq   4005b0 <Numbers::two()>

Fewer instructions—good. Still called Numbers::two(), which really can’t be avoided.

Link-Time Optimization

This tells the linker to perform optimization between the individual *.o object files.

Link-time optimization

% cp ~cs253/Examples/Inline/* .
% g++ -Wall -O3 -flto -c main.cc
% g++ -Wall -O3 -flto -c Numbers.cc
% g++ -Wall -O3 -flto main.o Numbers.o
% objdump --no-show-raw-insn -Cd | sed -n '/<main>/,/^$/p'
00000000004004c0 <main>:
  4004c0:	mov    $0x2,%eax
  4004c5:	retq   

Holy smokes! 😲

Conclusion

Modified: 2018-02-11T11:23

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building