MSVC:为什么调用静态函数比调用实例方法生成更多的汇编代码?

问题描述 投票:0回答:0

在反汇编我的应用程序时,我注意到与实例方法相比,当我调用静态方法时,MSVC 会生成更多的汇编操作码。即使两个函数相同(因此实例方法未使用

this
)也是如此。

代码示例:

struct matrix4
{
    float m[16];

    static matrix4 translation1();
    matrix4 translation2() const;
};

func(matrix4::translation1());
func(matrix4::identity.translation2());

生成的代码如下所示:

        call    static matrix4 matrix4::translation1(float)       ; matrix4::translation1
        push    16                                  ; 00000010H
        mov     esi, eax
        lea     edi, DWORD PTR $T2[ebp]
        pop     ecx
        lea     eax, DWORD PTR $T2[ebp]
        rep movsd
        push    eax
        call    void func(matrix4 const &)                ; func

[...]

        mov     ecx, OFFSET static matrix4 const matrix4::identity      ; matrix4::identity
        call    matrix4 matrix4::translation2(float)const       ; matrix4::translation2
        push    eax
        call    void func(matrix4 const &)                ; func

对于我的应用程序,我关心生成的文件大小。看起来我应该用实例方法替换我所有的静态方法,这让我感到非常惊讶。我宁愿避免这种情况,因为这意味着要更改大量代码。我错过了什么吗?

Repro:https://godbolt.org/z/o3x5vKGdK(比较 block1 和 block2)

我注意到其他编译器标志(有和没有 /Os)也有类似的问题。无论该方法是否内联,都会发生同样的情况。

Clang 好像没有这个行为。如果那是 MSVC 优化错误,是否有简单的解决方法?

(当我将我的代码从 Visual Studio 2010 升级到更新的 VS 版本时,我注意到了这个问题。VS2010 生成了一个较小的可执行文件。)

c++ visual-c++ compiler-optimization return-value-optimization
© www.soinside.com 2019 - 2024. All rights reserved.