title: Run time difference between chained functions and a list of functions in OCaml
body: <p>For registering unit functions to be called later, I came up with this solution using a <code>(unit -&gt; unit) ref</code> that chains the functions together:</p>
  <pre class="lang-ml prettyprint-override"><code>let callbacks = ref @@ fun () -&gt; ()
  
  let add_callback f =
    let old_callbacks = !callbacks in
    callbacks := (fun () -&gt; f (); old_callbacks ())
  
  (* ... call add_callback many times ... *)
  
  (* eventually, run all the callbacks I stacked: *)
  !callbacks ()
  </code></pre>
  <p>I am wondering how it compares to that other solution using a stack of functions <code>(unit -&gt; unit) list ref</code>:</p>
  <pre class="lang-ml prettyprint-override"><code>let callbacks = ref []
  
  let add_callback f =
    callbacks := f :: !callbacks
  
  (* ... call add_callback many times ... *)
  
  (* eventually, run all the callbacks I stacked: *)
  List.iter (fun f -&gt; f ()) !callbacks
  </code></pre>
  <p>Are there differences in how they are represented in memory?</p>
  <p>If I were to register a huge amount of functions, which solution would be the best in terms of memory and of unstacking time? For these 2 criteria, is there a better yet solution?</p>