简体   繁体   English

C ++优化级别是否会影响Swig Python模块的性能

[英]Does C++ optimisation level affect Swig Python module performance

I have a large Swig Python module. 我有一个大的Swig Python模块。 The C++ wrapper ends up being about 320,000 LoC (including headers I guess). C ++包装器最终约为320,000 LoC(包括我猜的头文件)。 I currently compile this with -O1, and g++ produces a binary that is 44MiB in size, and takes about 3 minutes to compile it. 我目前使用-O1编译它,并且g ++生成一个大小为44MiB的二进制文件,并且编译它需要大约3分钟。

If I turn off optimisation (-O0), the binary comes out at 40MiB, and it takes 44s to compile. 如果我关闭优化(-O0),二进制文件出现在40MiB,编译需要44秒。

Is compiling the wrapper with -O0 going to hurt the performance of the python module significantly? 使用-O0编译包装器会显着损害python模块的性能吗? Before I go and profile the performance of the module at different optimisation levels, has anyone done this sort of analysis before or have any insight into whether it matters? 在我开始分析不同优化级别的模块性能之前,有没有人在此之前进行过这种分析,或者对是否重要有任何见解?

-O0 deactivates all the optimizations performed by gcc. -O0取消激活gcc执行的所有优化。 And optimizations matter. 优化很重要。

So, without a lot of knowledge in your application, I could suggest that this will hurt the performance of your application. 因此,如果您的应用程序中没有很多知识,我可能会建议这会损害您的应用程序的性能。

A usually safe optimization level to use is -O2. 通常安全的优化级别是-O2。

You can check the kind of optimizations performed by GCC in: http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html . 您可以在以下网址查看 GCC执行的优化类型: http//gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

But at the end, if you want to know exactly you should compile at different levels and profile. 但最后,如果你想确切地知道你应该在不同的级别和配置文件编译。

This is bad irrespective of SWIG modules or not. 无论SWIG模块与否,这都是不好的。 There are many optimisations that happen even with gcc -O1 which you will miss if you prevent them happening. 即使使用gcc -O1也会发生许多优化,如果你阻止它们发生,你会错过这些优化。

You can check the difference by inspecting the asm generated by your compiler of choice. 您可以通过检查由您选择的编译器生成的asm来检查差异。 Of these the ones I trivially know will be detrimental to SWIG's generated wrapper: 其中我非常了解的那些对SWIG生成的包装器有害:

  1. Dead code elimination: 死代码消除:

     void foo() { int a = 1; a = 0; } 

    With -O1 this entirely pointless code gets totally removed: 使用-O1这个完全无意义的代码被完全删除:

     foo: pushl %ebp movl %esp, %ebp popl %ebp ret 

    whereas with -O0 it becomes: 而使用-O0则变为:

     foo: pushl %ebp movl %esp, %ebp subl $16, %esp movl $1, -4(%ebp) movl $0, -4(%ebp) leave ret 
  2. Register allocation will be detrimentally impacted in functions with lots of local variables - most SWIG wrapper functions will see a hit from this. 寄存器分配将在具有大量局部变量的函数中受到不利影响 - 大多数SWIG包装函数都会受此影响。 It's hard to show a concise example of this though. 尽管如此,很难展示一个简洁的例子。

  3. Another example, the output from gcc compiling the SWIG wrapper for the prototype: 另一个例子,gcc的输出为原型编译SWIG包装器:

     int foo(unsigned int a, unsigned int b, unsigned int c, unsigned int d); 

    Generates with -O0 : 使用-O0生成:

     Java_testJNI_foo: pushl %ebp movl %esp, %ebp subl $88, %esp movl 16(%ebp), %eax movl %eax, -48(%ebp) movl 20(%ebp), %eax movl %eax, -44(%ebp) movl 24(%ebp), %eax movl %eax, -56(%ebp) movl 28(%ebp), %eax movl %eax, -52(%ebp) movl 32(%ebp), %eax movl %eax, -64(%ebp) movl 36(%ebp), %eax movl %eax, -60(%ebp) movl 40(%ebp), %eax movl %eax, -72(%ebp) movl 44(%ebp), %eax movl %eax, -68(%ebp) movl $0, -32(%ebp) movl -48(%ebp), %eax movl %eax, -28(%ebp) movl -56(%ebp), %eax movl %eax, -24(%ebp) movl -64(%ebp), %eax movl %eax, -20(%ebp) movl -72(%ebp), %eax movl %eax, -16(%ebp) movl -16(%ebp), %eax movl %eax, 12(%esp) movl -20(%ebp), %eax movl %eax, 8(%esp) movl -24(%ebp), %eax movl %eax, 4(%esp) movl -28(%ebp), %eax movl %eax, (%esp) call foo movl %eax, -12(%ebp) movl -12(%ebp), %eax movl %eax, -32(%ebp) movl -32(%ebp), %eax leave ret 

    Compared to -O1 which generates just: 与仅生成的-O1相比:

     Java_testJNI_foo: pushl %ebp movl %esp, %ebp subl $24, %esp movl 40(%ebp), %eax movl %eax, 12(%esp) movl 32(%ebp), %eax movl %eax, 8(%esp) movl 24(%ebp), %eax movl %eax, 4(%esp) movl 16(%ebp), %eax movl %eax, (%esp) call foo leave ret 
  4. With -O1 g++ can generate far smarter code for: 使用-O1 g ++可以生成更智能的代码:

     %module test %{ int value() { return 100; } %} %feature("compactdefaultargs") foo; %inline %{ int foo(int a=value(), int b=value(), int c=value()) { return 0; } %} 

The short answer is with optimisations disabled completely GCC generates extremely naive code - this is true of SWIG wrappers as much as any other program, if not more given the style of the automatically generated code. 简短的回答是完全禁用优化GCC生成非常天真的代码 - SWIG包装器和任何其他程序一样,如果没有给出自动生成代码的样式。

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

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