From: "David Allsopp" <dra-news@metastack.com>
To: <rixed@happyleptic.org>, "'OCaml'" <caml-list@inria.fr>
Subject: RE: [Caml-list] lazy vs fun
Date: Tue, 25 Aug 2009 07:29:35 +0100 [thread overview]
Message-ID: <000301ca254d$6d4aab40$47e001c0$@metastack.com> (raw)
In-Reply-To: <20090825051958.GA2066@happyleptic.org>
rixed@happyleptic.org wrote:
> > Oops.
> > The following makes it possible for f to be garbage-collected:
>
> ...?
> Because the fact that the fun calls f does not count as a reference ?
The anonymous function in the second version of Martin's function doesn't
"call" [f] (at least directly): imagine if the `None case had been written
with [g]s instead:
let lz f =
let result = ref (`None f) in
fun () ->
match !result with
`None g ->
(try
let y = g () in
result := `Result y;
y
with e ->
result := `Exn e;
raise e
)
| `Result y -> y
| `Exn e -> raise e
The [fun] only "calls" (uses) [result]. In the first version, [f] could only
be collected after the [fun] had been collected (i.e. when the lazy value
itself is collected - not good). In the second version, [f] can be garbage
collected if the lazy value has already been forced (meaning that "[result =
`Result y | `Exn e]" so there will be no more references to [f]) - a clear
optimisation with no performance penalty in terms of space.
The optimisation would be irrelevant if the ocaml compiler was lazy and just
included all of the scope in the closure for [fun], but minimising (and
eliminating, where possible) closures is a critical part of ML compilers
(I'd be amazed if it's not covered in all of the books you've mentioned
previously and I wish I could remember the name usually given to the
problem... but I'm sure someone will chip in with it!)
David
next prev parent reply other threads:[~2009-08-25 6:29 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-24 21:57 Warren Harris
2009-08-24 22:04 ` [Caml-list] " Jake Donham
2009-08-24 22:15 ` Warren Harris
2009-08-24 22:18 ` Jake Donham
2009-08-24 22:06 ` Stéphane Glondu
2009-08-24 22:19 ` Martin Jambon
2009-08-24 23:11 ` Martin Jambon
2009-08-24 23:33 ` Warren Harris
2009-08-25 5:19 ` rixed
2009-08-25 6:29 ` David Allsopp [this message]
2009-08-25 18:09 ` rixed
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='000301ca254d$6d4aab40$47e001c0$@metastack.com' \
--to=dra-news@metastack.com \
--cc=caml-list@inria.fr \
--cc=rixed@happyleptic.org \
/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