CS253: Software Development with C++

Spring 2018

Inlining

See this page as a slide show

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'
00000000004004fd <main>:
  4004fd:	push   %rbp
  4004fe:	mov    %rsp,%rbp
  400501:	callq  400508 <Numbers::two()>
  400506:	pop    %rbp
  400507:	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'
0000000000400410 <main>:
  400410:	jmpq   400500 <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'
0000000000400410 <main>:
  400410:	mov    $0x2,%eax
  400415:	retq   

Holy smokes! 😲

Conclusion

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-24T16:54

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building