简体   繁体   English

为什么这些方法会使我的程序变得更大?

[英]Why do these methods make my program so much larger?

I'm working on some Arduino code and have the following code: 我正在处理一些Arduino代码并具有以下代码:

uint8_t world[24][2][3];
bool getDispPixel(uint8_t x, uint8_t y, uint8_t num)
{
    static uint8_t rowByte = 0; // 0 means top 8, 1 means bottom 8
    static uint8_t rowBit = 0;

    if(y > 7)
    {
        rowByte = 1;
        rowBit = x - 8;
    }
    else
    {
        rowByte = 0;
        rowBit = x;
    }

    return (world[x][rowByte][num] & (1 << rowBit)) > 0;
}

void setDispPixel(uint8_t x, uint8_t y, uint8_t num, bool state)
{
    static uint8_t rowByte = 0; // 0 means top 8, 1 means bottom 8
    static uint8_t rowBit = 0;

    if(y > 7)
    {
        rowByte = 1;
        rowBit = x - 8;
    }
    else
    {
        rowByte = 0;
        rowBit = x;
    }

    if(state)
        world[x][rowByte][num] |= (1 << rowBit);
    else
        world[x][rowByte][num] &= ~(1 << rowBit);
}

What's weird is these methods add a TON of size to the program. 奇怪的是这些方法为程序添加了大小的TON。 Even just parts of it. 即使只是一部分。 If i comment out the following part from just one of the methods, it drops 2536 bytes from the program size! 如果我只从其中一个方法中注释掉以下部分,它会从程序大小中删除2536个字节!

if(y > 7)
{
    rowByte = 1;
    rowBit = x - 8;
}
else
{
    rowByte = 0;
    rowBit = x;
}

Both methods are called quite often, over 200 times combined. 两种方法都经常被调用,超过200次组合。 I would believe it if they were marked as inline, but they are not. 如果它们被标记为内联,我会相信它,但它们不是。 Any idea of what could be causing this? 什么可能导致这个?

Update: If I completely comment out those methods' contents it drops the size by 20k! 更新:如果我完全注释掉那些方法的内容,它将大小减少20k! Looks like every call to the function eats up 94 bytes. 看起来每个函数调用都会占用94个字节。 No idea why... 不明白为什么......

If the Arduino toolchain supports GCC extensions (and some quick searching suggests it does), then you can use __attribute__((noinline)) to disable inlining on these functions like so: 如果Arduino工具链支持GCC扩展(一些快速搜索表明它支持),那么您可以使用__attribute__((noinline))禁用这些功能的内联,如下所示:

bool getDispPixel(uint8_t x, uint8_t y, uint8_t num) __attribute__((noinline));
bool getDispPixel(uint8_t x, uint8_t y, uint8_t num)
{
    // body of the function here
}

void setDispPixel(uint8_t x, uint8_t y, uint8_t num, bool state) __attribute((noinline));
void setDispPixel(uint8_t x, uint8_t y, uint8_t num, bool state)
{
    // body of the function here
}

The extra line looks redundant, but isn't. 多余的行看起来很多余,但不是。 It's how the syntax for the extension works. 这是扩展的语法如何工作。

Here is the output of: nm --print-size --size-sort --radix=d --demangle GOLClock.cpp.o (Size of your objects ranked by size): 以下是输出: nm --print-size --size-sort --radix=d --demangle GOLClock.cpp.o (对象的大小按大小排序):

http://pastebin.com/rHEhuEKg http://pastebin.com/rHEhuEKg

You can see that the assembly code for the function SetDispPixel takes 148 bytes, and for the function GetDispPixel 94 bytes. 你可以看到,该函数的汇编代码SetDispPixel需要148个字节,而对于功能GetDispPixel 94个字节。

If it causes a huge increase of your binary it probably means that your function is getting inlined. 如果它导致二进制文件的大量增加,则可能意味着您的函数已被内联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM