-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Experiment] Exception handling performance #77568
Comments
Very good initiative. Quite some years ago I tried to implement a Smalltalk dialect on the .Net DLR. One of the issues is the flow in a language like Smalltalk that requires many non-local returns and stack-unwinds. The way to solve this is with exceptions, but performance dies. The IronRuby implementors worked this around by implementing light-weight exceptions in their language, but those are not really exceptions, but return values. Anyway, if I may put my two cents in, there should be a way to throw exceptions without:
One option would be to categorize the exception in two categories:
Another option is to let the caller decide how he wants to handle the exception. For example: try
{
throw new NoDataException();
}
quichcatch(NoDataException ex)
{
// No stack trace in ex.StackTrace
MessageBox.Show("No data.");
}
catch(Exception)
{
....
} During first-chance exception handling, you could examine the handler if they care about the full exception details, or like the example, they just want to do a quick catch. And if we are on the subject of exceptions, I know this is a major thing, but please consider resumable exceptions. Resumable exceptions can be thrown, but the first-chance exception handler can examine the exception and decide to continue with the next statement without unwiding the stack. |
Thanks for the report.
I tested the |
I did an experiment with implementing a trivial cache for Now, the trivial implementation was intentionally a bit too trivial. I used a global variable with a lock. That's still creating a bottleneck of sorts, and it ignores shared library unloading. That said, if we assume that the code on the stack won't unload during exception unwinding, the potential for improvement is quite significant for a rather trivial change. It doesn't even need any modification in llvm-libunwind itself. (Perhaps we can just lookup the unwind sections in UnixCodeManager once and just reuse them everywhere we know the address to be a managed code.) |
I have rewritten the The exceptions per second is ~145000 now, so roughly 7x faster. Diff: https://github.com/dotnet/runtime/compare/main...filipnavara:runtime:cache_unwind_sections?expand=1 |
I wonder if there can be some kind of "pgo"-like exception handling. Like locating the most exception-frequent paths and somehow optimizing those even further? |
I'm the current maintainer of IKVM. This would be lovely. ;) Stack walking performance in .NET has always been pretty terrible. But, given the nature of IKVM, I've noticed it a lot lately. Enough that I googled about it and found this issue. I don't know anything about the internals of the CLR on this. But on the JVM, it tends to be so efficient that it's used for numerous tasks, even security checks to determine the calling path, etc. |
Closing since this has completed. |
Historically, we have considered exception handling performance to not to be important. The point was that exceptions should be used for exceptional cases handling and thus they should not have significant impact on well behaved applications. However, a new scenario where this is not true has surfaced recently. When a service depends on a resource that is inherently unreliable, for example a computer network, random temporary failures in such a resource can trigger a failure storm in the service and potentially other services that depend on that service. In this case, the performance of the exception handling is very likely an important factor for fast recovery of these services.
Moreover, the fact that such cases would most likely involve async patterns, the exception handling performance issues get amplified by the fact that the exceptions are rethrown on async state transitions.
A customer has reported that JAVA exception handling is about 10 times better on the same hardware (#12892). We also know that native AOT has much better exception handling performance than CoreCLR.
Goals
The text was updated successfully, but these errors were encountered: