Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* "C" Finalizers
@ 1999-02-10 15:27 Andrew Martin
  1999-02-14  1:39 ` doligez
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Martin @ 1999-02-10 15:27 UTC (permalink / raw)
  To: caml-list

Are there any restrictions on the things that a finalizer (the first
element of a block tagged with Final_tag) can do safely?  For instance,
can it allocate memory from the Ocaml heap?  Can it use callback to
apply a closure to a value?  Is there any interest in exploring the
posibility of introducing a general-purpose finalization mechanism into
the Ocaml language itself ?

Why do I ask?

The reason is probably more academic than practical.   I have become
curious about whether it is practical to write a BDD package in Ocaml.
Agreed, one would loose some efficiency over a "C" implementation.  But
more critical to BDD performance than the constants involved in
accessing BDDs are the heuristics used for variable ordering etc.  It
may be that a clear ocaml-style presentation is more amenable to
experimentation with reordering techniques and heuristics than a
necessarily complex "C" implementation.  If so, this might ultimately
lead to a better performing package which could always be re-implemented
(at least partially) in "C" if you wanted to shave some time of the
constants.

Unfortunately, to track the improvement gained by re-ordering, you
really need to be able to know whether a node is referenced by the
outside world.  While one could concoct various schemes involving arrays
of weak pointers to accomplish this, they all feel complicated and
contrived -- they very thing I was trying to avoid by writing in Ocaml
in the first place. The most natural way do deal with the problem is to
define a finalizer that recursively decrements ref counts.

Obviously, providing a general finalization mechanism in the Ocaml
language itself would be fraught with difficulty e.g. what's to prevent
the finalizer from creating new references to the object (and hence
indirectly to its descendants) that we just decided to free ?  Still,
I thought I'd raise the issue and see if anyone is thinking about it.

Regards,
Andy Martin

--
Andrew K. Martin, Ph.D.
Motorola Inc., Somerset Design Center
Networking and Computer Systems Group

phone: (512) 424-8325        6200 Bridgepoint Parkway, Building 4,
fax  : (512) 424-8846        Mail Drop OE70
email: amartin@ibmoto.com    Austin, TX 78730






^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: "C" Finalizers
  1999-02-10 15:27 "C" Finalizers Andrew Martin
@ 1999-02-14  1:39 ` doligez
  0 siblings, 0 replies; 2+ messages in thread
From: doligez @ 1999-02-14  1:39 UTC (permalink / raw)
  To: Andrew Martin; +Cc: caml-list


>From: Andrew Martin <amartin@ibmoto.com>

>Are there any restrictions on the things that a finalizer (the first
>element of a block tagged with Final_tag) can do safely?

Definitely yes.


>  For instance,
>can it allocate memory from the Ocaml heap?

No.


>  Can it use callback to apply a closure to a value?

No because that could allocate memory.


>  Is there any interest in exploring the
>posibility of introducing a general-purpose finalization mechanism into
>the Ocaml language itself ?

This is hard to do for two reasons.  First, you have to specify what
it means exactly, and what happens when the finalization function
reconnects (parts of) the data structure that is about to be freed.
Then you have to implement it without bugs.


>Unfortunately, to track the improvement gained by re-ordering, you
>really need to be able to know whether a node is referenced by the
>outside world.  While one could concoct various schemes involving arrays
>of weak pointers to accomplish this, they all feel complicated and
>contrived -- they very thing I was trying to avoid by writing in Ocaml
>in the first place.

Yet, this is what weak pointers are designed for.  Maybe it would be
easier with a library of well-chosen functions implemented on top of
the weak pointer primitives ?


> The most natural way do deal with the problem is to
>define a finalizer that recursively decrements ref counts.

That would be very inefficient, but if you really want to do it, you
should be able to write such a finalizer in C without too much work.


>Obviously, providing a general finalization mechanism in the Ocaml
>language itself would be fraught with difficulty e.g. what's to prevent
>the finalizer from creating new references to the object (and hence
>indirectly to its descendants) that we just decided to free ?  Still,
>I thought I'd raise the issue and see if anyone is thinking about it.

We do think about it from time to time, but the demand-to-difficulty
ratio is really bad.


-- Damien




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1999-02-14 18:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-02-10 15:27 "C" Finalizers Andrew Martin
1999-02-14  1:39 ` doligez

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox