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

Sam Harwell sharwell at pixelminegames.com
Mon Aug 9 13:47:40 PDT 2010


Regarding Java, take a look at this Stack Overflow question:

http://stackoverflow.com/questions/762330/do-singleton-exceptions-work

I'm generally of the mindset that exceptions should be avoided where
possible, but some answers to that question suggest possible uses.

Sam

-----Original Message-----
From: Kevin Carroll [mailto:kcarroll at signmgmt.com] 
Sent: Monday, August 09, 2010 5:30 AM
To: Sam Harwell; antlr-interest at antlr.org
Subject: RE: [antlr-interest] "Return via Exception Technique" and VS2010

Thanks, Sam.  I'll try to implement an "exception-free" technique for
handling method returns.  

For clarification, are you saying that TP's pattern might be acceptable if
the implementation language is Java but definitely not if C#?  If so, a
recommendation for a disclaimer in the book might be in order.

Thanks,
Kevin

-----Original Message-----
From: Sam Harwell [mailto:sharwell at pixelminegames.com] 
Sent: Sunday, August 08, 2010 1:58 PM
To: Kevin Carroll; antlr-interest at antlr.org
Subject: RE: [antlr-interest] "Return via Exception Technique" and VS2010

First of all, you should absolutely not be using exception handling for flow
control. If you are, there's a critical design error involved and you should
fix it before worrying about other things. Second, the way Java handles
exceptions is different from the way the CLR handles them. The CLR builds
the stack trace when the exception is thrown, reducing the possibility that
exceptions will be misused by reusing the exception object. Exception
handling is for errors - find another (proper) way to return your values and
both your performance and design problems will be fixed.

Sam

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Kevin Carroll
Sent: Sunday, August 08, 2010 10:10 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] "Return via Exception Technique" and VS2010

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

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address



More information about the antlr-interest mailing list