减少C ++代码大小 [英] Reducing C++ code size

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

问题描述

我正在为微控制器开发一种动态语言(可编译为C ++),作为一名CS专家,我决定先做正确的事,然后再担心优化.下面是我的对象系统的结构,



Base (Interface)
|
|---- Number (Interface)
|      |
|      |-- Int
|      |-- Float
|
|---- Char
|
|---- Sequence (Interface)
|      |
|      |-- LinkedList
|
|---- Function (Interface)
      |
      |-- Built-in Functions (Functors)

现在一切正常,生成的代码大小开始出现问题,每个操作都必须由函子((+,-while等)表示),因此我猜测这会导致代码大小增加或者我将所有内容都传递为基类,然后根据需要进行强制转换,因此我有很多强制转换,即使它没有使用所有对象类型(6个函子+ 4个整数),目前一个简单的led开/关循环也要花费15 KB. /p>

我尝试了各种不会减小代码大小的avr-gcc选项.因此,我想知道如何找出代码的哪一部分消耗了如此多的空间?

解决方案

在链接步骤中,您应该让avr-gcc生成一个映射文件,以便您可以查看它并查看它占用了太多空间. >

请确保使用-ffunction-sections编译选项和-Wl,-gc-sections链接器选项从二进制文件中删除未使用的功能.

您正在使用C ++的哪些功能?其中一些(例如类和名称空间)很便宜,而另一些则非常昂贵(例如异常处理).接口和虚拟函数将比仅具有类和子类更昂贵.

我曾经看过一个好话,叫做"C ++的低效率:事实还是虚构?"由IAR生产.它经历了C ++的每个高级功能,并为此付出了代价.它主要面向针对ARM体系结构进行开发的人员,但它对您仍然可能有价值.这是幻灯片: https://www.iar.com/globalassets/about-us/events/atc2015/inefficiencies-of-c.pdf

I am working on a dynamic language (that compiles into C++) for microcontrollers, being a CS guy I decided to do it properly first then worry about optimization. Below is how my object system is structured,



Base (Interface)
|
|---- Number (Interface)
|      |
|      |-- Int
|      |-- Float
|
|---- Char
|
|---- Sequence (Interface)
|      |
|      |-- LinkedList
|
|---- Function (Interface)
      |
      |-- Built-in Functions (Functors)

Now that everything is working generated code size started to be a problem, every operation has to be represented by a functor, (+,- while,for etc..) so I am guessing that is causing the bump in code size or I pass everything as base class then cast as needed so I have a lot of casts, currently a simple led on/off loop is costing 15 kbytes even though it doesn't use all object types (6 functors + 4 integers).

I have tried various avr-gcc options that did not reduce code size. So I am wondering how can I figure out which part of the code is consuming so much space?

解决方案

During the link step, you should have avr-gcc generate a map file so you can look at it and see what is taking up so much space.

Be sure to use the -ffunction-sections compilation option and the -Wl,-gc-sections linker option to remove unused functions from the binary.

What features of C++ are you using? Some of them (like classes and namespaces) are cheap, while others are very expensive (such as exception handling). Interfaces and virtual functions will be more expensive than just having classes and subclasses.

I saw a good talk once called "The Inefficiency of C++: Fact or Fiction?" produced by IAR. It went through each advanced feature of C++ and assigned a cost to it. It was mainly geared towards people developing for the ARM architecture but it could still be valuable to you. Here are the slides: https://www.iar.com/globalassets/about-us/events/atc2015/inefficiencies-of-c.pdf

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

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