SlideShare a Scribd company logo
1 of 6
Function Call Optimization


This note describes the observations on the following Function Calls:

     1.     Constructor call
     2.     Constructor call of Base Class
     3.     Get / Set Methods.

To Test for the above a sample Base Class has been developed.
class Base
{
      public:
            size_t            nameLen;
            char              *name;

          protected:
                Base(char *nameStr):
                      nameLen((nameStr)? strlen(nameStr): 0),
                      name(strcpy(new char[nameLen+1], nameStr))
                {}
          …
};

This generates the following assembly in debug build using g++. The function calls
have been marked in red.

          _ZN4BaseC2EPc:                            => Base::Base()
          .LFB1443:
                .loc 3 17 0
                pushq %rbp #
          .LCFI22:
                movq %rsp, %rbp #,
          .LCFI23:
                pushq %rbx #
          .LCFI24:
                subq $40, %rsp   #,
          .LCFI25:
                movq %rdi, -16(%rbp)         # this, this
                movq %rsi, -24(%rbp)         # nameStr, nameStr
          .LBB13:
                .loc 3 20 0
                movq -16(%rbp), %rax         # this,
                movq %rax, -32(%rbp)         #,
                cmpq $0, -24(%rbp)           #, nameStr
                je    .L39 #,
                movq -24(%rbp), %rdi         # nameStr, nameStr
                call strlen      #                 => strlen()
                movq %rax, -40(%rbp)         # tmp61,
                jmp   .L40 #
          .L39:
                movq $0, -40(%rbp)           #,
.L40:
             movq      -40(%rbp), %rax  #,
             movq      -32(%rbp), %rdx  #,
             movq      %rax, (%rdx)     #, <variable>.nameLen
             movq      -16(%rbp), %rbx  # this, this
             movq      -16(%rbp), %rax  # this, this
             movq      (%rax), %rdi     # <variable>.nameLen, tmp63
             incq      %rdi # tmp63
             call      _Znam #                => operator new()
             movq      %rax, %rdi #, tmp65
             movq      -24(%rbp), %rsi  # nameStr, nameStr
             call      strcpy       #         => strcpy()
             movq      %rax, 8(%rbx)    #, <variable>.name
       .LBE13:
             addq      $40, %rsp       #,
             popq      %rbx #
             leave
             ret

The Derived Class,

       class Derived: public Base
       {
                   Base *myBase;

               public:
                     Derived(char *name):
                           Base(name),
                           myBase((Base*)this)
                     {}
               …
       };

generates the following assembly:

       _ZN7DerivedC1EPc:                              => Derived::Derived()
       .LFB1452:
             .loc 3 53 0
             pushq %rbp #
       .LCFI19:
             movq %rsp, %rbp #,
       .LCFI20:
             subq $16, %rsp   #,
       .LCFI21:
             movq %rdi, -8(%rbp)              # this, this
             movq %rsi, -16(%rbp)             # name, name
       .LBB12:
             .loc 3 56 0
             movq -16(%rbp), %rsi             #   name, name
             movq -8(%rbp), %rdi              #   this, this
             call _ZN4BaseC2EPc               #       => Base::Base()
             movq -8(%rbp), %rdx              #   this, this
             movq -8(%rbp), %rax              #   this, this
             movq %rax, 16(%rdx)              #   this, <variable>.myBase
       .LBE12:
             leave
             ret

Finally, the instantiation of a derived Class Object as in

       char *s; Derived d(s);
generates:

             .loc 2 8 0
             movq -24(%rbp), %rsi            # s, s
             leaq -64(%rbp), %rdi            #, tmp59
       .LEHB0:
             call _ZN7DerivedC1EPc           #      => Derived::Derived()

This means that the functions are called respectively as we had expected.

Building the instantiation in release mode, we see the following:

       .LCFI2:
             testq %rdi, %rdi # s
             movq %rsp, %rbp #, tmp114
             je    .L3   #,
             call strlen       #          => strlen()
       .L3:
       .L5:
             leaq 1(%rax), %rdi     #, tmp67
             movq %rax, (%rbp)      # tmp63, <variable>.nameLen
       .LEHB0:
             call _Znam #                 => operator new()
       .LEHE0:
             movq %rbx, %rsi # s, nameStr
             movq %rax, %rdi #, <anonymous>
             call strcpy       #          => strcpy()
             movq %rax, 8(%rbp)     # tmp70, <variable>.name
             movq 8(%rsp), %rdi     # <variable>.name,
       <variable>.name
             movq %rbp, 16(%rbp)    # tmp114, <variable>.myBase
             testq %rdi, %rdi # <variable>.name
             jne   .L44 #,

Thus we see that the Derived (and Base) Constructor calls have been totally
optimized.

The above is illustrated in terms of the constructors. Similar optimizations do
(mostly) take place for (non-virtual) destructors, copy constructor, copy assignment
operator, all non-virtual inline member functions and all inline global functions.

Only virtual member functions are not inlined as their call sequence is runtime
dependent.

The same behavior is observed in case of Get / Set Methods.
class Base
{
      …
      char *GetName() const { return name; }
      void SetName(char *nameStr)
      {
            if (nameStr)
            {
                  if (name)
                  {
                         delete [] name;
                  }

                     name =
                     strcpy(new char[strlen(nameStr)+1], nameStr);
              }
       }
       …
};

The assemblies for the Get / Set Methods are shown below:
_ZNK4Base7GetNameEv:               => Base::GetName()
.LFB1448:
      .loc 3 29 0
      pushq %rbp #
.LCFI17:
      movq %rsp, %rbp #,
.LCFI18:
      movq %rdi, -8(%rbp)    # this, this
.LBB11:
      .loc 3 29 0
      movq -8(%rbp), %rax    # this, this
      movq 8(%rax), %rax     # <variable>.name,
<variable>.name
.LBE11:
      leave
      ret

_ZN4Base7SetNameEPc:               => Base::SetName()
.LFB1449:
      .loc 3 31 0
      pushq %rbp #
.LCFI13:
      movq %rsp, %rbp #,
.LCFI14:
      pushq %rbx #
.LCFI15:
      subq $24, %rsp   #,
.LCFI16:
      movq %rdi, -16(%rbp)   # this, this
      movq %rsi, -24(%rbp)   # nameStr, nameStr
.LBB10:
      .loc 3 32 0
      cmpq $0, -24(%rbp)     #, nameStr
      je    .L29 #,
      .loc 3 34 0
      movq -16(%rbp), %rax   # this, this
      cmpq $0, 8(%rax) #, <variable>.name
      je    .L31 #,
      .loc 3 36 0
      movq -16(%rbp), %rax   # this, this
      cmpq $0, 8(%rax) #, <variable>.name
      je    .L31 #,
      movq -16(%rbp), %rax   # this, this
      movq 8(%rax), %rdi     # <variable>.name,
<variable>.name
      call _ZdaPv      #           => operator delete()
.L31:
      .loc 3 39 0
      movq -16(%rbp), %rbx   # this, this
      movq -24(%rbp), %rdi   # nameStr, nameStr
      call strlen      #           => strlen()
      movq %rax, %rdi # tmp65, tmp63
      incq %rdi # tmp63
      call _Znam #                 => operatot new()
      movq %rax, %rdi #, tmp66
      movq -24(%rbp), %rsi   # nameStr, nameStr
      call strcpy      #           => strcpy()
      movq %rax, 8(%rbx)     #, <variable>.name
.L29:
.LBE10:
      .loc 3 41 0
      addq $24, %rsp   #,
      popq %rbx #
      leave
      ret
The calls,

               char *oldName = d.GetName();
               char *newName = "My Gang";
               d.SetName(newName);

generate the following assembly in debug build:

       .LEHE0:
             .loc    2 11 0
             leaq     -64(%rbp), %rdi   #, tmp60
             call     _ZNK4Base7GetNameEv     #     => Base::GetName()
             movq     %rax, -72(%rbp)   # tmp61, oldName
             .loc    2 12 0
             movq     $.LC0, -80(%rbp) #, newName
             .loc    2 13 0
             movq     -80(%rbp), %rsi   # newName, newName
             leaq     -64(%rbp), %rdi   #, tmp63
       .LEHB1:
             call     _ZN4Base7SetNameEPc    #      => Base::SetName()
             .loc    2 16 0
             movq     $.LC1, -80(%rbp) #, newName


The same in release build is:

       .L10:
             movl     $.LC0, %edi #, nameStr
             call     strlen      #                 => strlen()
             leaq     1(%rax), %rdi     #, tmp80
       .LEHB1:
             call     _Znam #                       => operator new()
             movq     %rax, %rdi #, tmp85
             movl     $.LC0, %esi #, nameStr
             call     strcpy      #                 => strcpy()
             testq    %rax, %rax # tmp86
             movq     %rax, 8(%rsp)     # tmp86, <variable>.name
             je       .L14 #,
             movq     %rax, %rdi # tmp86, <variable>.name
             call     _ZdaPv      #                 => operator delete()
             movl     $.LC1, %edi #, newName
             call     strlen      #
             leaq     1(%rax), %rdi     #, tmp90
             call     _Znam #                       => operator new()
             movq     %rax, %rdi #, tmp95
             movl     $.LC1, %esi #, newName
             call     strcpy      #                 => strcpy()
             movq     %rax, 8(%rsp)     # tmp96, <variable>.name

More Related Content

What's hot

The Ring programming language version 1.5.4 book - Part 78 of 185
The Ring programming language version 1.5.4 book - Part 78 of 185The Ring programming language version 1.5.4 book - Part 78 of 185
The Ring programming language version 1.5.4 book - Part 78 of 185Mahmoud Samir Fayed
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisaujihisa
 
What's new in PHP 5.5
What's new in PHP 5.5What's new in PHP 5.5
What's new in PHP 5.5Tom Corrigan
 
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationRedis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationrjsmelo
 
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code styleRuby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code styleAnton Shemerey
 
SfCon: Test Driven Development
SfCon: Test Driven DevelopmentSfCon: Test Driven Development
SfCon: Test Driven DevelopmentAugusto Pascutti
 
ekb.py - Python VS ...
ekb.py - Python VS ...ekb.py - Python VS ...
ekb.py - Python VS ...it-people
 
Meet up symfony 16 juin 2017 - Les PSR
Meet up symfony 16 juin 2017 -  Les PSRMeet up symfony 16 juin 2017 -  Les PSR
Meet up symfony 16 juin 2017 - Les PSRJulien Vinber
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)ujihisa
 
Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!Workhorse Computing
 
Interceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalInterceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalKent Ohashi
 
Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2osfameron
 
The Ring programming language version 1.10 book - Part 93 of 212
The Ring programming language version 1.10 book - Part 93 of 212The Ring programming language version 1.10 book - Part 93 of 212
The Ring programming language version 1.10 book - Part 93 of 212Mahmoud Samir Fayed
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The AnswerIan Barber
 
CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11Combell NV
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Workhorse Computing
 
Créer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heureCréer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heureAmaury Bouchard
 
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Workhorse Computing
 

What's hot (20)

The Ring programming language version 1.5.4 book - Part 78 of 185
The Ring programming language version 1.5.4 book - Part 78 of 185The Ring programming language version 1.5.4 book - Part 78 of 185
The Ring programming language version 1.5.4 book - Part 78 of 185
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisa
 
What's new in PHP 5.5
What's new in PHP 5.5What's new in PHP 5.5
What's new in PHP 5.5
 
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationRedis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your application
 
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code styleRuby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
 
SfCon: Test Driven Development
SfCon: Test Driven DevelopmentSfCon: Test Driven Development
SfCon: Test Driven Development
 
ekb.py - Python VS ...
ekb.py - Python VS ...ekb.py - Python VS ...
ekb.py - Python VS ...
 
Meet up symfony 16 juin 2017 - Les PSR
Meet up symfony 16 juin 2017 -  Les PSRMeet up symfony 16 juin 2017 -  Les PSR
Meet up symfony 16 juin 2017 - Les PSR
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)
 
Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!
 
Interceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalInterceptors: Into the Core of Pedestal
Interceptors: Into the Core of Pedestal
 
Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2
 
C99
C99C99
C99
 
The Ring programming language version 1.10 book - Part 93 of 212
The Ring programming language version 1.10 book - Part 93 of 212The Ring programming language version 1.10 book - Part 93 of 212
The Ring programming language version 1.10 book - Part 93 of 212
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
 
CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
Créer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heureCréer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heure
 
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
 
Memory Manglement in Raku
Memory Manglement in RakuMemory Manglement in Raku
Memory Manglement in Raku
 

Viewers also liked

NDL @ YOJANA
NDL @ YOJANANDL @ YOJANA
NDL @ YOJANAppd1961
 
How To Define An Integer Constant In C
How To Define An Integer Constant In CHow To Define An Integer Constant In C
How To Define An Integer Constant In Cppd1961
 
Small Basic Language
Small Basic LanguageSmall Basic Language
Small Basic LanguageLynn Langit
 
2.3 exploring shapes
2.3   exploring shapes2.3   exploring shapes
2.3 exploring shapesallenbailey
 
1.1 introduction to small basic
1.1   introduction to small basic1.1   introduction to small basic
1.1 introduction to small basicallenbailey
 
4.1 playing with shapes
4.1   playing with shapes4.1   playing with shapes
4.1 playing with shapesallenbailey
 
3.3 the math object
3.3   the math object3.3   the math object
3.3 the math objectallenbailey
 
3.4 events and interactivity
3.4   events and interactivity3.4   events and interactivity
3.4 events and interactivityallenbailey
 
Science & Culture Article with Editorial & Cover
Science & Culture Article with Editorial & CoverScience & Culture Article with Editorial & Cover
Science & Culture Article with Editorial & Coverppd1961
 
LONG-TERM PRESERVATION OF 3D ARCHITECTURAL BUILDING DATA: A LITERATURE REVIEW
LONG-TERM PRESERVATION OF 3D ARCHITECTURAL BUILDING DATA: A LITERATURE REVIEWLONG-TERM PRESERVATION OF 3D ARCHITECTURAL BUILDING DATA: A LITERATURE REVIEW
LONG-TERM PRESERVATION OF 3D ARCHITECTURAL BUILDING DATA: A LITERATURE REVIEWMoutaz Haddara
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++K Durga Prasad
 
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREC & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREjatin batra
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programmingSachin Sharma
 
Lab manual of C++
Lab manual of C++Lab manual of C++
Lab manual of C++thesaqib
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language Mohamed Loey
 
Intro. to prog. c++
Intro. to prog. c++Intro. to prog. c++
Intro. to prog. c++KurdGul
 

Viewers also liked (20)

NDL @ YOJANA
NDL @ YOJANANDL @ YOJANA
NDL @ YOJANA
 
How To Define An Integer Constant In C
How To Define An Integer Constant In CHow To Define An Integer Constant In C
How To Define An Integer Constant In C
 
Small Basic Language
Small Basic LanguageSmall Basic Language
Small Basic Language
 
C++11
C++11C++11
C++11
 
2.3 exploring shapes
2.3   exploring shapes2.3   exploring shapes
2.3 exploring shapes
 
1.1 introduction to small basic
1.1   introduction to small basic1.1   introduction to small basic
1.1 introduction to small basic
 
4.1 playing with shapes
4.1   playing with shapes4.1   playing with shapes
4.1 playing with shapes
 
3.3 the math object
3.3   the math object3.3   the math object
3.3 the math object
 
3.4 events and interactivity
3.4   events and interactivity3.4   events and interactivity
3.4 events and interactivity
 
Science & Culture Article with Editorial & Cover
Science & Culture Article with Editorial & CoverScience & Culture Article with Editorial & Cover
Science & Culture Article with Editorial & Cover
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
LONG-TERM PRESERVATION OF 3D ARCHITECTURAL BUILDING DATA: A LITERATURE REVIEW
LONG-TERM PRESERVATION OF 3D ARCHITECTURAL BUILDING DATA: A LITERATURE REVIEWLONG-TERM PRESERVATION OF 3D ARCHITECTURAL BUILDING DATA: A LITERATURE REVIEW
LONG-TERM PRESERVATION OF 3D ARCHITECTURAL BUILDING DATA: A LITERATURE REVIEW
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREC & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Lab manual of C++
Lab manual of C++Lab manual of C++
Lab manual of C++
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
Intro. to prog. c++
Intro. to prog. c++Intro. to prog. c++
Intro. to prog. c++
 

Similar to Function Call Optimization

Implement an MPI program to perform matrix-matrix multiplication AB .pdf
Implement an MPI program to perform matrix-matrix multiplication AB .pdfImplement an MPI program to perform matrix-matrix multiplication AB .pdf
Implement an MPI program to perform matrix-matrix multiplication AB .pdfmeerobertsonheyde608
 
Write a program in MIPS that reads in a Roman form number from th.pdf
Write a program in MIPS that reads in a Roman form number from th.pdfWrite a program in MIPS that reads in a Roman form number from th.pdf
Write a program in MIPS that reads in a Roman form number from th.pdfsantanadenisesarin13
 
Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)ujihisa
 
Answer1).LC0 .string Enter the value of N .LC1 .st.pdf
Answer1).LC0 .string Enter the value of N  .LC1 .st.pdfAnswer1).LC0 .string Enter the value of N  .LC1 .st.pdf
Answer1).LC0 .string Enter the value of N .LC1 .st.pdfannaiwatertreatment
 
AnswerAssembly Language Code .zero 1 .LC0 .string Enter.pdf
AnswerAssembly Language Code .zero 1 .LC0 .string Enter.pdfAnswerAssembly Language Code .zero 1 .LC0 .string Enter.pdf
AnswerAssembly Language Code .zero 1 .LC0 .string Enter.pdfarccreation001
 
WCTF 2018 binja Editorial
WCTF 2018 binja EditorialWCTF 2018 binja Editorial
WCTF 2018 binja EditorialCharo_IT
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
Fix assembly Code new peoblem on call printf gt .pdf
Fix assembly Code new peoblem on call printf gt  .pdfFix assembly Code new peoblem on call printf gt  .pdf
Fix assembly Code new peoblem on call printf gt .pdfrchopra4
 
Itroroduction to R language
Itroroduction to R languageItroroduction to R language
Itroroduction to R languagechhabria-nitesh
 
Meta-objective Lisp @名古屋 Reject 会議
Meta-objective Lisp @名古屋 Reject 会議Meta-objective Lisp @名古屋 Reject 会議
Meta-objective Lisp @名古屋 Reject 会議dico_leque
 
Write an MPI program that implements a shell-sort like parallel algo.pdf
Write an MPI program that implements a shell-sort like parallel algo.pdfWrite an MPI program that implements a shell-sort like parallel algo.pdf
Write an MPI program that implements a shell-sort like parallel algo.pdfbharatchawla141
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]RootedCON
 
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)Phil Calçado
 
Native interfaces for R
Native interfaces for RNative interfaces for R
Native interfaces for RSeth Falcon
 
Call Return Exploration
Call Return ExplorationCall Return Exploration
Call Return ExplorationPat Hawks
 
Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lispkyleburton
 

Similar to Function Call Optimization (20)

Implement an MPI program to perform matrix-matrix multiplication AB .pdf
Implement an MPI program to perform matrix-matrix multiplication AB .pdfImplement an MPI program to perform matrix-matrix multiplication AB .pdf
Implement an MPI program to perform matrix-matrix multiplication AB .pdf
 
Write a program in MIPS that reads in a Roman form number from th.pdf
Write a program in MIPS that reads in a Roman form number from th.pdfWrite a program in MIPS that reads in a Roman form number from th.pdf
Write a program in MIPS that reads in a Roman form number from th.pdf
 
Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)
 
Answer1).LC0 .string Enter the value of N .LC1 .st.pdf
Answer1).LC0 .string Enter the value of N  .LC1 .st.pdfAnswer1).LC0 .string Enter the value of N  .LC1 .st.pdf
Answer1).LC0 .string Enter the value of N .LC1 .st.pdf
 
AnswerAssembly Language Code .zero 1 .LC0 .string Enter.pdf
AnswerAssembly Language Code .zero 1 .LC0 .string Enter.pdfAnswerAssembly Language Code .zero 1 .LC0 .string Enter.pdf
AnswerAssembly Language Code .zero 1 .LC0 .string Enter.pdf
 
WCTF 2018 binja Editorial
WCTF 2018 binja EditorialWCTF 2018 binja Editorial
WCTF 2018 binja Editorial
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Fix assembly Code new peoblem on call printf gt .pdf
Fix assembly Code new peoblem on call printf gt  .pdfFix assembly Code new peoblem on call printf gt  .pdf
Fix assembly Code new peoblem on call printf gt .pdf
 
Itroroduction to R language
Itroroduction to R languageItroroduction to R language
Itroroduction to R language
 
Meta-objective Lisp @名古屋 Reject 会議
Meta-objective Lisp @名古屋 Reject 会議Meta-objective Lisp @名古屋 Reject 会議
Meta-objective Lisp @名古屋 Reject 会議
 
Write an MPI program that implements a shell-sort like parallel algo.pdf
Write an MPI program that implements a shell-sort like parallel algo.pdfWrite an MPI program that implements a shell-sort like parallel algo.pdf
Write an MPI program that implements a shell-sort like parallel algo.pdf
 
ROP
ROPROP
ROP
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
 
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
 
R/C++ talk at earl 2014
R/C++ talk at earl 2014R/C++ talk at earl 2014
R/C++ talk at earl 2014
 
Native interfaces for R
Native interfaces for RNative interfaces for R
Native interfaces for R
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
Call Return Exploration
Call Return ExplorationCall Return Exploration
Call Return Exploration
 
Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lisp
 

More from ppd1961

Land of Pyramids, Petra, and Prayers - Egypt, Jordan, and Israel Tour
Land of Pyramids, Petra, and Prayers - Egypt, Jordan, and Israel TourLand of Pyramids, Petra, and Prayers - Egypt, Jordan, and Israel Tour
Land of Pyramids, Petra, and Prayers - Egypt, Jordan, and Israel Tourppd1961
 
Unified Modeling Language (UML)
Unified Modeling Language (UML)Unified Modeling Language (UML)
Unified Modeling Language (UML)ppd1961
 
OOP in C++
OOP in C++OOP in C++
OOP in C++ppd1961
 
Digital geometry - An introduction
Digital geometry  - An introductionDigital geometry  - An introduction
Digital geometry - An introductionppd1961
 
Innovation in technology
Innovation in technologyInnovation in technology
Innovation in technologyppd1961
 
Kinectic vision looking deep into depth
Kinectic vision   looking deep into depthKinectic vision   looking deep into depth
Kinectic vision looking deep into depthppd1961
 
Stl Containers
Stl ContainersStl Containers
Stl Containersppd1961
 
Object Lifetime In C C++
Object Lifetime In C C++Object Lifetime In C C++
Object Lifetime In C C++ppd1961
 
Technical Documentation By Techies
Technical Documentation By TechiesTechnical Documentation By Techies
Technical Documentation By Techiesppd1961
 
Vlsi Education In India
Vlsi Education In IndiaVlsi Education In India
Vlsi Education In Indiappd1961
 
Reconfigurable Computing
Reconfigurable ComputingReconfigurable Computing
Reconfigurable Computingppd1961
 
Women In Engineering Panel Discussion
Women In Engineering   Panel DiscussionWomen In Engineering   Panel Discussion
Women In Engineering Panel Discussionppd1961
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2ppd1961
 
Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]ppd1961
 
Dimensions of Offshore Technology Services
Dimensions of Offshore Technology ServicesDimensions of Offshore Technology Services
Dimensions of Offshore Technology Servicesppd1961
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming LanguagesConcepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languagesppd1961
 
Glimpses of C++0x
Glimpses of C++0xGlimpses of C++0x
Glimpses of C++0xppd1961
 
Quality - A Priority In Service Engagements
Quality - A Priority In Service EngagementsQuality - A Priority In Service Engagements
Quality - A Priority In Service Engagementsppd1961
 
Design Patterns
Design PatternsDesign Patterns
Design Patternsppd1961
 
Singleton Object Management
Singleton Object ManagementSingleton Object Management
Singleton Object Managementppd1961
 

More from ppd1961 (20)

Land of Pyramids, Petra, and Prayers - Egypt, Jordan, and Israel Tour
Land of Pyramids, Petra, and Prayers - Egypt, Jordan, and Israel TourLand of Pyramids, Petra, and Prayers - Egypt, Jordan, and Israel Tour
Land of Pyramids, Petra, and Prayers - Egypt, Jordan, and Israel Tour
 
Unified Modeling Language (UML)
Unified Modeling Language (UML)Unified Modeling Language (UML)
Unified Modeling Language (UML)
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
 
Digital geometry - An introduction
Digital geometry  - An introductionDigital geometry  - An introduction
Digital geometry - An introduction
 
Innovation in technology
Innovation in technologyInnovation in technology
Innovation in technology
 
Kinectic vision looking deep into depth
Kinectic vision   looking deep into depthKinectic vision   looking deep into depth
Kinectic vision looking deep into depth
 
Stl Containers
Stl ContainersStl Containers
Stl Containers
 
Object Lifetime In C C++
Object Lifetime In C C++Object Lifetime In C C++
Object Lifetime In C C++
 
Technical Documentation By Techies
Technical Documentation By TechiesTechnical Documentation By Techies
Technical Documentation By Techies
 
Vlsi Education In India
Vlsi Education In IndiaVlsi Education In India
Vlsi Education In India
 
Reconfigurable Computing
Reconfigurable ComputingReconfigurable Computing
Reconfigurable Computing
 
Women In Engineering Panel Discussion
Women In Engineering   Panel DiscussionWomen In Engineering   Panel Discussion
Women In Engineering Panel Discussion
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2
 
Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]
 
Dimensions of Offshore Technology Services
Dimensions of Offshore Technology ServicesDimensions of Offshore Technology Services
Dimensions of Offshore Technology Services
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming LanguagesConcepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languages
 
Glimpses of C++0x
Glimpses of C++0xGlimpses of C++0x
Glimpses of C++0x
 
Quality - A Priority In Service Engagements
Quality - A Priority In Service EngagementsQuality - A Priority In Service Engagements
Quality - A Priority In Service Engagements
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Singleton Object Management
Singleton Object ManagementSingleton Object Management
Singleton Object Management
 

Function Call Optimization

  • 1. Function Call Optimization This note describes the observations on the following Function Calls: 1. Constructor call 2. Constructor call of Base Class 3. Get / Set Methods. To Test for the above a sample Base Class has been developed. class Base { public: size_t nameLen; char *name; protected: Base(char *nameStr): nameLen((nameStr)? strlen(nameStr): 0), name(strcpy(new char[nameLen+1], nameStr)) {} … }; This generates the following assembly in debug build using g++. The function calls have been marked in red. _ZN4BaseC2EPc: => Base::Base() .LFB1443: .loc 3 17 0 pushq %rbp # .LCFI22: movq %rsp, %rbp #, .LCFI23: pushq %rbx # .LCFI24: subq $40, %rsp #, .LCFI25: movq %rdi, -16(%rbp) # this, this movq %rsi, -24(%rbp) # nameStr, nameStr .LBB13: .loc 3 20 0 movq -16(%rbp), %rax # this, movq %rax, -32(%rbp) #, cmpq $0, -24(%rbp) #, nameStr je .L39 #, movq -24(%rbp), %rdi # nameStr, nameStr call strlen # => strlen() movq %rax, -40(%rbp) # tmp61, jmp .L40 # .L39: movq $0, -40(%rbp) #,
  • 2. .L40: movq -40(%rbp), %rax #, movq -32(%rbp), %rdx #, movq %rax, (%rdx) #, <variable>.nameLen movq -16(%rbp), %rbx # this, this movq -16(%rbp), %rax # this, this movq (%rax), %rdi # <variable>.nameLen, tmp63 incq %rdi # tmp63 call _Znam # => operator new() movq %rax, %rdi #, tmp65 movq -24(%rbp), %rsi # nameStr, nameStr call strcpy # => strcpy() movq %rax, 8(%rbx) #, <variable>.name .LBE13: addq $40, %rsp #, popq %rbx # leave ret The Derived Class, class Derived: public Base { Base *myBase; public: Derived(char *name): Base(name), myBase((Base*)this) {} … }; generates the following assembly: _ZN7DerivedC1EPc: => Derived::Derived() .LFB1452: .loc 3 53 0 pushq %rbp # .LCFI19: movq %rsp, %rbp #, .LCFI20: subq $16, %rsp #, .LCFI21: movq %rdi, -8(%rbp) # this, this movq %rsi, -16(%rbp) # name, name .LBB12: .loc 3 56 0 movq -16(%rbp), %rsi # name, name movq -8(%rbp), %rdi # this, this call _ZN4BaseC2EPc # => Base::Base() movq -8(%rbp), %rdx # this, this movq -8(%rbp), %rax # this, this movq %rax, 16(%rdx) # this, <variable>.myBase .LBE12: leave ret Finally, the instantiation of a derived Class Object as in char *s; Derived d(s);
  • 3. generates: .loc 2 8 0 movq -24(%rbp), %rsi # s, s leaq -64(%rbp), %rdi #, tmp59 .LEHB0: call _ZN7DerivedC1EPc # => Derived::Derived() This means that the functions are called respectively as we had expected. Building the instantiation in release mode, we see the following: .LCFI2: testq %rdi, %rdi # s movq %rsp, %rbp #, tmp114 je .L3 #, call strlen # => strlen() .L3: .L5: leaq 1(%rax), %rdi #, tmp67 movq %rax, (%rbp) # tmp63, <variable>.nameLen .LEHB0: call _Znam # => operator new() .LEHE0: movq %rbx, %rsi # s, nameStr movq %rax, %rdi #, <anonymous> call strcpy # => strcpy() movq %rax, 8(%rbp) # tmp70, <variable>.name movq 8(%rsp), %rdi # <variable>.name, <variable>.name movq %rbp, 16(%rbp) # tmp114, <variable>.myBase testq %rdi, %rdi # <variable>.name jne .L44 #, Thus we see that the Derived (and Base) Constructor calls have been totally optimized. The above is illustrated in terms of the constructors. Similar optimizations do (mostly) take place for (non-virtual) destructors, copy constructor, copy assignment operator, all non-virtual inline member functions and all inline global functions. Only virtual member functions are not inlined as their call sequence is runtime dependent. The same behavior is observed in case of Get / Set Methods.
  • 4. class Base { … char *GetName() const { return name; } void SetName(char *nameStr) { if (nameStr) { if (name) { delete [] name; } name = strcpy(new char[strlen(nameStr)+1], nameStr); } } … }; The assemblies for the Get / Set Methods are shown below:
  • 5. _ZNK4Base7GetNameEv: => Base::GetName() .LFB1448: .loc 3 29 0 pushq %rbp # .LCFI17: movq %rsp, %rbp #, .LCFI18: movq %rdi, -8(%rbp) # this, this .LBB11: .loc 3 29 0 movq -8(%rbp), %rax # this, this movq 8(%rax), %rax # <variable>.name, <variable>.name .LBE11: leave ret _ZN4Base7SetNameEPc: => Base::SetName() .LFB1449: .loc 3 31 0 pushq %rbp # .LCFI13: movq %rsp, %rbp #, .LCFI14: pushq %rbx # .LCFI15: subq $24, %rsp #, .LCFI16: movq %rdi, -16(%rbp) # this, this movq %rsi, -24(%rbp) # nameStr, nameStr .LBB10: .loc 3 32 0 cmpq $0, -24(%rbp) #, nameStr je .L29 #, .loc 3 34 0 movq -16(%rbp), %rax # this, this cmpq $0, 8(%rax) #, <variable>.name je .L31 #, .loc 3 36 0 movq -16(%rbp), %rax # this, this cmpq $0, 8(%rax) #, <variable>.name je .L31 #, movq -16(%rbp), %rax # this, this movq 8(%rax), %rdi # <variable>.name, <variable>.name call _ZdaPv # => operator delete() .L31: .loc 3 39 0 movq -16(%rbp), %rbx # this, this movq -24(%rbp), %rdi # nameStr, nameStr call strlen # => strlen() movq %rax, %rdi # tmp65, tmp63 incq %rdi # tmp63 call _Znam # => operatot new() movq %rax, %rdi #, tmp66 movq -24(%rbp), %rsi # nameStr, nameStr call strcpy # => strcpy() movq %rax, 8(%rbx) #, <variable>.name .L29: .LBE10: .loc 3 41 0 addq $24, %rsp #, popq %rbx # leave ret
  • 6. The calls, char *oldName = d.GetName(); char *newName = "My Gang"; d.SetName(newName); generate the following assembly in debug build: .LEHE0: .loc 2 11 0 leaq -64(%rbp), %rdi #, tmp60 call _ZNK4Base7GetNameEv # => Base::GetName() movq %rax, -72(%rbp) # tmp61, oldName .loc 2 12 0 movq $.LC0, -80(%rbp) #, newName .loc 2 13 0 movq -80(%rbp), %rsi # newName, newName leaq -64(%rbp), %rdi #, tmp63 .LEHB1: call _ZN4Base7SetNameEPc # => Base::SetName() .loc 2 16 0 movq $.LC1, -80(%rbp) #, newName The same in release build is: .L10: movl $.LC0, %edi #, nameStr call strlen # => strlen() leaq 1(%rax), %rdi #, tmp80 .LEHB1: call _Znam # => operator new() movq %rax, %rdi #, tmp85 movl $.LC0, %esi #, nameStr call strcpy # => strcpy() testq %rax, %rax # tmp86 movq %rax, 8(%rsp) # tmp86, <variable>.name je .L14 #, movq %rax, %rdi # tmp86, <variable>.name call _ZdaPv # => operator delete() movl $.LC1, %edi #, newName call strlen # leaq 1(%rax), %rdi #, tmp90 call _Znam # => operator new() movq %rax, %rdi #, tmp95 movl $.LC1, %esi #, newName call strcpy # => strcpy() movq %rax, 8(%rsp) # tmp96, <variable>.name