caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Marshal, closures, bytecode and native compilers
@ 2007-02-01 18:44 Damien Pous
  2007-02-02  2:15 ` [Caml-list] " Jacques Garrigue
  0 siblings, 1 reply; 2+ messages in thread
From: Damien Pous @ 2007-02-01 18:44 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-02-02  2:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-01 18:44 Marshal, closures, bytecode and native compilers Damien Pous
2007-02-02  2:15 ` [Caml-list] " Jacques Garrigue

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).