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 UAA08697 for caml-redistribution; Thu, 28 Jan 1999 20:58:23 +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 OAA16243 for ; Thu, 28 Jan 1999 14:07:17 +0100 (MET) Received: from xcom-78-133.mdc.net (xcom-78-133.mdc.net [209.251.78.133]) by nez-perce.inria.fr (8.8.7/8.8.7) with ESMTP id OAA11316 for ; Thu, 28 Jan 1999 14:07:09 +0100 (MET) Received: (from ajenkins@localhost) by xcom-78-133.mdc.net (8.8.7/8.8.7) id IAA00542; Thu, 28 Jan 1999 08:03:56 -0500 Date: Thu, 28 Jan 1999 08:03:56 -0500 Message-Id: <199901281303.IAA00542@xcom-78-133.mdc.net> From: "Adam P. Jenkins" MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: Michael Hicks Cc: caml-list@inria.fr Subject: one-time initialization In-Reply-To: <199901280508.AAA21644@codex.cis.upenn.edu> References: <199901280508.AAA21644@codex.cis.upenn.edu> X-Mailer: VM 6.35 under Emacs 20.2.1 Sender: weis Michael Hicks writes: > 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're reading the state from a file, then you could make global not be a ref by writing let global = readState "initFile" You could even store the init filename in an environment variable so that readState could read a different file on different program invocations. As far as getting rid of the pattern matching, if the state can only be one of several choices, then you could make several versions of f() and g(), one for each pattern, and assign them from readState. That is. let (global, f, g) = readState "initFile" where readState returns different functions for f() and g() depending on what state it reads. Adam