How much of the size increase do the built-in functions make?

I recently started creating C ++ Wrapper from GTK + (nothing special, just wrapping everything in C ++ classes for easy development, to use it from my own experience) and in order to cause minimal performance on already slow Gtk + I built-in functions almost everywhere. Take a look at a few class features ...

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 nonexistent bloat of performance, and the startup time and memory usage are exactly the same, the size increased significantly. The Gtk + sample window C generates 6.5 kb , and the sample window using my shell generates 22.5 kb. so I need some advice. Should I continue to use the built-in functions? I want my applications to be effective, and I can compromise the file size a bit, how can I accept it, even if the 6.5 KB C GTK + program generated as 400-500 kB using my wrapper, but NOT MORE. I do not want my cover to create HUGE EXES like wxWidgets or MFC. So is it worth using built-in functions or using regular ones?

NOTE : all my functions occupy only one, and sometimes two lines, and are not large, as you can see in this example.

+6
source share
8 answers

I strongly suspect that you are comparing apples to oranges.

Were you a compiler with exactly the same compiler, the same compilation flag and creating an application with the same functionality?

If so, parse the executable and see what the additional code is.

My guess is that this is some one-time library used to support C ++ functions that have not been used before.

But, as always, do not guess, measure.

You have one data point. This does not tell you. We could look at a 350% increase in file size in all cases, or we could look at fixed 16K overhead. You need to find out what it is. So get some more data points. Extend your application. Make it open ten windows instead of one, or add additional features in some other way. is your version three times as large in this case? Or is it 16kb more? Or somewhere in the middle? Get a few more data points and you can see how the file size scales.

But most likely you are not worried for several reasons:

  • the C ++ compiler treats inline as a hint. You simplify the built-in function for the compiler, but the decision belongs to the compiler itself, and it tries to make the application quickly. If the file size starts to get out of hand, this will slow down your code, so your compiler will try to optimize more towards a smaller file size.
  • you look at a couple of kilobytes. In the era of terabyte hard drives. If this can be a problem, you should be able to provoke this problem in a test case. If you can not write a test that will increase the file size by more than 16 kilobytes, then this is not worth the worry.
  • If file size becomes a problem, compilers usually have the "optimize for size" flag.
  • large executables usually get their size because they contain a lot of data and resources. The code itself is very rarely a problem (unless you become completely funkers with metaprogramming templates)
+5
source

The size difference is most likely due to the libraries that are pulled in for C ++, and not in your C equivalent, less resources.

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

Believes that your decision is worth it to implement a shell.

+5
source

To cause minimal bloat performance for already slow Gtk + I, built-in functions are used almost everywhere

Compilers know well when to embed and not perform built-in functions. The inline does not really mean that the function will be built-in, only the definitions of this function in different translation units do not violate one definition rule (ODR).

As for the size of the code, this will depend on the compiler options and a few other things, but for the code that you represent, there should be no effect, as if the function was turned on, the call to one function will be replaced to call another, this is one liner. Please note that many compilers do create this function and leave it in binary format, even if all applications are embedded, you can see the compiler / linker documentation on how to remove them, but even if they are created, the size of the project should not depend much .

If you want your project to grow from 6.5 to 400 KB, you must be more than accurate.

+3
source

It depends on the

  • how big are your functions.
  • how often do you use them.
  • compiler settings (which you can influence)
  • compiler implementation (which you cannot)

etc .. etc In other words, do not assume measure. Make a representative fragment of your project with and without built-in functions and see what effect is in your specific circumstances. Any predictions you receive over the Internet will be educated guesses at best.

+1
source

If your functions consist of only one or two lines, then it is very unlikely that they will increase the size of your resulting binary file by any significant amount. In fact, they can reduce it if the code itself is smaller than the utility code of the function call.

Please note that in any case, the overhead will be negligible. Just removing one call to std::sort or creating a std::map will compensate for any bloat. If you care about the size of the code, small built-in functions are the least of your worries.

+1
source

The examples you provided should not cause an increase in code on the call side. Replacing a built-in function that only performs one call and nothing else should be optimized by any decent compiler.

You will need to investigate where the real increase is. Look at the collector that is being created, which usually gives a good overview of the overhead.

0
source

There is no simple answer. It depends on many factors:

  • function complexity
  • processor architecture and memory model
  • procedures call execution procedures
  • mechanism for passing parameters, including how to access this object

Size is not the only efficiency. On many modern processors, executing any kind of branch or call command can stop the CPU in the equivalent time of many instructions. Often replacing a call statement with a few statements from the function body is a big time gain. It can also be an advantage of code size, since processor registers may already have functional parameters in them, so they would not need to be moved or moved.

Do not sweat small optimizations until a known problem with space or speed appears. Then find a 10% fix that affects 90% of the problem.

0
source

Congratulations, you are inventing GTKmm , the official C ++ bindings for GTK +.

-1
source

Source: https://habr.com/ru/post/892207/


All Articles