caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christophe Raffalli <raffalli@univ-savoie.fr>
Cc: caml-list@inria.fr
Subject: [Caml-list] teil recursive function and GC
Date: Wed, 09 Jan 2002 14:28:07 +0100	[thread overview]
Message-ID: <3C3C4567.511AA372@univ-savoie.fr> (raw)
In-Reply-To: <ya38zb96kcu.dlv@serveur3.labri.fr>


Hello,

It seems that the two follwing functions are not equivalent: the tail
recursive version keeps pointers on all the intermediate sets and use a
huge amount of memory (with around 200000 triangles) while the
imperative version works well.

I think the compiler should generate code that do not keep useless
pointer accessible by the GC for tail recursive function calls (and also
for other function call) ?

(I use ocaml-3.03 with the native code compiler)

Imperative version (it is only a piece of my code):

  let vertices_number = 
    let adone = ref Vertex_set.empty in
    let count_vertices (p1,p2,p3) =
      adone := Vertex_set.add p1 
		       (Vertex_set.add p2
			  (Vertex_set.add p3 !adone))
    in
    List.iter count_vertices triangles;
    Vertex_set.cardinal !adone
  in
  let edges_number = 
    let adone = ref Edge_set.empty in
    let count_edges (p1,p2,p3) =
      adone := Edge_set.add (p1,p2)
		         (Edge_set.add (p2,p3)
			  (Edge_set.add (p1,p3) !adone))
    in
    List.iter count_edges triangles;
    Edge_set.cardinal !adone
  in

Tail recursive version:

  let rec count_vertices adone ts =
    match ts with 
      [] -> Vertex_set.cardinal adone
    | (p1,p2,p3)::ts -> count_vertices (Vertex_set.add p1 
		       (Vertex_set.add p2
			  (Vertex_set.add p3 adone))) ts
  in
  let vertices_number = count_vertices Vertex_set.empty triangles in
  let rec count_edges adone ts =
    match ts with 
      [] -> Edge_set.cardinal adone
    | (p1,p2,p3)::ts -> count_edges (Edge_set.add (p1,p2)
		         (Edge_set.add (p2,p3)
			  (Edge_set.add (p1,p3) adone ))) ts
  in
  let edges_number = count_edges Edge_set.empty triangles in


-- 
Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


      reply	other threads:[~2002-01-11  9:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <romildo@uber.com.br>
2002-01-08 13:08 ` [Caml-list] Function definition with multiple patterns in multiple equations José Romildo Malaquias
2002-01-08 13:16   ` Francois Thomasset
2002-01-08 13:18   ` Clement Renard
2002-01-08 13:26   ` John Prevost
2002-01-11  9:23     ` Xavier Leroy
2002-01-11  9:47     ` Andreas Rossberg
2002-01-08 13:30   ` Remi VANICAT
2002-01-09 13:28     ` Christophe Raffalli [this message]

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=3C3C4567.511AA372@univ-savoie.fr \
    --to=raffalli@univ-savoie.fr \
    --cc=caml-list@inria.fr \
    /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;
as well as URLs for NNTP newsgroup(s).