[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Damien Pous <Damien.Pous@e...> |
| Subject: | Marshal, closures, bytecode and native compilers |
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