处理器是否有实际的电路来帮助异常处理?
我的问题非常简单:我想知道处理器是否尝试以某种方式帮助异常处理。如果在设计“异常就绪”处理器方面付出足够的努力,是否可以完全消除异常处理和抛出的开销?
因为据我所知,所有异常处理都是通过软件完成的,这总是会增加一些开销。我错了吗?
- 编辑
所以,感谢以下所有答案。我很欣赏。你已经回答了我的问题。
但只是为了澄清我为什么问这个:一般来说,人们不会太深入地优化异常,因为他们都认为“异常是针对特殊情况的”,因此它们不是瓶颈。
我不认为只有在戏剧性的情况下才应该抛出异常。我的想法基本上是,只要函数无法遵守其承诺的操作,就应该抛出异常。
如果我说:
doSomethingImportant();
如果由于无论原因无法完成“重要的事情”,则应该抛出异常。
当然, doSomethingImportant() 可能无法遵守,因为系统内存不足(一个戏剧性的问题),但我认为我们应该能够将更简单的“我现在/这次不能这样做,抱歉”建模为我们的软件嵌入到我们的设计中。我想说的是,我认为异常可能是异常的,是的,但它们应该像正常的软件流程一样,而不是系统必须从中“恢复”的“致命错误” ,诺姆赛因?
虽然由良好数据中心支持的大型应用程序几乎不会因为异常处理而出现瓶颈,但请不要忘记嵌入式设备有一个需要计算资源的市场,并且异常处理确实会产生影响(这是我的目标是什么)。
我个人认为异常非常具有表现力,并且我希望在嵌入式设备中使用它们,并通过返回“-1”并使用“if”进行检查来获得尽可能多的“开销”。
My question here is pretty simple: I want to know if processors try to help exception handling somehow. Would it be possible to completely remove overhead from exception handling and throwing if enough effort is put in designing a processor "exception-ready"?
Because as far as I know, all exception handling is done via software, and that always adds some overhead. Am I wrong?
-- edit
So, thanks for all the answers below. I appreciate. You have answered my question.
But just to clarify why I asked this: generally, people don't go too deep into optimizing exceptions because they all think "exceptions are for exceptional circumstances", and therefore they are no bottleneck.
I don't think exceptions should only be thrown under dramatic circumstances. What I think is, basically, that an exception should be thrown anytime a function cannot comply to what it promised to do.
If I say:
doSomethingImportant();
And if for whatever reason "something important" can't be done, this should throw an exception.
Of course, doSomethingImportant() might not be able to comply because the system ran out of memory (a dramatic problem), but I think we should be able to model simpler "I can't do that now/this time, sorry" into our software, embedded into our designs. I'd like to say that I think exceptions can be exceptional, yes, but they are to be expected like normal software flow, not as a "fatal error" from which the system has to "recover", nomsain?
And while big applications backed by nice data centers will hardly ever bottleneck because of exception handling, please don't forget there's market for embedded devices where resources are counted, and exception handling does have an impact (which is what I'm aiming for).
I personally find exceptions quite expressive, and I'd like to use them in embedded devices with as much "overhead" as I would get by returning "-1" and checking that with an "if".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在指令集架构文档中,异常是处理器的异常情况(例如零除、非法指令等)。它们通常被转换为中断(但大多数中断是处理器的外部信号)。
在编程语言规范中,异常是非本地控制流结构,通常涉及某种调用堆栈展开。
我相信最近的微架构专门处理堆栈指针(例如wrt缓存和指令调度)。他们可能有一些专门用于编程语言异常所需的堆栈指针更改的电路。
某些语言和实现比其他语言和实现具有更好的异常语义和机制。例如,Ocaml 异常处理比 C++ 更快(至少使用 GCC 编译器)。
In Instruction Set Architecture documentations, exceptions are abnormal situations for processors (like zero-divide, illegal instructions, etc..). They are usually translated to interrupts (but most interrupts are external signals to the processor).
In programming language specifications, exceptions are non-local control flow constructs, usually involving some kind of call stack unwinding.
I believe that recent micro-architectures handle specially the stack pointer (for instance w.r.t. caching and instruction scheduling). They probably have some circuits dedicated to stack pointers changes required by programming language exceptions.
Some languages and implementations have better exception semantics and machinery than others. For example, Ocaml exception handling is faster than C++ one (at least with the GCC compiler).
通常有两种类型的异常处理:
我假设您正在谈论第一个。截至目前,不,我认为没有任何处理器有这样的支持。尽管我不是硬件设计师,但以下是我的论点,说明为什么会出现这种情况,以及为什么异常处理可能永远不会在硬件中实现。
因此,添加对异常处理的硬件支持实际上并不能帮助正确的用户群。
There are generally two types of exception handling:
I'm assuming you're talking about the first one. As of right now, no I don't think any processor has such support. Although I'm not a hardware designer, here are my arguments for why this is the case, as well as why exception handling may never be implemented in hardware.
So adding hardware support for exception handling doesn't actually help the right user-base.
我不相信他们会这么做,至少目前不会。异常处理不是机器代码的一部分,它是高级语言的一个功能,可以通过寄存器和调用堆栈、中断等轻松实现,并且编译器可以选择实现异常处理它想要。
所以是的,它总是会增加一些开销。然而,我认为向任何 CPU 指令集添加异常处理并没有什么意义。例外是指特殊情况;如果它们因放置过多而导致性能问题,则代码中存在问题。
I don't believe they do, at least not currently. Exception handling isn't part of machine code, it's a feature of higher-level languages that can be implemented fairly easily through registers and call stacks, interrupts and... other things... and a compiler can choose to implement exception handling however it wants.
So yes, it always adds some overhead. However, I don't see much of a point in adding exception-handling to any CPU instruction sets just because. Exceptions are for exceptional circumstances; if they're causing performance problems from being thrown around too much, something's wrong in the code.