什么是一些重构方法来减少编译代码的大小? [英] What are some refactoring methods to reduce size of compiled code?

查看:153
本文介绍了什么是一些重构方法来减少编译代码的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要新功能的旧版固件应用程序。应用程序的大小已经接近设备的闪存容量有限,并且几个新功能和变量将其推送到边缘。打开编译器优化是诀窍,但客户警惕这样做是因为它们导致过去的故障。那么,当重构C代码来产生较小的输出时,需要寻找什么常见的东西?

解决方案


  • 使用生成函数而不是数据表

  • 禁用内联函数

  • 将经常使用的宏转换为函数

  • 降低大于本机大小的变量的分辨率(即8位微,尝试去掉16位和32位变量 - 将一些代码序列加倍和四倍)

  • 如果micro有一个较小的指令集(Arm thumb)在编译器中启用它

  • 如果内存被分段(即分页或非线性),那么


    • 重新排列代码,以便需要使用更少的全局调用(较大的调用指令)

    • 重新排列代码和变量使用量以消除全局内存调用

    • 重新评估全局内存使用情况 - 如果可以放置在堆栈上,那么更好的


  • Ma确保你正在编译调试关闭 - 在某些处理器上有很大的区别

  • 压缩无法生成的数据 - 然后在启动时解压缩到ram以快速访问

  • 深入了解编译器选项 - 可能每个调用都是全局的,但您可能可以安全地按文件方式禁用它,以减小大小(有时显着)



如果您仍然需要比编译的优化更多的空间,然后查看生成的程序集与未优化的代码。然后重新编写最大更改发生的代码,以便编译器基于精简关闭的棘手C重写器生成相同的优化。



例如,您可能会有几个类似比较的if语句:

  if(A&& B&& (A&&(C || D)){} 
如果(!A&& B& amp;& amp;& ;(C || D)){}

然后创建一个新变量并提前做一些比较保存编译器不要重复代码:

  E =(C || D); 

如果(A&& B& amp; E){}
如果(A&&B&& E){}
if (!A&& B& E){}

这是编译器自动为您启动优化。有很多,很多其他的,你可能会考虑阅读一些编译器理论,如果你想学习如何用C代码来做这个。


I have a legacy firmware application that requires new functionality. The size of the application was already near the limited flash capacity of the device and the few new functions and variables pushed it over the edge. Turning on compiler optimization does the trick, but the customer is wary of doing so because they have caused failures in the past. So, what are some common things to look for when refactoring C code to produce smaller output?

解决方案

  • Use generation functions instead of data tables where possible
  • Disable inline functions
  • Turn frequently used macros into functions
  • Reduce resolution for variables larger than the native machine size (ie, 8 bit micro, try to get rid of 16 and 32 bit variables - doubles and quadruples some code sequences)
  • If the micro has a smaller instruction set (Arm thumb) enable it in the compiler
  • If the memory is segmented (ie, paged or nonlinear) then
    • Rearrange code so that fewer global calls (larger call instructions) need to be used
    • Rearrange code and variable usage to eliminate global memory calls
    • Re-evaluate global memory usage - if it can be placed on the stack then so much the better
  • Make sure you're compiling with debug turned off - on some processors it makes a big difference
  • Compress data that can't be generated on the fly - then decompress into ram at startup for fast access
  • Delve into the compiler options - it may be that every call is automatically global, but you might be able to safely disable that on a file by file basis to reduce size (sometimes significantly)

If you still need more space than with compile with optimizations turned on, then look at the generated assembly versus unoptimized code. Then re-write the code where the biggest changes took place so that the compiler generates the same optimizations based on tricky C re-writes with optimization turned off.

For instance, you may have several 'if' statements that make similar comparisons:

if(A && B && (C || D)){}
if(A && !B && (C || D)){}
if(!A && B && (C || D)){}

Then creating anew variable and making some comparisons in advance will save the compiler from duplicating code:

E = (C || D);

if(A && B && E){}
if(A && !B && E){}
if(!A && B && E){}

This is one of the optimizations the compiler does for you automatically if you turn it on. There are many, many others, and you might consider reading a bit of compiler theory if you want to learn how to do this by hand in the C code.

这篇关于什么是一些重构方法来减少编译代码的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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