caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Damien Pous <Damien.Pous@ens-lyon.fr>
To: caml-list@inria.fr
Subject: Marshal, closures, bytecode and native compilers
Date: Thu, 01 Feb 2007 19:44:06 +0100	[thread overview]
Message-ID: <87lkjhogzt.fsf@smtp.ens-lyon.fr> (raw)

Bonjour,

I found some strange difference between the native and bytecode
compilers, when Marshaling functional values:

[damien@mostha]$ cat lift.ml
let r = ref 0
let f =
  fun () -> incr r; print_int !r; print_newline()
let () = match Sys.argv.(1) with
  | "w" ->  Marshal.to_channel stdout f [Marshal.Closures]
  | "r" ->
      let g = (Marshal.from_channel stdin: unit -> unit) in
        g (); f ()
  | _ -> assert false

[damien@mostha]$ ocamlc lift.ml; ( ./a.out w | ./a.out r )
1
1
[damien@mostha]$ ocamlopt lift.ml; ( ./a.out w | ./a.out r )
1
2
[damien@mostha]$ ocamlc -version
3.09.2

In the bytecode version, the reference [r] gets marshaled along with
[f] so that the calls [f()] and [g()] respectively affect the initial
reference of the reader, and the (fresh) marshaled reference.

On the contrary in the native version, it seems that [f] is not
`closed': its code address is directly sent, and the call [g()]
affects the initial reference of the reader.

For my needs, I definitely prefer the second answer (only the address
is sent). However, if I move the declaration of the reference inside
the definition of [f], both compilers agree on the first answer: the
reference is marshaled.

[damien@mostha]$ cat refs.ml
let f =
  let r = ref 0 in
    fun () -> incr r; print_int !r; print_newline()
let () = match Sys.argv.(1) with
  | "w" ->  Marshal.to_channel stdout f [Marshal.Closures]
  | "r" ->
      let g = (Marshal.from_channel stdin: unit -> unit) in
        g (); f ()
  | _ -> assert false

[damien@mostha]$ ocamlc refs.ml; ( ./a.out w | ./a.out r )
1
1
[damien@mostha]$ ocamlopt refs.ml; ( ./a.out w | ./a.out r )
1
1


More than the different behaviour of ocamlc and ocamlopt on "lift.ml",
I am quite surprised that ocamlopt does not give the same results on
"refs.ml" and "lift.ml" : the second is just a `lambda-lifting' of the
first one!


Here come my questions:
 - How to guess how deep a functional value will be marshaled?
 - Is there a way to enforce the second behaviour, where the reference is
   not marshalled (ocamlopt lift.ml)?


Cimer beaucoup,
Damien


             reply	other threads:[~2007-02-01 18:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-01 18:44 Damien Pous [this message]
2007-02-02  2:15 ` [Caml-list] " Jacques Garrigue

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=87lkjhogzt.fsf@smtp.ens-lyon.fr \
    --to=damien.pous@ens-lyon.fr \
    --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;
as well as URLs for NNTP newsgroup(s).