[mercury-users] exceptions considered bloody annoying

Richard A. O'Keefe ok at cs.otago.ac.nz
Wed Apr 17 14:21:47 AEST 2002

	Okasaki was referring to the implementation of the set insertion
	operation, not its interface.  This use of exceptions is purely for
	efficiency and entirely invisible to the caller.
This assumes (a) and ((b) or (c)) where
(a) setting up an exception handler is extremely cheap
(b) adding something to a set it's already in is rare
(c) raising and handling an exception is cheap.

In some exception handling implementations, (a) is false.
This makes the use of exception handling *in*efficient.
For example,
 - in Quintus Prolog, setting up an exception handler costs the same
   as pushing a choice-point (in fact it _is_ pushing a choice-point).
   This is enough to be worth avoiding when one can, and QP and the
   QP libraries carefully made no use of exceptions except for handling
   real problems.

 - in Ada, exception handling may inhibit certain optimisations.
   (The reports that were produced as part of the Ada83->Ada95
   transition came to nearly a metre.  I still have them somewhere.
   This is described in one of those.)

 - in compilers that target C, setting up an exception handler using
   setjmp() incurs _both_ kinds of penalty:  setjmp() isn't free AND
   you have to declare a lot of variables 'volatile', inhibiting most

Assume, as is said to be the case in C++, that setting up an exception
handler has zero cost.  (It doesn't.  The protected code has to be a
single chunk, which inhibits certain optimisations, and the region map
has to be present in (virtual) memory, which costs space.)  This comes
at a price:  raising and catching exceptions is more expensive.  So we
are assuming that these exceptions are rare.

But in many of my uses of sets, adding an element to a set is NOT a
rare occurrence.  It is very common.  Indeed, it's often the reason
that I'm building a set in the first place.

In short, the *implementation* of a set data structure should be
so designed that it efficiently supports 'adding' an element that
is already present.

(Add to this the peeve that one of the languages I would have liked to
use Okasaki's code with doesn't _have_ any exception handling facility
at all...)

    The "Exceptions should be exceptional" principle.

    Use exceptions *ONLY* to handle exceptional events,
    not for normal control flow.

		(idea cribbed from the Ada Quality and Style manual,
		and from just about every software engineer who has
		written about exceptions)
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe

More information about the users mailing list