Fabulous Adventures In Coding

Eric Lippert's Blog

Long jumps considered way more harmful than exceptions

Bob Congdon's blog (http://www.bobcongdon.net/blog/) points out that in the dark days before exception handling you could always use setjmp/longjmp to do non-local gotos.

 

In fact, the script engines are compiled in C++ with exception handling turned off (for performance reasons), and the mainline loop of the bytecode interpreter uses setjmp-longjmp exception handling to implement error handling.  When you have a script that calls an object that returns an error, we longjmp back to the start of the interpreter loop and then figure out what to do next. 

 

In VBScript of course it depends on whether On Error Resume Next is on or not, and in JScript we construct an exception object and start propagating it back up the stack until we find an interpreter frame that has a catch block.  (If there are multiple script engines on the stack then things get extremely complicated, so I won't even go there.)

 

Since a long jump does not call any destructors, it was very important that we design our interpreter loop to not put anything on the system stack that required destructing.  Fortunately, since we were designing the interpreter to be an interpreter for a garbage-collected language, it was pretty easy.  Everything that the interpreter does that requires memory either takes the memory out of the area reserved for the script's stack (which will be cleaned up when the frame goes away) or heap-allocates it and adds the memory to the garbage collector.

 

Not everyone has the luxury of having a longjmp-safe garbage collector already implemented, so kids, don't try this at home!  If you must use exception handling in C++, take my advice and use real C++ exception handling.

Published Thursday, October 16, 2003 6:20 PM by Eric Lippert

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

MM said:

What was the measured cost when C++ exceptions were turned on?
October 17, 2003 12:35 AM
 

RJ said:

"In fact, the script engines are compiled in C++ with exception handling turned off (for performance reasons)" It would be interesting to know *which* performance reasons these were after your reply to my comment in "I take exception to that" - sorry for being facetious 8*)
October 17, 2003 4:30 AM
 

Eric Lippert said:

> What was the measured cost when C++ exceptions were turned on? You're asking me for the numbers which we measured on Pentium 1's seven years ago? I haven't got them on me, sorry. But it was significant. > It would be interesting to know *which* performance reasons these were The overall cost, not the per-exception cost, killed our performance on the server. Exception handling adds additional code to every function, and that additional code has a nonzero run time. Since the purpose of the code -- handling exceptions -- was NEVER going to be fulfilled becasue we never THREW an exception, we turned it off.
October 17, 2003 11:29 AM
 

RJ said:

Sorry if it appears I am being awkward, I'm not - I'm just interested in learning to be optimal :)
October 17, 2003 1:12 PM

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

About Eric Lippert

Eric Lippert is a senior developer on the Microsoft C# compiler team. Before that he worked on the framework of Visual Studio Tools For Office. Before that, he worked on the compilers, runtimes and tools for VBScript, JScript, Windows Script Host and other Microsoft Scripting technologies. He lives in Seattle and spends his free time editing books about programming languages, playing the piano, and trying to keep his tiny sailboat upright in Puget Sound.

This Blog

Syndication


© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker