Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Thomas Fischbacher <tf@functionality.de>
To: Jon Harrop <jon@ffconsultancy.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] The Implicit Accumulator: a design pattern using optional arguments
Date: Thu, 28 Jun 2007 17:01:05 +0100	[thread overview]
Message-ID: <4683DB41.50801@functionality.de> (raw)
In-Reply-To: <200706281543.01424.jon@ffconsultancy.com>

Jon Harrop wrote:

> I am more than happy to talk about continuations and consing but you need to 
> post code that uses continuations or conses before anyone can help.

Let us look at the following example:

===>

let print_gc_info verbose =
   let s = if verbose then Gc.stat() else Gc.quick_stat() in
     Printf.printf
"=== GC Stats (%s) ===
minor_words:       %f
promoted_words:    %f
major_words:       %f
minor_collections: %d
major_collections: %d
heap_words:        %d
heap_chunks:       %d
live_words:        %d
live_blocks:       %d
free_words:        %d
free_blocks:       %d
largest_free:      %d
fragments:         %d
compactions:       %d
top_heap_words:    %d
"
       (if verbose then "verbose" else "quick")
       s.Gc.minor_words
       s.Gc.promoted_words
       s.Gc.major_words
       s.Gc.minor_collections
       s.Gc.major_collections
       s.Gc.heap_words
       s.Gc.heap_chunks
       s.Gc.live_words
       s.Gc.live_blocks
       s.Gc.free_words
       s.Gc.free_blocks
       s.Gc.largest_free
       s.Gc.fragments
       s.Gc.compactions
       s.Gc.top_heap_words
;;

let twofuns_v1 x =
   (x mod 3, x mod 7)
;;

let twofuns_v2 c x =
   c (x mod 3) (x mod 7)
;;

let walk_pairs_v1 ?(target=[|0;0|]) n =
   begin
     target.(0) <- 0;
     target.(1) <- 0;
     for i=0 to n-1 do
       let (f1,f2) = twofuns_v1 i in
	begin
	  target.(0) <- target.(0) + f1;
	  target.(1) <- target.(1) + f2;
	end
     done;
     target
   end
;;

let walk_pairs_v2 ?(target=[|0;0|]) n =
   begin
     target.(0) <- 0;
     target.(1) <- 0;
     (let cont x y =
        begin
	 target.(0) <- target.(0) + x;
	 target.(1) <- target.(1) + y;
        end
      in
        for i=0 to n-1 do
	 twofuns_v2 cont i
        done);
     target
   end
;;

let () =
   let walker =
     if Sys.argv.(1) = "v1"
     then
       let () = Printf.printf "Using variant #1\n" in
	walk_pairs_v1
     else
       let () = Printf.printf "Using variant #2\n" in
	walk_pairs_v2
   in
   let target=[|0;0|] in
     begin
       ignore(walker ~target 100000000);
       Printf.printf "%d %d\n" target.(0) target.(1)
     end
;;

print_gc_info true;;
<===

I get (for the non-continuation approach):

=== GC Stats (verbose) ===
minor_words:       300018751.000000
promoted_words:    65.000000
major_words:       74.000000
minor_collections: 9155
major_collections: 0
heap_words:        61440
heap_chunks:       1
live_words:        74
live_blocks:       23
free_words:        61366
free_blocks:       1
largest_free:      61366
fragments:         0
compactions:       0
top_heap_words:    61440

And using the continuation:

=== GC Stats (verbose) ===
minor_words:       447.000000
promoted_words:    0.000000
major_words:       9.000000
minor_collections: 0
major_collections: 0
heap_words:        61440
heap_chunks:       1
live_words:        9
live_blocks:       3
free_words:        61431
free_blocks:       1
largest_free:      61431
fragments:         0
compactions:       0
top_heap_words:    61440

...so the continuation-based approach can execute not using the GC
at all - neither major nor minor. Sure, the OCaml GC behaves so
nicely that it does not make a difference in terms of run-time for
this particular small example (...or is it that calling a closure
is at present so inefficient that it outweighs the benefits of not
having to cons?) - but (1) is this the same if the minor heap is
more complicated, as in a real application and (2) shouldn't there
be huge potential for optimization in the second case then?

In particular, concerning point (2), when comparing run times
with the following bit of C code, I find that both OCaml
variants are slower than the C variant by more than a factor
of 3:

===>

#include <stdio.h>

typedef void (*cfii)(int,int);

static int buf[2]={0,0};

void twofuns_cont(cfii c,int x)
{
   c(x%3,x%7);
}

void incbuf(int x,int y)
{
   buf[0]+=x;
   buf[1]+=y;
}

int main(void)
{
   int i;

   for(i=0;i<100000000;i++)
     {
       twofuns_cont(&incbuf,i);
     }
   printf("%d %d\n",buf[0],buf[1]);
}
<===


>>According to your usually-screwed-up metrics...
> 
> 
> Time taken?

We are talking about your ray-tracer here.

For those who do not know yet, the fundamental problem with that study
of yours is that you kept on setting the *criteria* what to consider as
a permissible solution only after seeing the result, and doing so in
such a way that the outcome is the one you desired, i.e. to create the
impression OCaml were the best system around. This is not proper
scientific behaviour.

-- 
best regards,
Thomas Fischbacher
tf@functionality.de


  reply	other threads:[~2007-06-28 16:00 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-27 12:14 Jon Harrop
2007-06-27 13:18 ` [Caml-list] " Quôc Peyrot
2007-06-27 13:53   ` Jon Harrop
2007-06-27 14:18     ` Thomas Fischbacher
2007-06-27 15:09     ` Quôc Peyrot
2007-06-27 15:28     ` Mattias Engdegård
2007-06-27 15:38       ` Robert Fischer
2007-06-27 15:48         ` Mattias Engdegård
2007-06-27 16:01           ` Robert Fischer
2007-06-27 16:01           ` Mattias Engdegård
2007-06-27 18:06           ` Jon Harrop
2007-06-27 18:31             ` Brian Hurt
2007-06-27 19:56               ` skaller
2007-06-27 20:17               ` Jonathan Bryant
2007-06-27 22:57               ` Jon Harrop
2007-06-27 16:53     ` Hash-consing (was Re: [Caml-list] The Implicit Accumulator: a design pattern using optional arguments) Daniel Bünzli
2007-06-30  8:19     ` [Caml-list] The Implicit Accumulator: a design pattern using optional arguments Pierre Etchemaïté
2007-06-27 13:55   ` Thomas Fischbacher
2007-06-27 15:06     ` Quôc Peyrot
2007-06-27 15:53       ` Jon Harrop
2007-06-28 11:01         ` Thomas Fischbacher
2007-06-28 11:32           ` Jon Harrop
2007-06-28 11:42             ` Joel Reymont
2007-06-28 12:08               ` Jon Harrop
2007-06-28 13:10                 ` Quôc Peyrot
2007-06-28 13:35                   ` Thomas Fischbacher
2007-06-28 12:59             ` Thomas Fischbacher
2007-06-28 13:05               ` Jon Harrop
2007-06-28 13:33                 ` Thomas Fischbacher
2007-06-28 14:43                   ` Jon Harrop
2007-06-28 16:01                     ` Thomas Fischbacher [this message]
2007-06-28 17:53                       ` Jon Harrop
2007-06-27 16:39       ` Thomas Fischbacher
2007-06-27 19:26         ` Quôc Peyrot
2007-06-28 11:39           ` Thomas Fischbacher
2007-06-28 14:44             ` Jon Harrop
2007-06-28 16:03               ` Thomas Fischbacher
2007-06-28 17:20                 ` Dirk Thierbach
2007-06-28 22:12                   ` Thomas Fischbacher
2007-06-29  1:10                     ` Jon Harrop
2007-06-29 10:55                       ` Thomas Fischbacher
2007-06-29  6:12                     ` Dirk Thierbach
2007-06-27 17:16       ` Book about functional design patterns Gabriel Kerneis
2007-06-27 17:48         ` [Caml-list] " Jon Harrop
2007-06-27 19:33           ` Quôc Peyrot
2007-06-27 19:30         ` Quôc Peyrot
2007-06-27 19:48           ` Brian Hurt
2007-06-27 20:04             ` Quôc Peyrot
2007-06-27 20:35               ` Brian Hurt
2007-06-27 20:55                 ` Quôc Peyrot
2007-06-27 20:58                   ` Pal-Kristian Engstad
2007-06-27 21:18                     ` Quôc Peyrot
2007-06-27 21:18                       ` Pal-Kristian Engstad
2007-06-27 21:34                         ` Quôc Peyrot
2007-06-27 22:13                           ` Pal-Kristian Engstad
2007-06-27 15:18     ` [Caml-list] The Implicit Accumulator: a design pattern using optional arguments Jon Harrop
2007-06-27 16:44       ` Thomas Fischbacher
2007-06-27 18:17         ` Jon Harrop
2007-06-28 11:18           ` Thomas Fischbacher
2007-06-29 13:15     ` Bill Wood

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4683DB41.50801@functionality.de \
    --to=tf@functionality.de \
    --cc=caml-list@yquem.inria.fr \
    --cc=jon@ffconsultancy.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox