From: Gerd Stolpmann <Gerd.Stolpmann@darmstadt.netsurf.de>
To: caml-list@inria.fr
Subject: Re: one-time initialization
Date: Thu, 28 Jan 1999 21:23:21 +0100 [thread overview]
Message-ID: <99012821410500.10116@schneemann> (raw)
In-Reply-To: <199901280508.AAA21644@codex.cis.upenn.edu>
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
----------------------------------------------------------------------------
prev parent reply other threads:[~1999-01-29 8:47 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-01-28 5:08 Michael Hicks
1999-01-28 9:54 ` John Prevost
1999-01-28 14:47 ` Michael Hicks
1999-01-28 10:38 ` Xavier Leroy
1999-01-28 13:03 ` Adam P. Jenkins
1999-01-28 20:23 ` Gerd Stolpmann [this message]
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=99012821410500.10116@schneemann \
--to=gerd.stolpmann@darmstadt.netsurf.de \
--cc=caml-list@inria.fr \
/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