D中的异常处理开销 [英] Overhead of exception handling in D

查看:138
本文介绍了D中的异常处理开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在D2编程语言中,使用异常处理的性能是什么?特别是:




  • 如果我不写异常处理代码怎么办?

  • 如果我这样做,但是没有例外会被抛出?

  • 如果我做,异常​​被抛出了怎么办?

  • 异常处理是否导致任何优化机会被错过?

  • 可以在许多(大多数)C ++实现中禁用异常处理?



我知道几乎所有的商业游戏开发工作室都会在其C ++中禁用异常处理,这是因为与正确处理异常相关的性能影响和增加的开发时间。我知道D使后者不那么痛苦,但是性能如何呢?



当然,这可能是实现定义的,所以在这个问题上,请专注于DMD编译器

解决方案

我不能说D或其任何编译器,但我可以告诉你一些关于C ++,Windows和Visual Studio编译器。这可能有助于您大致了解D做什么。



首先,32位和64位机器上的异常处理是不同的。 x86 ABI(prolog / epilog,展开,调用约定)更为松散,因此编译器和程序本身必须做更多的工作。 x86-64 ABI更严格,操作系统扮演着更大的角色,使程序本身更容易处理异常。如果您在Windows上运行D,那么可能使用 SEH (结构化异常处理)像C ++一样。



同样,我以下的所有答案都与Windows,C ++和Visual Studio相关。


如果我不写异常处理
代码怎么办?


x86 / x86-64:该方法没有任何费用。


如果我这样做,但没有例外是
被抛出?


x86:即使没有抛出异常,也会有成本。异常处理信息被推入TIB(线程信息块),如初始范围和功能特定的异常处理程序。为了知道要破坏的对象以及要搜索的处理程序,维护作用域变量。当您输入 try 块并构造具有析构函数的堆栈对象时,此作用域变量将更新。



x86-64:由于更严格的规则,没有额外的代码(或非常非常小的)。这比x86有一个很大的优势。


如果我这样做,异常是
抛出?


在x86或x86-64上肯定会有一个命中。 例外应该是例外,。不要使用它们进行正常的控制流程。只能使用它们来表示真正特殊的意想不到的事件。从本质上说,您不必担心异常的成本。即使他们花了2秒钟,你也不应该在乎,因为只有当所有东西都向南时,才应该发生。



就是说,在x86-64上抛出异常比在x86上扔它们更贵。 x86-64架构针对没有异常抛出的情况进行优化,理想情况下,几乎所有的时间都是这样。






大图:




  • 我看不到传播错误代码比异常处理快得多,特别是在x64平台上。 >
  • 我怀疑你会发现异常处理是一个问题,除非你滥用例外。

  • 如果你不确定,你应该衡量你的代码


In the D2 programming language, what are the performance implications of using exception handling? In particular:

  • What if I write no exception handling code?
  • What if I do, but no exceptions are ever thrown?
  • What if I do, and exception are thrown?
  • Does exception handling cause any optimization opportunities to be missed?
  • Can exception handling be disabled like it can in many (most?) C++ implementations?

I know that almost all commercial game development studios disable exception handling in their C++ due to the performance implications and increased development time associated with correctly handling exceptions. I know D makes that latter less painful, but what about performance?

Of course, this is all probably implementation defined, so for this question, please focus on the DMD compiler.

解决方案

I cannot speak about D or any of its compilers, but I can tell you some about C++, Windows, and the Visual Studio compiler. This might help you understand roughly how D does things.

First, exception handling on 32- and 64-bit machines is different. The x86 ABI (prolog/epilog, unwinding, calling convention) is more lax, so the compiler and the program itself must do more work. The x86-64 ABI is stricter and the OS plays a bigger role, making it easier for the program itself to work with exceptions. If you're running D on Windows, then it likely uses SEH (structured exception handling) like C++ does.

Again, all of my answers below relate to Windows, C++, and Visual Studio.

What if I write no exception handling code?

x86/x86-64: There is no cost for that method.

What if I do, but no exceptions are ever thrown?

x86: There is a cost even when exceptions aren't thrown. Exception handling information is pushed into the TIB (thread information block) such as the initial scope and the function-specific exception handler. In order to know what objects to destruct and what handlers to search, a scope variable is maintained. This scope variable is updated as you enter try blocks and construct stack objects that have destructors.

x86-64: Because of the stricter rules, there is no extra code (or very very minimal). This is a big advantage over x86.

What if I do, and exception are thrown?

On either x86 or x86-64, there will definitely be a hit. Exceptions should be exceptional though. Do not use them for regular control flow. Only use them to signal truly exceptional, unexpected events. Essentially, you should never have to worry about the cost of exceptions. Even if they took 2 seconds, you shouldn't care, because they should only happen when everything is going south anyway.

With that said, throwing exceptions on x86-64 is more expensive than throwing them on x86. The x86-64 architecture is optimized around the case of no exceptions being thrown, which, ideally, is nearly all the time.


Big picture:

  • I can't see propagating error codes being materially faster than exception handling, especially on x64 platforms.
  • I doubt you will ever find exception handling being a problem unless you're abusing exceptions.
  • If you're unsure, you should measure the performance of your code.

这篇关于D中的异常处理开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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