Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much of an increase in size do inline functions cause?

Tags:

c++

gtk

I have recently started making a C++ Wrapper of GTK+ (Nothing special, just wrapping everything into C++ classes for easy development, to be used in-house) and to cause minimum performance bloat to the already slow Gtk+ I used inline functions almost everywhere. Take a look at a few class functions...

class Widget : public Object
{
public: // A few sample functions. gwidget is the internal GTK+ widget.
    void Show(void) { gtk_widget_show(GTK_WIDGET(gwidget)); }
    void ShowNow(void) { gtk_widget_show_now(GTK_WIDGET(gwidget)); }
    void Hide(void) { gtk_widget_hide(GTK_WIDGET(gwidget)); }
    void ShowAll(void) { gtk_widget_show_all(GTK_WIDGET(gwidget)); }
public: // the internal Gtk+ widget.
    GtkWidget* gwidget;
};

And although there is an almost non-existent performance bloat and the startup time and memory usage is exactly the same, the file size has increased dramatically. a C Gtk+ sample window generates 6.5 kb while a sample window using my wrapper generates 22.5 kb. , so I need a little advice. Should I continue using inline functions? I want my apps to be efficient and I can compromise a little on file size like I can take it even if a 6.5 kb C GTK+ program generated like 400-500 kb using my wrapper but NOT MORE. I don't want my wrapper to produce HUGE EXES like wxWidgets or MFC. So is it worthwile to use inline functions or should i use normal ones?

NOTE: all my functions only take up one or sometimes two lines and are not big as you can see in the example.

like image 812
ApprenticeHacker Avatar asked Jul 07 '11 07:07

ApprenticeHacker


People also ask

Does inline function increase the code size?

Inline functions can increase the code size of your application and add stress to the instruction cache. Some debuggers have difficulty showing the thread of execution for inline functions. A function call itself can limit the compiler's ability to optimize register usage in the calling function.

What happens when you inline a function?

Answer. An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

Do inline functions use more memory?

There are two kinds of memory usage that inline functions will affect: code size — in general, inlining code will increase how much memory is used to load your program. This is because there will be multiple copies of the generated code scattered around your program.

What is the limit of inline function?

Limitations of Inline FunctionsInline functions do not work if the body of the function contains any sort of looping or iteration. Inline functions do not support the use of switch or goto statements. C++ Inline functions cannot work if the function defined is recursive in nature.


2 Answers

I strongly suspect you're comparing apples to oranges.

Did you compiler with exactly the same compiler, the same compile flags, and making an application with the exact same functionality?

If so, disassemble the executable, and see what the extra code is.

My guess is that it's some one-off library code used to support C++ features that were previously unused.

But as always, don't guess, measure.

You've got a single data point. That doesn't tell you much. We could be looking at a 350% increase in file size in all cases, or we could be looking at a fixed 16kb overhead. You need to find out which it is. So get some more data points. Extend your application. Make it open ten windows instead of one, or otherwise add extra functionality. is "your" version three times as big in that case too? Or is it 16kb larger? Or somewhere in between? Get some more data points, and you'll be able to see how the file size scales.

But most likely, you're worrying over nothing, for several reasons:

  • the C++ compiler treats inline as a hint. You are making it easy for the compiler to inline the function, but the decision rests with the compiler itself, and it tries to make the application fast. If the file size starts growing out of control, that will slow down your code, and so your compiler will try to optimize more towards a smaller file size.
  • you are looking at a couple of kilobytes. In an age of terabyte harddrives. If this has the potential to become a problem, then you should be able to provoke that problem in a test case. If you can't write a test that causes more than 16kb growth in file size, then it's not worth worrying about.
  • if file size does become a problem, compilers typically have an "optimize for size" flag.
  • large executables typically gain their size because they contain a lot of data and resources. The code itself is very rarely the problem (unless you go completely bonkers with template metaprogramming)
like image 71
jalf Avatar answered Sep 18 '22 10:09

jalf


The size difference is more likely to be due to the libraries which are pulled in for C++, rather than in your C equivalent there is less library overhead.

If all your wrapper code follows the above, then very little to none in terms of bloat.

Looks to me that your solution is a good worth while way of implementing the wrapper.

like image 22
Soren Avatar answered Sep 19 '22 10:09

Soren