Ian Rogers wrote:
> > My comments on exceptions are NOT VM specific. Exceptions should be
> > "exceptional". From a VM designer point of view, the last thing to
> > optimize (in priority) is exceptions.
>
> I don't know. Saying exceptions are exceptional is one way of viewing
> there use. An alternative way of programming is to write code assuming no
> problems will occur during execution. Then go back and catch the cases
> where things go "wrong" (exceptions to the normal way things happen). Often
> things go "wrong" more often than we like and we end up with lots of
> exceptions.
I have to agree that the line between where to use exceptions, or not,
can be difficult to draw, specially when implementing a class library.
My point was to raise the design principle behind exceptions, which goes
beyond Java. It is true for other languages (C++ comes to mind) and
even the principle even extends to Operating Systems.
> I think it is important for a VM to optimise exceptions :-)
I did not say that exceptions should not be optimized, but sometimes a
system designer has to make choices. Here's a example:
What should a vm designer choose between the following two alternatives:
1- Gain 2 processor cycles on every "GETFIELD" bytecode, and slow down
null pointer exceptions by 1000 cycles.
2- Lose 2 processor cycles on every "GETFIELD" bytecode, and gain 1000
cycles on null pointer exceptions.
My answer would be 1. Why? because GETFIELD bytecodes will be on the
normal path of programs. (I would think: too bad for programmers that
use exceptions for testing the null property on the critical path of
their program).
Lastly, I would stress that throwing an exception is a very costly
matter:
(1) An exception object needs to be created [gc pressure]
(2) The constructor of the exception object is called [execution time]
(3) The stack trace need to be saved [execution time + gc pressure]
(4) The vm needs to track down an exception handler [execution time]
Some Java interpreters/JITs might be able to optimize this, sometimes,
by avoiding the whole object creation in some cases (like the empty
catch clause of your example, in presence of inlining). But, now, you
are down to optimizing a program for a specific vm, which is not
necessarily a good idea if you expect your program/library to run on
various vms. (Anyway, I suspect such vm to translate the exception
detection into an explicit "if" test, that you tried to avoid in the
first place;-)
So, the choice of using exceptions should not be lightly made, that is,
in the final implementation of a system. I agree with you, that for a
system in development, exceptions can simplify many things, and thus
optimizing their usage can be done once you have something that actually
works;-)
Etienne
--
----------------------------------------------------------------------
Etienne M. Gagnon, M.Sc. e-mail: [EMAIL PROTECTED]
Author of SableVM: http://www.sablevm.org/
----------------------------------------------------------------------