From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA14840 for caml-redistribution; Fri, 29 Jan 1999 09:47:17 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA13570 for ; Thu, 28 Jan 1999 22:25:37 +0100 (MET) Received: from beach.frankfurt.netsurf.de (beach.frankfurt.netsurf.de [194.64.181.2]) by nez-perce.inria.fr (8.8.7/8.8.7) with ESMTP id WAA19831 for ; Thu, 28 Jan 1999 22:25:35 +0100 (MET) Received: from schneemann.darmstadt.netsurf.de (board-90.darmstadt.netsurf.de [194.163.86.218]) by beach.frankfurt.netsurf.de (8.8.5/8.8.5) with ESMTP id WAA01731 for ; Thu, 28 Jan 1999 22:25:35 +0100 (MET) Received: from localhost (localhost [[UNIX: localhost]]) by schneemann.darmstadt.netsurf.de (8.8.8/8.8.8) id VAA12469 for caml-list@inria.fr; Thu, 28 Jan 1999 21:41:05 +0100 From: Gerd Stolpmann Reply-To: Gerd.Stolpmann@darmstadt.netsurf.de To: caml-list@inria.fr Subject: Re: one-time initialization Date: Thu, 28 Jan 1999 21:23:21 +0100 X-Mailer: KMail [version 0.7.9] Content-Type: text/plain References: <199901280508.AAA21644@codex.cis.upenn.edu> MIME-Version: 1.0 Message-Id: <99012821410500.10116@schneemann> Content-Transfer-Encoding: 8bit Sender: weis On Thu, 28 Jan 1999, Michael Hicks wrote: >I wonder if anyone knows how to optimize the following (simplified for the >sake of dicussion) situation: > >let global = ref None >let init i = > global := Some i >let f () = > match (!global) with > Some x -> x > | None -> failwith "not initialized";; >let g() = > match (!global) with > ... > >Essentially, there is some global state that is initialized once, and is >used by all functions in the module. In a more realistic situation, this >state might be initialized by reading in a file. Given that following >initialization the global state never changes, it should be conceivable to >eliminate the match and dereference; on my machine (pentium 166), the match >and dereference result in about a 30% slowdown. I've fooled around with >some things, but haven't found anything that performs better than this >straightforward approach or is any more elegant. If you encapsulate your module, there is a solution that may eliminate the need of an 'option' type (I assume that 'global' has type t ref, and that v is a known value of t): module M : sig type initialized val init : t -> initialized val f : initialized -> t val g : initialized -> ... end = struct type initialized = unit let global = ref v let init i = global := i let f () = !global let g () = ... end You can only call f and g if you have a value of type 'initialized', and your only chance to get it is to call 'init'. This is a good example how to use types as assertions about state. This simply means that the type checker prooves for you that f and g are never called before init. To get rid of dereferencing is impossible. Perhaps a mutable record is faster in some cases. -- ---------------------------------------------------------------------------- Gerd Stolpmann Telefon: +49 6151 997705 (privat) Viktoriastr. 100 64293 Darmstadt EMail: Gerd.Stolpmann@darmstadt.netsurf.de (privat) Germany ----------------------------------------------------------------------------