val _ = () and exception optimization in MLton

Henry Cejtin henry@sourcelight.com
Fri, 11 Aug 2000 16:15:07 -0500


I  guess  that  I  was  assuming that it was already true that if the handled
expression made no procedure calls then the list  of  active  handlers  would
never be changed.

Still,  I  really  am  not  convinced  that this optimization is going to buy
anything.  The example you give is misleading because the handler handles all
exceptions.   Also note that the transformation you have used can really hurt
the common case where no  exception  is  raised  (because  now  the  cost  of
establishing a handler is unbounded).  I.e., as I understand it code like

    let fun inner n =
               if n = 0
                  then ()
                  else inner (n - 1)
        fun outer n =
               if n = 0
                  then ()
                  else (inner n;
                        outer (n - 1))
    in outer 1000 handle _ => ()
    end

Will  put  up  and tear down the handler 10^6 times, while the old code would
have  done  it  once.   (Note,  this  also  shows  another  reason  why   the
optimization  doesn't  buy  much.   Even  arithmetic can raise an exception.)
(All of the above for the case where outer does NOT get inlined.)