Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0006003OCamlOCaml backend (code generation)public2013-05-01 14:462013-05-01 15:20
Reportervbrankov 
Assigned Togasche 
PrioritynormalSeverityminorReproducibilityalways
StatusresolvedResolutionno change required 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0006003: simple references sometimes slow
DescriptionThe following two code samples show inefficiency in dealing with references.

I believe that the first example is more likely to appear in a real world code. I have a feeling that it may not be hard to fix.

The second example is more synthetic. I understand the reason and I don't have a clear idea how it could be solved.
Steps To Reproducelet () =
  let start = Unix.gettimeofday () in
  let s = ref 0. in
  for _i = 0 to 16_777_215 do
    s := !s +. 1.;
    s := !s +. 1.;
    s := !s +. 1.;
    s := !s +. 1.;
    s := !s +. 1.;
    s := !s +. 1.;
    s := !s +. 1.;
    s := !s +. 1.
  done;
  let _ = !s in
  Printf.printf "%f\n" (Unix.gettimeofday () -. start);
  let start = Unix.gettimeofday () in
  let s = ref 0. in
  for _i = 0 to 16_777_215 do
    s := !s +. 1.;
    s := !s +. 1.;
    s := !s +. 1.;
    s := !s +. 1.;
    s := !s +. 1.;
    s := !s +. 1.;
    s := !s +. 1.;
    s := !s +. 1.
  done;
  let _ = !s +. 1. in
  Printf.printf "%f\n" (Unix.gettimeofday () -. start)

let foo x =
  let _ = !x in ()

let () =
  let start = Unix.gettimeofday () in
  let s = ref 0 in
  for _i = 0 to 16_777_215 do
    s := !s + 1;
    s := !s + 1;
    s := !s + 1;
    s := !s + 1;
    s := !s + 1;
    s := !s + 1;
    s := !s + 1;
    s := !s + 1
  done;
  foo s;
  Printf.printf "%f\n" (Unix.gettimeofday () -. start);
  let start = Unix.gettimeofday () in
  let s = ref 0 in
  for _i = 0 to 16_777_215 do
    s := !s + 1;
    s := !s + 1;
    s := !s + 1;
    s := !s + 1;
    s := !s + 1;
    s := !s + 1;
    s := !s + 1;
    s := !s + 1
  done;
  foo (ref (!s + 1));
  Printf.printf "%f\n" (Unix.gettimeofday () -. start)
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0009236)
gasche (developer)
2013-05-01 14:58

There is no bug here.

The int behavior is the expect result of the local reference unboxing strategy: the call to the external function `foo` makes the reference escape, so it is kept in its boxed form, while the second loop doesn't escape "s", so a local mutable variable is used instead. Let's not get roles confused here: it is not the first code that is too slow, but the second one that is faster than expected! Of course, this optimization is local in scope and is expected to stop applying at some point.

The float behavior is a bit less satisfying because as you notice the "escape analysis" is in this case a bit more fragile. This is known, and Alain Frisch and Pierre Chambart have been working on better unboxing strategies (see e.g. Alain Frisch "more_unboxing" SVN branch).

I appreciate the time and interest you devote in micro-optimizations of the compiler, but I will still close this bug report as there is no real-world code being improved here.
(0009237)
vbrankov (reporter)
2013-05-01 15:20

Thank you very much for the quick response.

- Issue History
Date Modified Username Field Change
2013-05-01 14:46 vbrankov New Issue
2013-05-01 14:58 gasche Note Added: 0009236
2013-05-01 14:58 gasche Status new => resolved
2013-05-01 14:58 gasche Resolution open => no change required
2013-05-01 14:58 gasche Assigned To => gasche
2013-05-01 15:20 vbrankov Note Added: 0009237


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker