C ++中大小的代码优化 [英] Code optimization for size in C++

查看:75
本文介绍了C ++中大小的代码优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我们正在研究CrossCore Tool,我们开发了一个嵌入式C ++应用程序。

我们的二进制大小很大,但我们需要减小二进制大小以适应不同的MCU!

C ++中是否有任何技术可以帮助我们减少二进制大小?

我们使用的编译器是GCC。

请在这方面帮助我们!



我们已经尝试了



内联函数,虚函数,未使用的变量删除和将局部变量移动到最内层范围



但这些技术帮助我们提高了性能而不是尺寸,我们需要减小尺寸!



问候,

Raja Vignesh



我有什么尝试过:



大家好,



我们试过了



- >内联函数

- >虚拟功能

- >未使用的变量删除

- >将局部变量移动到最大范围内



但是这些技术帮助我们提高了性能而不是尺寸,我们需要减小尺寸!

解决方案

看起来你试图将太多代码填入太少的内存中。没有标准的程序肯定会有所帮助。太多太多了。一个很大的问题仍然是:您的vexecutable是否太大而无法安装,或者您是否在运行时耗尽了内存?从你的问题我假设第一个。



尝试以下步骤,但请注意他们逐渐绝望,可能仍然不足以解决问题。 />


1)开始在代码中寻找冗余。不止一次使用类似的代码显然是一个很大的浪费。



2)查看代码并尝试识别生成最长代码的函数。尝试重写和优化它们。使用使用较少内存的不同数据结构或算法。



3)丢弃可能很不错的小功能代码,但不是必需的。



4)协商新的要求,使更多的代码不必要,然后扔出去。


你尝试过的技术根本不会减少你的代码大小,其中一些会使你的代码更大。



- 内联函数:是一种工具,可以让您的代码更快,并且通常可以使您的代码更大!



- 虚函数:与代码大小无关。



- 未使用的变量删除:不要减小代码大小,编译器可能会无论如何要照顾好它们。



- 将局部变量移到最里面的范围:与代码大小无关。



那你该怎么办?首先,您应该分析您的代码并查看大块的位置。获取链接器列表,查看占用大部分代码大小的内容。然后处理这些事情。



如果你的代码很大,因为你有成千上万的函数,除了试图找出之外没什么可做的相应的常用功能和重构代码。



一般来说,有两个大问题可能会破坏你的代码:



1.你正在使用的图书馆 - 你没有太大的影响力。



2.模板的使用:这会炸毁你的如果操作不当,代码会显着。



同时检查您是否仍在调试模式而不是生产模式下构建。编译器添加的调试信息可以使你的代码变大几倍!



祝你好运。


引用:

- >内联函数

错误:由于需要较少的堆栈操作,内联函数显式增加代码大小以获得性能。

Quote:

- >虚函数

最多没有影响,实际上由于虚拟表需要空间,它会稍微增加代码大小。

引用:
- >未使用的变量删除

除非它们是静态的,否则它不会改变代码大小,而是在运行时更改应用程序的内存占用量。很好,但不适合你想要达到的目标。

Quote:

- >将局部变量移动到最内部范围

对于什么?变量声明不会增加代码大小,只增加内存占用量。在函数内部,所有变量都存储在堆栈中。充其量,使用非常专业的编译器,您可以获得堆栈地址重用,以最大限度地减少运行时内存占用。



为了最小化代码大小,您应该:

*消除所有不需要的静态库。

*减少层次结构中的调用路径和中间对象的数量。

*如果可能,使用具有多个签名的函数,只使用其中的一个按顺序在编译时没有链接其他变体。小心:单个遗漏函数会将其变体添加到链接文件中。

*查看您的跨平台框架是否具有选项(通常采用#defines形式)以消除大多数编组和中间调用针对特定MCU编译。跨平台框架很棒,但它们确实增加了代码大小。

*消除像boost这样的大型库,这很棒但很臃肿。



这些是我的2美分,

Denis


Hi all,

We are working on CrossCore Tool, on which we have developed a embedded C++ application.
Our binary size is big, but we need to reduce the binary size so as to fit in different MCU!
Is there any techniques in C++ that really help us in reducing binary size?
The compiler we use is GCC.
Please help us in this regard!

We have tried

Inline Functions, Virtual Functions, Unused variables removal and Moving local variables to the inner most scope

But these techniques have helped us in improving performance rather than size, we need size reduction!

Regards,
Raja Vignesh

What I have tried:

Hi all,

We have tried

-> Inline Functions
-> Virtual Functions
-> Unused variables removal
-> Moving local variables to the inner most scope

But these techniques have helped us in improving performance rather than size, we need size reduction!

解决方案

It looks like you are trying to stuff too much code into too little memory. There is no standard procedure that will surely help. Too much is too much. One big question still is: Is your vexecutable too big to be installed or do you run out of memory at runtime? From your question I assume the first.

Try the following steps, but be advised that they become progressively desperate and may still not be enough to solve the problem.

1) Start looking for redundancy in your code. Having similar code more than once obviously is a big waste.

2) Look over your code and try to identify the functions that generate the longest code. Try to rewrite and optimize them. Use different data structures or algorithms that use less memory.

3) Throw out the code of minor features which may be nice to have, but are not essential.

4) Negotiate new requirements to make more code nonessential and then throw it out.


The techniques you have tried won't reduce your code size at all, some of them make your code even bigger.

- Inline functions: Are a tool to make your code faster and generally make your code bigger!

- Virtual functions: Have nothing to do with code size.

- Unused variable removal: Don't reduce your code size and the compiler will probably take care of them anyway.

- Moving local variables to the inner most scope: Has nothing to do with code size.

So what should you try? First of all you should analyze your code and see where the big chunks are. Take a linker listing and see what uses up most of your code size. Then work on those things.

If your code is big, because you have tens of thousands of functions, there is not much you can do except to try to find out common functionality and restructure your code accordingly.

In general there are two big issues that can potentially blow up your code:

1. The libraries you are using -- you don't have much influence there.

2. Usage of templates: This can blow up your code significantly if not done properly.

Also check whether you are still building in debug mode, instead of production mode. The debug information that the compiler adds can make your code several times bigger!

Good luck.


Quote:

-> Inline Functions

Bad: inline functions explicitly increase code size to the benefit of performances due to less stack manipulation required.

Quote:

-> Virtual Functions

No impact at best, actually it increases the code size a bit due to the virtual tables needing space.

Quote:

-> Unused variables removal

Unless they are statics it does not change code size but the memory footprint of the application when running. Good but not for what you want to achieve.

Quote:

-> Moving local variables to the inner most scope

What for? A variable declaration does not increase code size, only memory footprint. Inside a function ALL the variables are memorized in the stack. At best, with very specialized compilers, you earn a stack address reuse to minimize runtime memory footprint.

To minimize code size you should:
* Eliminate all unneeded static libraries.
* Reduce the call paths and the number of intermediate objects in the hierarchy.
* If possible, with functions having multiple signatures, using only a single one of them in order to not have the other variants linked at compile time. Careful: a single missed function will add its variant to the linked file.
* See if your cross platform framework has options (usually in form of #defines) to eliminate most of th marshalling and intermediate calls when compiled for a specific MCU. Cross platform frameworks are great but they do increase code size.
* Eliminate any huge library like boost, which is fantastic but bloated.

These are my 2 cents,
Denis


这篇关于C ++中大小的代码优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆