Login

Manual optimizations

Lifting inner functions to avoid closure allocation

Example with Stdlib.Hashtbl.mem: https://github.com/ocaml/ocaml/pull/11500

Lifting inner constants to avoid per-use allocation

f {x="a"; y="b"; z=true} foo bar

It's not clear to me when lifting is needed to prevent an allocation per evaluation of this call, except that it's certainly needed if any of these fields are mutable but you want to treat it as a constant anyway.

-dcmm can tell you whether it allocates. None of these calls allocate:

(function{point.ml:10,6-93} camlPoint.f_71 (param/674: val)
 (let
   (sequence/678
      (app{point.ml:11,2-15} "camlPoint.show_46" "camlPoint__const_block_43"
        3 val)
    sequence/681
      (app{point.ml:12,2-15} "camlPoint.show_46" "camlPoint__const_block_43"
        5 val)
    sequence/685
      (app{point.ml:13,2-23} "camlPoint.show_46" "camlPoint__const_block_92"
        7 val))
   (app{point.ml:14,2-23} "camlPoint.show_46" "camlPoint__const_block_92" 9
     val)))

and the latter two allocate of these calls:

(function{point.ml:10,6-93} camlPoint.f_74 (param/677: val)
 (let
   (sequence/681
      (app{point.ml:11,2-15} "camlPoint.show_49"
        (load_mut val "camlPoint__Pmakeblock_130") 3 val)
    sequence/685
      (app{point.ml:12,2-15} "camlPoint.show_49"
        (load_mut val "camlPoint__Pmakeblock_130") 5 val)
    sequence/691
      (app{point.ml:13,2-23} "camlPoint.show_49"
        (alloc{point.ml:13,7-21} 2048 3 3) 7 val))
   (app{point.ml:14,2-23} "camlPoint.show_49"
     (alloc{point.ml:14,7-21} 2048 3 3) 9 val)))

Both results from

let f () =
  show origin 1;
  show origin 2;
  show {x = 1; y = 1} 3;
  show {x = 1; y = 1} 4

the first with an immutable point and the later with a mutable one.