[antlr-interest] "Return via Exception Technique" and VS2010

Kevin Carroll kcarroll at signmgmt.com
Sun Aug 8 08:10:03 PDT 2010


I'm having a problem that hopefully someone on this list can shed some light on.

I've implemented a tree-based interpreter in C# on .Net for a DSL using ANTLR.  I've used Terrence Parr's "Language Implementation Patterns" book for guidance.  In his chapter on tree-based interpreters, he used a technique for returning from a method call which involved wrapping the code that calls the method in a try-catch block, throwing an exception that contains the return value as the return mechanism, and catching the exception to retrieve the return value.  He states that "This mechanism isn't slow if we share the same exception object.  Only creating exceptions is expensive; throwing them is no big deal."  I implemented the technique for my return mechanism.

This technique seems to work great, and it runs acceptably fast if NOT running inside the VS2010 debugger.  However, if the same DSL script is run IN the debugger, the performance drags to a crawl.  The same script can literally run 100-1000 times slower in the debugger vs. out of the debugger.

I'm only going on a hunch (and another programmer who works with me agrees), but things seem to be pointing to the exception throw-catch situation as the culprit.

Another variable involved is that my interpreter makes extensive use of .Net 4.0's new DynamicObject.  I'm not using ExpressionTree's for my AST (yet), but some of the AST node execution methods use the DLR.

Does anyone have any insight into what might be going on, or any suggestions on ways to check what might be causing the problems?

I would appreciate any and all input.

Thanks,
Kevin Carroll


More information about the antlr-interest mailing list