处理器是否有实际的电路来帮助异常处理?

发布于 2024-12-22 22:50:12 字数 784 浏览 2 评论 0原文

我的问题非常简单:我想知道处理器是否尝试以某种方式帮助异常处理。如果在设计“异常就绪”处理器方面付出足够的努力,是否可以完全消除异常处理和抛出的开销?

因为据我所知,所有异常处理都是通过软件完成的,这总是会增加一些开销。我错了吗?

- 编辑

所以,感谢以下所有答案。我很欣赏。你已经回答了我的问题。

但只是为了澄清我为什么问这个:一般来说,人们不会太深入地优化异常,因为他们都认为“异常是针对特殊情况的”,因此它们不是瓶颈。

我不认为只有在戏剧性的情况下才应该抛出异常。我的想法基本上是,只要函数无法遵守其承诺的操作,就应该抛出异常。

如果我说:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里人 2024-12-29 22:50:12

指令集架构文档中,异常是处理器的异常情况(例如零除、非法指令等)。它们通常被转换为中断(但大多数中断是处理器的外部信号)。

编程语言规范中,异常是非本地控制流结构,通常涉及某种调用堆栈展开。

我相信最近的微架构专门处理堆栈指针(例如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).

想你只要分分秒秒 2024-12-29 22:50:12

通常有两种类型的异常处理:

  1. 语言异常。 (您在 C++/Java 或任何其他语言中抛出的类型)
  2. 硬件异常。 (信号、段错误、未对准等...)

我假设您正在谈论第一个。截至目前,不,我认为没有任何处理器有这样的支持。尽管我不是硬件设计师,但以下是我的论点,说明为什么会出现这种情况,以及为什么异常处理可能永远不会在硬件中实现。

  1. 不同的语言有不同的异常处理协议。 C++ 可能与 Java、C# 等不同...堆栈展开和析构函数调用将使这个过程变得复杂。
  2. 异常处理是否是瓶颈?大多数高度优化的 HPC 性能关键型应用程序无论如何都不使用异常处理。大多数依赖于异常处理的面向 OOP 的代码通常会受到其他因素的瓶颈。 (例如分支、缓存、内存等...)

因此,添加对异常处理的硬件支持实际上并不能帮助正确的用户群。

There are generally two types of exception handling:

  1. The language exception. (the kind that you throw in C++/Java or any other language)
  2. Hardware exceptions. (signals, seg-faults, misalignment, etc...)

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.

  1. Different languages have different exception handling protocols. C++ maybe different from Java, C#, etc... Stack unwinding and destructor calls will complicate this process.
  2. Is exception handling even a bottleneck? The majority of the highly-optimized HPC performance-critical applications don't use exception handling anyway. Most of the OOP-oriented code that relies on exception handling is typically bottlenecked by other factors. (such as branching, cache, memory, etc...)

So adding hardware support for exception handling doesn't actually help the right user-base.

苦妄 2024-12-29 22:50:12

我不相信他们会这么做,至少目前不会。异常处理不是机器代码的一部分,它是高级语言的一个功能,可以通过寄存器和调用堆栈、中断等轻松实现,并且编译器可以选择实现异常处理它想要。

所以是的,它总是会增加一些开销。然而,我认为向任何 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文