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

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

问题描述

我需要新的功能遗留固件应用程序。的应用程序的大小已经所述装置的有限的闪存容量邻近和一些新的函数和变量推它在边缘。打开编译器的优化做的伎俩,但客户是警惕这样做是因为他们已经造成了过去失败的。那么,什么是一些常见的事情看重构C code当产生较小的输出?

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?

推荐答案


  • 使用生成功能,而不是数据表尽可能

  • 禁用内联函数

  • 打开经常使用到的宏功能

  • 降低了比本机尺寸较大的变数分辨率(即8位微,试图摆脱16位和32位变量 - 双打和四倍一些code序列)

  • 如果微有较小的指令集(臂拇指)使它能够在编译器

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

    • 重新排列code,使较少的全局呼叫(大调用指令),需要使用

    • 重新排列code和变量的使用,以消除全球内存调用

    • 重新评估全球内存使用情况 - 如果它可以放在堆栈然后那就更好了

    如果您仍然需要比更多的空间,编译优化打开,然后看看生成的程序集与未经优化的code。与关闭优化然后重新写code,其中最大的变化发生了这样的基础上棘手C中的编译器生成相同的优化重新编写。

    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)){}
    

    然后重新创建变量,使得提前一些比较节省编译器复制code:

    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){}
    

    这是,如果你打开​​它的编译器会自动为你做了优化之一。有很多很多人,如果你想了解如何用手工在C code做到这一点,你可能会考虑读一个位编译原理的。

    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.

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

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