From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=AWL,SPF_SOFTFAIL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 3EC64BC6B for ; Sun, 9 Dec 2007 22:57:36 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAABjzW0fAbSoIh2dsb2JhbACPYwIBCAopgRQ X-IronPort-AV: E=Sophos;i="4.23,274,1194217200"; d="scan'208";a="6642461" Received: from einhorn.in-berlin.de ([192.109.42.8]) by mail3-smtp-sop.national.inria.fr with ESMTP; 09 Dec 2007 22:57:35 +0100 X-Envelope-From: oliver@first.in-berlin.de X-Envelope-To: Received: from einhorn.in-berlin.de (localhost [127.0.0.1]) by einhorn.in-berlin.de (8.13.6/8.13.6/Debian-1) with ESMTP id lB9LvYkK013962 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Sun, 9 Dec 2007 22:57:35 +0100 Received: (from www-data@localhost) by einhorn.in-berlin.de (8.13.6/8.13.6/Submit) id lB9LvY9D013960 for caml-list@yquem.inria.fr; Sun, 9 Dec 2007 22:57:34 +0100 X-Authentication-Warning: einhorn.in-berlin.de: www-data set sender to oliver@first.in-berlin.de using -f Received: from dslb-088-073-089-167.pools.arcor-ip.net (dslb-088-073-089-167.pools.arcor-ip.net [88.73.89.167]) by webmail.in-berlin.de (IMP) with HTTP for ; Sun, 9 Dec 2007 22:57:34 +0100 Message-ID: <1197237454.475c64cec866b@webmail.in-berlin.de> Date: Sun, 9 Dec 2007 22:57:34 +0100 From: Oliver Bandel To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Ask for a more efficient way to deallocate memory (full version) References: <39980.81.57.198.61.1197236388.squirrel@webmail.lpn.cnrs.fr> In-Reply-To: <39980.81.57.198.61.1197236388.squirrel@webmail.lpn.cnrs.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit User-Agent: Internet Messaging Program (IMP) 3.2.6 X-Scanned-By: MIMEDefang_at_IN-Berlin_e.V. on 192.109.42.8 X-Spam: no; 0.00; bandel:01 in-berlin:01 opendir:01 ocaml's:01 opendir:01 ocaml:01 alist:01 readdir:01 ocaml:01 garage:98 eating:98 garbage:01 garbage:01 unix:01 unix:01 Hello, Zitat von Fabrice.Pardo@lpn.cnrs.fr: > Hello, and sorry for my previous unfinished message. > > As the function Unix.opendir returns a value, > we can expect that Unix.closedir will be automatically > called when the dir_handle value is no more referenced. > That's not the case, Yes, and that's a operating system issue, not OCaml's. There btw. a long while ago was already a discussion on that. opendir does only give back a handle, and that does not need much memory. So it is not a problem for that you should use the Gc-module. the error you got says: there are not more filedescriptors available to you, and the system gives you an error because of that and OCaml then throsws you an exception. You have to open and to close by yourself. There are many possiblities to do that work. You can code it directly in a function. So, you could open the directory, read the conetnst to alist, then close the directory and give back the dircontens (list of entries) as a return value. The calling function then can collect the items. Or give a list of dierctories to the function and let it fo all opendir/readdir/closedir things, collecting all dir-contents. So, this all is possible without Gc-module, and using the Gc-module shows me that you don't understand the rpoblem at hand: it is not the garbage collector that must be used to throw away allocated data, it is the Operating-System, which needs the files to be closed. As it is not the Garbage Collector, it makes no sense to use Gc-functions here. Just open the dir, read the contents and then close the dir. That's all. > and calling opendir too many times as in > > for i = 1 to 2000 do > let d = Unix.opendir "/tmp" in () > done;; This makles no sense. Why to open directories and not use them? Do you go to the bakery, buy 2000 breads and then put them in your garage, not eating one of them? [...] > > Replacing Unix.opendir by safe_opendir: > > let safe_opendir path = > let _ = Gc.major () > and d = Unix.opendir path > in Gc.finalise Unix.closedir d > > seems a solution, but the efficiency is very low due to high > cost of garbage collector calling function. So, don't use it. > > My question is how to write a code as efficient > as it would be possible using a reference-counted language. [...] So, it seems you think OCaml is very inefficient and so you try to make coding "as efficient as possible" before you write any line of code. This makes no sense. OCaml is astouningly efficient, and you should better start to write your code and only if you come to a limit, where you really think you have to optimize, then you should. So, do you really want to open some-thousand directories? Then you can write a function, that gets a list of directories as argument and then in a loop opens --- reads (and daves data) --- closes No Gc-stuff needed. Ciao, Oliver