我希望改造一个现有的固定点数库。目前,库只是在32位有符号整数上运行的命名空间函数。我想转过头来创建一个包含整数的固定点类,但是不希望为这个细粒度的东西支付与类相关的任何性能损失,因为性能是用例的问题。
由于预期类具有如此简单的数据要求,并且没有资源,我认为可以使类“面向价值”,利用非修改操作并在合理的情况下按值传递实例。如果实现,这将是一个简单的类,而不是层次结构的一部分。
我想知道是否有可能以这样的方式编写整数包装类,与使用原始整数相比,不会产生实际的性能损失。我几乎确信这是事实,但对编译过程不太了解,只是跳进去。
我知道有人说stl迭代器被编译成简单的指针操作,并且只想用整数操作做类似的事情。
无论如何,库将作为项目的一部分更新到c ++ 11,所以我希望至少使用constexpr和rvalue引用等其他新功能,我可以将这个类的性能推到接近纯整数运算。
此外,任何有关两种实现之间性能差异基准测试的建议都将受到赞赏。
答案 0 :(得分:3)
这个问题的有趣之处在于它只是依赖于编译器。使用Clang / LLVM:
#include <iostream>
using namespace std;
inline int foo(int a) { return a << 1; }
struct Bar
{
int a;
Bar(int x) : a(x) {}
Bar baz() { return a << 1; }
};
void out(int x) __attribute__ ((noinline));
void out(int x) { cout << x; }
void out(Bar x) __attribute__ ((noinline));
void out(Bar x) { cout << x.a; }
void f1(int x) __attribute ((noinline));
void f1(int x) { out(foo(x)); }
void f2(Bar b) __attribute ((noinline));
void f2(Bar b) { out(b.baz()); }
int main(int argc, char** argv)
{
f1(argc);
f2(argc);
}
define void @_Z3outi(i32 %x) uwtable noinline {
%1 = tail call %"class.std::basic_ostream"*
@_ZNSolsEi(%"class.std::basic_ostream"* @_ZSt4cout, i32 %x)
ret void
}
define void @_Z3out3Bar(i32 %x.coerce) uwtable noinline {
%1 = tail call %"class.std::basic_ostream"*
@_ZNSolsEi(%"class.std::basic_ostream"* @_ZSt4cout, i32 %x.coerce)
ret void
}
define void @_Z2f1i(i32 %x) uwtable noinline {
%1 = shl i32 %x, 1
tail call void @_Z3outi(i32 %1)
ret void
}
define void @_Z2f23Bar(i32 %b.coerce) uwtable noinline {
%1 = shl i32 %b.coerce, 1
tail call void @_Z3out3Bar(i32 %1)
ret void
}
毫不奇怪,生成的程序集完全相同:
.globl _Z2f1i
.align 16, 0x90
.type _Z2f1i,@function
_Z2f1i: # @_Z2f1i
.Ltmp6:
.cfi_startproc
# BB#0:
addl %edi, %edi
jmp _Z3outi # TAILCALL
.Ltmp7:
.size _Z2f1i, .Ltmp7-_Z2f1i
.Ltmp8:
.cfi_endproc
.Leh_func_end2:
.globl _Z2f23Bar
.align 16, 0x90
.type _Z2f23Bar,@function
_Z2f23Bar: # @_Z2f23Bar
.Ltmp9:
.cfi_startproc
# BB#0:
addl %edi, %edi
jmp _Z3out3Bar # TAILCALL
.Ltmp10:
.size _Z2f23Bar, .Ltmp10-_Z2f23Bar
.Ltmp11:
.cfi_endproc
.Leh_func_end3:
通常,只要类中的方法被内联,就可以轻松省略this
参数和引用。我不太清楚gcc会如何搞砸它。
答案 1 :(得分:1)
使用值语义实现定点算法会产生较差的性能,因为......
#include <iostream>
using namespace std;
inline int foo(int a) { return a << 1; }
struct Bar
{
int a;
Bar(int x) : a(x) {}
Bar baz() { return a << 1; }
};
void out(int x) __attribute__ ((noinline));
void out(int x) { cout << x; }
void out(Bar x) __attribute__ ((noinline));
void out(Bar x) { cout << x.a; }
void f1(int x) __attribute ((noinline));
void f1(int x) { out(foo(x)); }
void f2(Bar b) __attribute ((noinline));
void f2(Bar b) { out(b.baz()); }
int main(int argc, char** argv)
{
f1(argc);
f2(argc);
}
现在让我们看看f1和f2的反汇编......
00000000004006e0 <f1(int)>:
4006e0: 01 ff add edi,edi
4006e2: e9 d9 ff ff ff jmp 4006c0 <out(int)>
4006e7: 66 0f 1f 84 00 00 00 nop WORD PTR [rax+rax*1+0x0]
4006ee: 00 00
00000000004006f0 <f2(Bar)>:
4006f0: 48 83 ec 08 sub rsp,0x8
4006f4: 01 ff add edi,edi
4006f6: e8 d5 ff ff ff call 4006d0 <out(Bar)>
4006fb: 48 83 c4 08 add rsp,0x8
4006ff: c3 ret
正如你所看到的,f2对堆栈指针有一些额外的麻烦,这也可以防止ret被删除。
(这是-++的g ++ 4.6.1)