C++ with GCC: optimizing for size

Discussion about Parallella (and Epiphany) Software Development

Moderators: amylaar, jeremybennett, simoncook

C++ with GCC: optimizing for size

Postby Gravis » Fri Oct 24, 2014 6:07 pm

I recently found myself pondering what made the object code of a class so large (38KB with -Os) and what i found was surprising.

  • vtables are huge
    my class inherits a simple base but that simple base used 24 KB for two pure virtual functions
    since they are not actually required to be in the base, i commented them out and suddenly the object code could fit on an ecore.

  • inline functions may save you space
    calling a function requires that you push and pop stack data, so relatively short functions that are sparsely used should be inlined. just add "inline" before it's the function's implementation.
    beware, if it's a commonly used function, it will cause the binary output to balloon.

  • template arguments can reduce code size (but not by much).
    don't avoid template arguments if they may be used in place of parameters.

  • -Os doesn't always generate the smallest code
    in my particular case, -O1 resulted in 100B smaller code than -Os

i kid you not when i say my object code started at 38KB and with these minor tweaks, it is now 3.8KB, an order of magnitude smaller! do note that
i recommend testing each instance of the tweaks individually because sometimes a tweak can grow the object code.

this just goes to show that compilers are still stupid and the keyword register is still needed.
User avatar
Gravis
 
Posts: 445
Joined: Mon Dec 17, 2012 3:27 am
Location: East coast USA.

Re: C++ with GCC: optimizing for size

Postby jeremybennett » Sun Oct 26, 2014 5:09 pm

Hi Gravis,

All fair comments. C++ is inherently a bigger language than C - it's only just possible on Epiphany. You really are better sticking to C.

I'm not sure how much the inline keyword will do for you. It is advisory only. You are probably better looking at the command line options which control inlining.

Its not at all unusual to get smaller code with -O1 than -Os. -Os is just -O2 with a few optimizations that tend to increase size disabled. There is no GCC equivalent of LLVM's -Oz.
User avatar
jeremybennett
 
Posts: 61
Joined: Mon Dec 17, 2012 9:06 am

Re: C++ with GCC: optimizing for size

Postby Gravis » Tue Oct 28, 2014 10:08 am

jeremybennett wrote:C++ is inherently a bigger language than C - it's only just possible on Epiphany.

C++ (sans STL) results in generated binaries no larger than C (sans C Standard Library). The reason being that C++ is merely a syntactic simplification of things you can do in C using pointers and preprocessor tomfoolery. C++'s ability to hide function calls (encapsulation) and code generation (templates) behind deceptively complex objects is probably the reason many people think C++ is a resource hog. However, if you know what your code really does then you understand how something so small is really quite large and thus can avoid it.

jeremybennett wrote:You really are better sticking to C.

switching to C is only a benefit if you have a single instance of an object.

jeremybennett wrote:I'm not sure how much the inline keyword will do for you. It is advisory only. You are probably better looking at the command line options which control inlining.

i'm aware that inline and register are advisory but with gcc they are honored unless you override it with command line options or in some extreme situations.

jeremybennett wrote:Its not at all unusual to get smaller code with -O1 than -Os. -Os is just -O2 with a few optimizations that tend to increase size disabled. There is no GCC equivalent of LLVM's -Oz.

good to know!
User avatar
Gravis
 
Posts: 445
Joined: Mon Dec 17, 2012 3:27 am
Location: East coast USA.

Re: C++ with GCC: optimizing for size

Postby notzed » Tue Oct 28, 2014 10:16 pm

C++ isn't just C with "simpler syntax" (rofl), it's a completely different language. Historical evidence (and indeed, your complaints) suggests it's also a very bulky one.
notzed
 
Posts: 331
Joined: Mon Dec 17, 2012 12:28 am
Location: Australia

Re: C++ with GCC: optimizing for size

Postby Gravis » Wed Oct 29, 2014 12:51 pm

notzed wrote:C++ isn't just C with "simpler syntax" (rofl), it's a completely different language.

it's an expansion of the C language. however, all of it's abilities can be emulated using the preprocessor. it's cumbersome but C can be hacked to do it.

notzed wrote:Historical evidence (and indeed, your complaints) suggests it's also a very bulky one.

That's a great example of a specious argument. What i have pointed out shows that GCC does not not have a perfect C++ compiler but there are ways to work around it's imperfections. An (attempted) implementation of a standardized specification does not always reflect the specification itself.
User avatar
Gravis
 
Posts: 445
Joined: Mon Dec 17, 2012 3:27 am
Location: East coast USA.

Re: C++ with GCC: optimizing for size

Postby jeremybennett » Wed Oct 29, 2014 4:32 pm

Hi notzed, Gravis,

C++ is truly a separate language, it draws its inspiration from two languages, C (of course) and Simula 67. Which is why C++ compilers are not just pre-processors for C.

C++ defines a richer semantics, and implementing that richer semantics will often result in larger code. However if you keep to C like semantics, then on the whole the code size will be the same, as Gravis found out.

The real killer for C++ is that it defines a very large standard libary (the STL). This is where your really big memory growth will happen.

Just one thing to correct from an earlier comment. You can implement an OO design in C, you just need to follow suitable coding discipline. It is quite common to use UML when designing and then implement in C. However C++ provides linguistic structures to make OO coding easier, and these can sometimes prove more efficient when compiled than writing the implementation in C.

HTH,


Jeremy
User avatar
jeremybennett
 
Posts: 61
Joined: Mon Dec 17, 2012 9:06 am

Re: C++ with GCC: optimizing for size

Postby xilman » Sun Nov 02, 2014 8:33 pm

jeremybennett wrote:Just one thing to correct from an earlier comment. You can implement an OO design in C, you just need to follow suitable coding discipline.
Yet another example of the general statement: you can implement anything in anything, given a Turing complete implementation language.

Sometimes the discipline required becomes too onerous.
xilman
 
Posts: 80
Joined: Sat May 10, 2014 8:10 pm
Location: UK

Re: C++ with GCC: optimizing for size

Postby notzed » Sun Nov 02, 2014 10:00 pm

xilman wrote:
jeremybennett wrote:Just one thing to correct from an earlier comment. You can implement an OO design in C, you just need to follow suitable coding discipline.
Yet another example of the general statement: you can implement anything in anything, given a Turing complete implementation language.

Sometimes the discipline required becomes too onerous.


Well of course it's "an example of a general statement" as OO is a generally applicable approach to software design and not any specific language.
notzed
 
Posts: 331
Joined: Mon Dec 17, 2012 12:28 am
Location: Australia

Re: C++ with GCC: optimizing for size

Postby etim » Tue Sep 29, 2015 12:32 pm

How do you exactly go about reducing the size of the compiled program? The hello-world example (https://github.com/adapteva/epiphany-examples/tree/2015.1/apps/hello-world) compiles down to a 39KB ELF file. I'm a complete newbie and would love some direction. Thanks!
etim
 
Posts: 22
Joined: Sat Jun 27, 2015 6:08 pm


Return to Programming Q & A

Who is online

Users browsing this forum: No registered users and 12 guests

cron