request switch statement with common block

Jonathan M Davis jmdavisProg at gmx.com
Sat Aug 3 15:18:49 PDT 2013


On Saturday, August 03, 2013 14:49:30 H. S. Teoh wrote:
> On Sat, Aug 03, 2013 at 02:34:51PM -0700, Jonathan M Davis wrote:
> > On Saturday, August 03, 2013 14:25:24 H. S. Teoh wrote:
> > > On Sat, Aug 03, 2013 at 01:33:44PM -0700, Jonathan M Davis wrote:
> [...]
> 
> > > > I definitely use scope from time to time, but I don't know how
> > > > frequently I use it in comparison to how often I should use it. I
> > > > was actually using scope(success) more until someone pointed out
> > > > that there's an efficiency hit with that as it was putting
> > > > try-catch blocks in places where there wouldn't have been before
> > > > (as opposed to scope(exit) or scope(failure) where you'd be using
> > > > try-catch blocks if you weren't using scope).
> > > 
> > > [...]
> > > 
> > > Hmm, I never thought of that. So that limits the use cases of scope
> > > guards even more. :-P
> > 
> > Yeah. For instance, I'd made it so that RedBlackTree was using
> > scope(success) for mutating its length in a few places, which helped
> > make the code cleaner (because you didn't have to worry about
> > incrementing the length at each of the various return statements), but
> > that introduced try-catch blocks where there otherwise wouldn't have
> > been any, and that code definitely needs to be as efficient as
> > possible, so the scope(success) statements were removed from there.
> > Other code wouldn't care as much, but for something like RedBlackTree,
> > it really matters. It is a bit of a shame though, since it made the
> > code cleaner.
> 
> [...]
> 
> On second thoughts, why is scope(success) implemented as try/catch?
> Shouldn't the compiler be able to insert it before the end of each block
> instead? Stack-unwinding bypasses the end of block code, so the only way
> you can get there is by successful exit.

Hmmm. For a scope(success) statement to work, the code has to be put at every 
successful exit point from the scope. In the simple case, that means putting 
it at the end of the try portion of a try-catch block, but the try-catch isn't 
strictly necessary if there is no scope(failure) or scope(exit) - though I 
don't know what dmd currently does.

Where scope(success) is useful is when you have multiple return statements and 
want something to occur when they return before exiting the function (or 
possibly the same thing but with break or continue statements inside a loop). 
In that case, try-catch doesn't help at all, beacause in all cases, you're 
forced to either duplicate the code in the scope(success) block at each point 
that the code exits the scope or have a common block with a goto at each exit 
point.

Yeah. I don't see any need for try-catch there. So, it shouldn't be a problem. 
I know that it was discussed with regards to a pull for RedBlackTree, but I 
don't think that it was a long discussion, and I guess that it wasn't long 
enough. Thinking through it now, I really don't see a problem with it (though 
maybe I'm missing something). But I'd have to investigate what dmd actually 
does to see what the actual efficiency impact is. It wouldn't surprise me at all 
if dmd currently inserts useless try-catch blocks or something like that 
(though hopefully it doesn't). But assuming that dmd's implementation is 
appropriately efficient, I don't see any problem with scope(success), so I may 
start using it again (though I should probably investigate what code it 
generates first).

- Jonathan M Davis


More information about the Digitalmars-d mailing list