From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id MAA31873; Sat, 27 Mar 2004 12:19:04 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id MAA30014 for ; Sat, 27 Mar 2004 12:19:03 +0100 (MET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i2RBIWHd013622; Sat, 27 Mar 2004 12:18:32 +0100 Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id MAA31629; Sat, 27 Mar 2004 12:18:32 +0100 (MET) Date: Sat, 27 Mar 2004 12:18:32 +0100 From: Xavier Leroy To: Richard Jones Cc: caml-list@inria.fr Subject: Re: [Caml-list] Delaying module initialization Message-ID: <20040327121832.B29840@pauillac.inria.fr> References: <20040326165943.GA18734@redhat.com> <20040327.022627.38703728.yoriyuki@mbg.ocn.ne.jp> <20040326184424.GA20895@redhat.com> <8765cre8el.dlv@vanicat.homelinux.org> <20040327101944.GA1941@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <20040327101944.GA1941@redhat.com>; from rich@annexia.org on Sat, Mar 27, 2004 at 10:19:45AM +0000 X-Miltered: at concorde by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 dynlink:01 dynamically:01 dynlink:01 unreferenced:01 libs:01 dynamically:01 hassling:01 jones':01 lwp:01 useragent:99 lwp:01 useragent:99 constants:01 ocaml:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 386 > I think this must have something to do with Dynlink. The manual > states: > > All toplevel expressions in the loaded compilation units are > evaluated. Yes, indeed. Compilation units loaded at top-level or dynamically via Dynlink are always loaded and their initialization code executed, because there is no way for the system to determine whether they'll be needed later. Static linking can determine this, and therefore removes unreferenced units from .cma libraries. J. Skaller unhelpfully suggests: > Hassle the ocaml team for generation of shared libraries? Shared libs will definitely not help here (and elsewhere). By definition, linking with a shared lib is like dynamically loading all units contained in this lib, hence no way to eliminate unneeded ones. Only static linking can do that. (As far as "hassling" goes, this will only reinforce my conviction that shared libraries are useless. So, don't do that.) Coming back to Rich Jones' problem, there are two ways to go about this: 1- Since you're dynamically loading the Caml modules anyway, couldn't you arrange so that only those you really need are loaded? 2- Instead of doing let _ = eval "use LWP::UserAgent" let somefun x y = ... you could implement on-demand evaluation of the "use" bit, e.g. let inited = ref false let init() = if not !inited then begin eval "use LWP::UserAgent"; inited := true end let somefun x y = init(); ... Every function needs to be protected by a call to init(). If you export constants and not just functions, this won't work, of course, but maybe this doesn't happen too often. A nicer way to write the above is let init = lazy (eval "use LWP::UserAgent") let somefun x y = Lazy.force init; ... Hope this helps, - Xavier Leroy ------------------- 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