CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2010
    Posts
    105

    Question g++ Linker Order and Optimization?

    I have been using NetBeans IDE to build my program with its auto generated make file. Then I noticed the binary file generated depends on the order of linking. For example
    Code:
    g++ -static -o myprogram a.o b.o c.o d.o e.o -la -lb -lc
    ,
    Code:
    g++ -static -o myprogram a.o b.o c.o d.o e.o -lb -la -lc
    (change order of libraries), and
    Code:
    g++ -static -o myprogram c.o d.o e.o a.o b.o -la -lb -lc
    (change order of object files) all produce different binaries, even though the libraries and object files are all identical in each of the three cases.

    So here are my questions:
    How does NetBeans decide the order of linking and how to change it?

    Does the order of linking affect performance and if so how to optimize it?

    Thank you!

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: g++ Linker Order and Optimization?

    My guess is that it's just putting the chunks in the order you specify, I'll bet it has no affect on performance. If you want to optimize where code is located, then you can specify hot and cold spots. "hot" code will always be placed together and "cold" code will be scattered where ever the compiler sees fit.

    Code:
    //this method will be placed with other hot methods
    void foo(void) __attribute__ ((hot));
    void foo(void){
       //implementation
    }
    
    //this will be put where ever
    void bar(void) __attribute__ ((cold));
    void bar(void){
       //implementation
    }
    Of course this does not effect linking, it only applies to locality within the object file (unless you do linking optimizations, which I'm pretty sure is only applicable to pure C, not C++) Hot/cold code in my experience produces code between 0 and 15% faster. Sometimes the best optimization that you can do is compiling with -Os. -O3 is usually thought to be the fastest, but for small code, I've never found that to be the case, code locality is a much bigger optimization than anything -O3 does (for small programs.) It's because of how the processor caches instructions, when it loads instructions, it loads the instructions around it too, and navigating RAM is much much slower than navigating L2 cache.

    Another localization optimization you can do is manual branch prediction. If you have
    Code:
    if (x){
       //code set 1
    } else {
       //code set 2
    }
    The compiler has no idea which case is the common one, if you do, you can tell g++ that and it will compile better. Say code set 1 is much more likely, if the compiler decided to make that a jump, it's going to be slower than if code set 1 immediately followed the if and it made code set 2 take the jump.

    Code:
    if (__builtin_expect((long)(x),1)){
       //code set 1
    } else {
       //code set 2
    }
    I've seen this produce heavy speed improvements too.

    NOTE: both branch prediction and hot spots are g++ specific, I recommend if you decide to do this optimization, to make macros for them and do a check for the __GNUC__ definition.
    Last edited by ninja9578; April 20th, 2012 at 03:40 PM.

  3. #3
    Join Date
    Nov 2010
    Posts
    105

    Re: g++ Linker Order and Optimization?

    Thank you!

  4. #4
    Join Date
    Nov 2010
    Posts
    105

    Question NetBeans Linker Order?

    Quote Originally Posted by acppdummy View Post
    ...
    How does NetBeans decide the order of linking and how to change it?...
    Any NetBeans user out there that knows the trick? Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured