digitalmars.D - What does nothrow buy me?
- Q. Schroll (7/7) Oct 26 2020 Obviously, nothrow documents the behavior of a function and is
- Tobias Pankrath (2/9) Oct 26 2020 Isn't proper unwinding of the stack only required for Exceptions?
- H. S. Teoh (11/22) Oct 26 2020 Yes. The compiler is permitted to skip stack unwinding code when
- Joseph Rushton Wakeling (9/16) Oct 27 2020 Another use-case is when you have functions or methods that are
- Atila Neves (4/11) Oct 27 2020 It's also extremely useful for when D code is being called from
- H. S. Teoh (8/18) Oct 27 2020 But how does it help in the case of throwing an Error from a function
- Atila Neves (6/24) Oct 28 2020 Unfortunately, it doesn't. But errors are supposed to mean a
- Steven Schveighoffer (7/34) Oct 28 2020 How does it make you write a catch block? If you only call nothrow
- Atila Neves (6/31) Nov 07 2020 Because the code I call from the nothrow function isn't nothrow
- Max Haughton (6/13) Oct 27 2020 If the compiler doesn't have to think about recovery (i.e. errors
- random (14/21) Nov 08 2020 This is a bit old but anyway.
- H. S. Teoh (12/27) Nov 08 2020 The reason it's a bad idea to catch Throwable is because the compiler is
- Steven Schveighoffer (12/26) Nov 09 2020 I think this is slightly incorrect. If you throw an Error inside a
Obviously, nothrow documents the behavior of a function and is checked when the code is available to the compiler. At first glance, one could assume a nothrow function can save the compiler from the need to generate exception handling stuff for a function. But since nothrow doesn't mean the function won't throw, but merely that it won't throw an Exception, what optimizations does nothrow enable?
Oct 26 2020
On Monday, 26 October 2020 at 18:47:41 UTC, Q. Schroll wrote:Obviously, nothrow documents the behavior of a function and is checked when the code is available to the compiler. At first glance, one could assume a nothrow function can save the compiler from the need to generate exception handling stuff for a function. But since nothrow doesn't mean the function won't throw, but merely that it won't throw an Exception, what optimizations does nothrow enable?Isn't proper unwinding of the stack only required for Exceptions?
Oct 26 2020
On Mon, Oct 26, 2020 at 06:52:40PM +0000, Tobias Pankrath via Digitalmars-d wrote:On Monday, 26 October 2020 at 18:47:41 UTC, Q. Schroll wrote:Yes. The compiler is permitted to skip stack unwinding code when throwing Errors. So yes, nothrow does win you some optimizations in some cases. That's one of the reasons user code generally shouldn't catch Errors -- because some unwinding code may have been skipped and your state may be inconsistent. Continuing normal execution after catching an Error is definitely a no-no. T -- Life is too short to run proprietary software. -- Bdale GarbeeObviously, nothrow documents the behavior of a function and is checked when the code is available to the compiler. At first glance, one could assume a nothrow function can save the compiler from the need to generate exception handling stuff for a function. But since nothrow doesn't mean the function won't throw, but merely that it won't throw an Exception, what optimizations does nothrow enable?Isn't proper unwinding of the stack only required for Exceptions?
Oct 26 2020
On Monday, 26 October 2020 at 18:47:41 UTC, Q. Schroll wrote:Obviously, nothrow documents the behavior of a function and is checked when the code is available to the compiler. At first glance, one could assume a nothrow function can save the compiler from the need to generate exception handling stuff for a function. But since nothrow doesn't mean the function won't throw, but merely that it won't throw an Exception, what optimizations does nothrow enable?Another use-case is when you have functions or methods that are designed to be run as the entry point of a separate thread or fiber task. An uncaught exception can cause the thread (or whole program) to hang, so having these functions guaranteed `nothrow` can be valuable. Of course, this is a bit of a workaround for the lack of decent exception handling for these cross-thread/fiber cases, but that's a bigger technical discussion...
Oct 27 2020
On Monday, 26 October 2020 at 18:47:41 UTC, Q. Schroll wrote:Obviously, nothrow documents the behavior of a function and is checked when the code is available to the compiler. At first glance, one could assume a nothrow function can save the compiler from the need to generate exception handling stuff for a function. But since nothrow doesn't mean the function won't throw, but merely that it won't throw an Exception, what optimizations does nothrow enable?It's also extremely useful for when D code is being called from other languages. Throwing an exception from a function registered with Python is not a thing one wants to do, for instance.
Oct 27 2020
On Tue, Oct 27, 2020 at 06:10:45PM +0000, Atila Neves via Digitalmars-d wrote:On Monday, 26 October 2020 at 18:47:41 UTC, Q. Schroll wrote:[...]But how does it help in the case of throwing an Error from a function registered with Python? Arguably, that's something you even *more* wouldn't want to do. T -- Why waste time reinventing the wheel, when you could be reinventing the engine? -- Damian ConwayAt first glance, one could assume a nothrow function can save the compiler from the need to generate exception handling stuff for a function. But since nothrow doesn't mean the function won't throw, but merely that it won't throw an Exception, what optimizations does nothrow enable?It's also extremely useful for when D code is being called from other languages. Throwing an exception from a function registered with Python is not a thing one wants to do, for instance.
Oct 27 2020
On Tuesday, 27 October 2020 at 18:21:23 UTC, H. S. Teoh wrote:On Tue, Oct 27, 2020 at 06:10:45PM +0000, Atila Neves via Digitalmars-d wrote:Unfortunately, it doesn't. But errors are supposed to mean a non-recoverable problem anyway. I know it's not always like that in practice and that you don't want to crash someone's Excel sheet, but... making them nothrow makes me write a catch block, which makes me also catch errors.On Monday, 26 October 2020 at 18:47:41 UTC, Q. Schroll wrote:[...]But how does it help in the case of throwing an Error from a function registered with Python? Arguably, that's something you even *more* wouldn't want to do. TAt first glance, one could assume a nothrow function can save the compiler from the need to generate exception handling stuff for a function. But since nothrow doesn't mean the function won't throw, but merely that it won't throw an Exception, what optimizations does nothrow enable?It's also extremely useful for when D code is being called from other languages. Throwing an exception from a function registered with Python is not a thing one wants to do, for instance.
Oct 28 2020
On 10/28/20 8:52 AM, Atila Neves wrote:On Tuesday, 27 October 2020 at 18:21:23 UTC, H. S. Teoh wrote:How does it make you write a catch block? If you only call nothrow functions, then your function is nothrow. If you assert in your nothrow function, it won't complain about catching. How does it make you write a catch block that catches Errors? Catching Exception is sufficient to shut up the compiler. -SteveOn Tue, Oct 27, 2020 at 06:10:45PM +0000, Atila Neves via Digitalmars-d wrote:Unfortunately, it doesn't. But errors are supposed to mean a non-recoverable problem anyway. I know it's not always like that in practice and that you don't want to crash someone's Excel sheet, but... making them nothrow makes me write a catch block, which makes me also catch errors.On Monday, 26 October 2020 at 18:47:41 UTC, Q. Schroll wrote:[...]But how does it help in the case of throwing an Error from a function registered with Python? Arguably, that's something you even *more* wouldn't want to do. TAt first glance, one could assume a nothrow function can > save thecompiler from the need to generate exception > handling stuff for a function. But since nothrow doesn't > mean the function won't throw, but merely that it won't > throw an Exception, what optimizations does nothrow enable? It's also extremely useful for when D code is being called from other languages. Throwing an exception from a function registered with Python is not a thing one wants to do, for instance.
Oct 28 2020
On Wednesday, 28 October 2020 at 14:46:40 UTC, Steven Schveighoffer wrote:On 10/28/20 8:52 AM, Atila Neves wrote:Because the code I call from the nothrow function isn't nothrow itself. It's true that it won't force me to catch errors, but I tend to in wrapper code nearly automatically. I realise now I didn't explain it well before.On Tuesday, 27 October 2020 at 18:21:23 UTC, H. S. Teoh wrote:How does it make you write a catch block? If you only call nothrow functions, then your function is nothrow. If you assert in your nothrow function, it won't complain about catching. How does it make you write a catch block that catches Errors? Catching Exception is sufficient to shut up the compiler.On Tue, Oct 27, 2020 at 06:10:45PM +0000, Atila Neves via Digitalmars-d wrote:Unfortunately, it doesn't. But errors are supposed to mean a non-recoverable problem anyway. I know it's not always like that in practice and that you don't want to crash someone's Excel sheet, but... making them nothrow makes me write a catch block, which makes me also catch errors.[...][...][...]But how does it help in the case of throwing an Error from a function registered with Python? Arguably, that's something you even *more* wouldn't want to do. T
Nov 07 2020
On Monday, 26 October 2020 at 18:47:41 UTC, Q. Schroll wrote:Obviously, nothrow documents the behavior of a function and is checked when the code is available to the compiler. At first glance, one could assume a nothrow function can save the compiler from the need to generate exception handling stuff for a function. But since nothrow doesn't mean the function won't throw, but merely that it won't throw an Exception, what optimizations does nothrow enable?If the compiler doesn't have to think about recovery (i.e. errors should result in the program's termination) then the control flow analysis and register allocation can be improved. It's hard to measure because of the implementation of the function, but you can see it in the assembly sometimes.
Oct 27 2020
On Monday, 26 October 2020 at 18:47:41 UTC, Q. Schroll wrote:Obviously, nothrow documents the behavior of a function and is checked when the code is available to the compiler. At first glance, one could assume a nothrow function can save the compiler from the need to generate exception handling stuff for a function. But since nothrow doesn't mean the function won't throw, but merely that it won't throw an Exception, what optimizations does nothrow enable?This is a bit old but anyway. When you call functions which potentially throw, the compiler has to generate landing pads to handle exceptions if: 1. You have a catch block. 2. You have to call destructors. This does not directly impact runtime performance but it bloats the executable code (and it will probably impact runtime performance because of caching). I'm not a compiler expert, so take this with a grain of salt. I found this very helpful to understand how exceptions work: https://monoinfinito.wordpress.com/2013/04/23/c-exceptions-under-the-hood-14-multiple-landing-pads-the-teachings-of-the-guru/ I linked article 14 with landing pads but you should probably start at the beginning ;)
Nov 08 2020
On Sun, Nov 08, 2020 at 10:00:28PM +0000, random via Digitalmars-d wrote:On Monday, 26 October 2020 at 18:47:41 UTC, Q. Schroll wrote:[...]The reason it's a bad idea to catch Throwable is because the compiler is *not* obligated to generate exception handling stuff in nothrow functions, even if a Throwable ends up going through that function.. [...]At first glance, one could assume a nothrow function can save the compiler from the need to generate exception handling stuff for a function. But since nothrow doesn't mean the function won't throw, but merely that it won't throw an Exception, what optimizations does nothrow enable?When you call functions which potentially throw, the compiler has to generate landing pads to handle exceptions if: 1. You have a catch block. 2. You have to call destructors. This does not directly impact runtime performance but it bloats the executable code (and it will probably impact runtime performance because of caching).[...] Nothrow functions can have exception handling code elided. So in that case it will not incur the runtime cost. T -- Computers aren't intelligent; they only think they are.
Nov 08 2020
On 11/8/20 6:53 PM, H. S. Teoh wrote:On Sun, Nov 08, 2020 at 10:00:28PM +0000, random via Digitalmars-d wrote: > [...]I think this is slightly incorrect. If you throw an Error inside a nothrow function, it will properly run destructors, and you can catch the Error inside the function. I would say as well, if you catch an Error or Throwable resulting from a call to a nothrow function, it should always generate that handling code. But I think the spec is written such that it might elide the exception handling code (I don't see this ever happening). The main case is when you *don't* catch Error or Exception when calling a nothrow function, and your code does cleanup via RAII, the cleanup will not happen. -SteveWhen you call functions which potentially throw, the compiler has to generate landing pads to handle exceptions if: 1. You have a catch block. 2. You have to call destructors. This does not directly impact runtime performance but it bloats the executable code (and it will probably impact runtime performance because of caching).[...] Nothrow functions can have exception handling code elided. So in that case it will not incur the runtime cost.
Nov 09 2020