caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: skaller <skaller@ozemail.com.au>
Cc: Ville-Pertti Keinonen <will@exomi.com>,
	Dustin Sallings <dustin@spy.net>,
	Damien Doligez <damien.doligez@inria.fr>,
	Caml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] GC and file descriptors
Date: Sun, 16 Nov 2003 13:19:12 -0600 (CST)	[thread overview]
Message-ID: <Pine.LNX.4.44.0311161255150.5009-100000@localhost.localdomain> (raw)
In-Reply-To: <1068917444.25869.244.camel@pelican>

On 16 Nov 2003, skaller wrote:

> On Sun, 2003-11-16 at 02:56, Ville-Pertti Keinonen wrote:
> > On Sun, Nov 16, 2003 at 01:16:08AM +1100, skaller wrote:
> > 
> > > It is possible to modify the Ocaml collector to make finalisation
> > > orderly, but it is very expensive. It is also possible
> > 
> > Not just expensive, more like something you definitely don't want to
> > even try.  Python tries to have deterministic finalization, but
> > falls back on "normal" garbage collection for containers 
> 
> Funny, but I actually *needed* deterministic finalisation. 
> And the reason was .. I was implementing a Python interpreter
> in Ocaml, and needed to emulate Python's ref counting.
> But I didn't want to actually do any ref counting :-)

You can't fake reference counting with mark-and-sweep.

Here's the problem: reference counting is expensive.  In a scripting 
language this performance hit may not be problem, it may be outweighed by 
other performance limitation.  But every time some C++ programmers bitchs 
about garbage collection being slow, he's almost certainly talking about 
reference counting.

I wish the mainstream would crawl out of the sixties era computer science 
and get at least into the eighties.  The only "mainstream" programming 
language I know of with *excusable* garbage collection is Java.  Of 
course, Java's type system is state of the art- for 1968.  Which convinces 
everyone that strong typing is bad- in much the same way that reference 
counting convinces everyone that garbage collection is slow.  Grr.

> 
> Because of this problem, my own major Python program
> interscript would not run on my interpreter, since
> it relied on orderly finalisation.
> 
> The reason for *that* is that it effectively executed
> commands written by the client, and the specification
> provided for opening and writing to files, but
> not for closing them. Closing files was important
> and had to be correctly timed: for example, closing
> the main document actually generated the table of
> contents (since only at the end are all the contents
> known).

You were abusing finalization.  The fact that it worked (in reference 
counting Python) doesn't mean it was a good idea.  Accidentally having 
dangling references which delay some objects finalization could introduce 
unexpected orderings and "bugs".  A much better way to do this would be to 
have a central command queue.  Every time a new command is created, it 
adds itself to the queue.  Then you just flush the command queue- 
executing the commands in the order they were added.  This is done every 
so often while the program is running and, of course, on program exit.

> 
> Anyhow .. there do exist cases where explicit finalisation
> is difficult. 

IIRC, in the general case it's equivelent to solving the halting problem.

On a more general note, I will agree that having the GC free non-memory
resources raises problems.  I agree (and strongly beleive) that everything
that isn't memory should have some way for the program to free them
without invoking the GC- for example, file descriptors should have a close 
routine.  And I agree that programs *should* use them instead of relying 
on the GC in all but the most intractable cases.

The question is, is it worse to have the GC try to reclaim the resources
than it is to have a guarenteed leak?  Consider the case where close 
return EINTR and doesn't close the file descriptor.  Since the descriptor 
has already leaked- for it to reach this point the program no longer has 
any way to reach the descriptor (if the program did, the object wouldn't 
be being collected).  So what's the difference in this case between the 
collector silently ignoring the error, and the collector not even trying 
to free the resource?

Actually, an idea I've had is to add Java-style "throws" information- 
basically what exceptions a function can throw- to the type information of 
a function.  With type inference, this shouldn't be the headache in Ocaml 
it is in Java.  The advantage here is that you could enlist the type 
system to gaurentee that a destructor doesn't throw any exceptions- i.e. 
the code for a destructor should handle all exceptions it generates.  

-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  parent reply	other threads:[~2003-11-16 18:21 UTC|newest]

Thread overview: 95+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-13  0:50 Dustin Sallings
2003-11-13  1:18 ` David Fox
2003-11-13  4:09   ` Dustin Sallings
2003-11-14 13:42     ` Damien Doligez
2003-11-14 14:57       ` Christophe Raffalli
2003-11-14 20:24         ` Dmitry Bely
2003-11-14 20:54           ` Eric Dahlman
2003-11-14 22:21             ` Brian Hurt
2003-11-14 21:36               ` John J Lee
2003-11-14 21:48           ` Brian Hurt
2003-11-15  1:47             ` Dmitry Bely
2003-11-15  2:25           ` Max Kirillov
2003-11-15  2:49             ` Mike Furr
2003-11-16  4:09               ` [Caml-list] Bugs from ignoring errors from close (was Re: GC and file..) Tim Freeman
2003-11-15  2:58             ` [Caml-list] GC and file descriptors David Brown
2003-11-17 14:19         ` Damien Doligez
2003-11-17 18:18           ` skaller
2003-11-14 18:35       ` Dustin Sallings
2003-11-15 14:16         ` skaller
2003-11-15 15:56           ` Ville-Pertti Keinonen
2003-11-15 17:30             ` skaller
2003-11-15 20:31               ` Martin Berger
2003-11-16 19:19               ` Brian Hurt [this message]
2003-11-17 18:15                 ` skaller
2003-11-17 19:26                   ` Aleksey Nogin
2003-11-18 13:49                     ` skaller
2003-11-18 17:51                       ` Dustin Sallings
2003-11-18 20:17                       ` Aleksey Nogin
2003-11-20  7:36                         ` Florian Hars
2003-11-17 21:20                   ` Brian Hurt
2003-11-17 23:02                     ` John J Lee
2003-11-18 12:05                     ` Ville-Pertti Keinonen
2003-11-18 15:19                       ` skaller
2003-11-18 18:10                         ` John J Lee
2003-11-18 17:55                           ` skaller
2003-11-18 20:02                         ` Ville-Pertti Keinonen
2003-11-18 21:20                           ` John J Lee
2003-11-19 12:25                           ` skaller
2003-11-19 13:55                             ` Ville-Pertti Keinonen
2003-11-19 14:26                               ` Samuel Lacas
2003-11-19 14:47                               ` skaller
2003-11-18 15:28                       ` skaller
2003-11-18 18:00                       ` John J Lee
2003-11-18 22:28                       ` Brian Hurt
2003-11-18 23:07                         ` John J Lee
2003-11-18 23:22                         ` Benjamin Geer
2003-11-19  1:49                         ` Martin Berger
2003-11-19  3:57                           ` Dustin Sallings
2003-11-19 13:35                           ` skaller
2003-11-19 13:00                         ` skaller
2003-11-19 13:02                         ` skaller
2003-11-19 17:36                           ` Brian Hurt
2003-11-20  5:14                             ` skaller
2003-11-20  7:37                               ` David Brown
2003-11-18 15:12                     ` skaller
2003-11-18 16:49                       ` Martin Berger
2003-11-18 17:46                         ` skaller
2003-11-19  1:33                           ` Martin Berger
2003-11-19  3:19                             ` Design by Contract, was " Brian Hurt
2003-11-19  2:57                               ` Jacques Carette
2003-11-19 13:27                             ` skaller
2003-11-19 14:41                               ` Martin Berger
2003-11-19 16:54                             ` Richard Jones
2003-11-19 17:18                               ` Damien Doligez
2003-11-19 21:45                                 ` Richard Jones
2003-11-19 23:09                                   ` Benjamin Geer
2003-11-20  0:50                                     ` Nicolas Cannasse
2003-11-20  9:42                                       ` Benjamin Geer
2003-11-19 18:03                               ` Martin Berger
2003-11-18 18:26                         ` Benjamin Geer
2003-11-18 19:24                           ` Xavier Leroy
2003-11-18 23:49                             ` Benjamin Geer
2003-11-19  1:36                             ` Martin Berger
2003-11-19  2:28                               ` Nicolas Cannasse
2003-11-19  3:26                               ` Brian Hurt
2003-11-19 11:44                                 ` Martin Berger
2003-11-19 17:29                                   ` Brian Hurt
2003-11-20  5:17                                     ` skaller
2003-11-20 16:13                                       ` Brian Hurt
2003-11-19 13:33                               ` skaller
2003-11-19 17:01                                 ` Richard Jones
2003-11-22  2:39                                   ` [Caml-list] AutoMLI (Was: GC and file descriptors) Jim
2003-11-19 17:43                                 ` [Caml-list] GC and file descriptors Brian Hurt
2003-11-20  5:05                                   ` skaller
2003-11-19  1:33                           ` Martin Berger
2003-11-19  2:47                             ` Benjamin Geer
2003-11-18 22:23                       ` Brian Hurt
2003-11-19 13:00                         ` skaller
2003-11-17 22:37                   ` OCaml popularity [was: Re: [Caml-list] GC and file...] John J Lee
2003-11-18  1:02                   ` [Caml-list] Re: GC and file descriptors Jed Davis
2003-11-13  1:19 ` [Caml-list] " Nicolas George
     [not found] ` <87smkstkhg.fsf@igloo.phubuh.org>
     [not found]   ` <347A7A46-1612-11D8-8F93-000393CFE6B8@spy.net>
2003-11-13 20:18     ` Mikael Brockman
     [not found] <20031118232227.GA8437@swordfish>
     [not found] ` <Pine.LNX.4.44.0311182039440.5009-100000@localhost.localdomain>
2003-11-20  6:35   ` Matt Gushee
2003-11-21 16:44     ` skaller
2003-11-21 22:17 Gregory Morrisett

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.44.0311161255150.5009-100000@localhost.localdomain \
    --to=bhurt@spnz.org \
    --cc=caml-list@inria.fr \
    --cc=damien.doligez@inria.fr \
    --cc=dustin@spy.net \
    --cc=skaller@ozemail.com.au \
    --cc=will@exomi.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).