caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: Christophe Raffalli <Christophe.Raffalli@univ-savoie.fr>
Cc: Alain Frisch <alain@frisch.fr>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Preferred Way to Split a List
Date: Tue, 30 Oct 2007 22:31:20 +1100	[thread overview]
Message-ID: <1193743880.6697.18.camel@rosella.wigram> (raw)
In-Reply-To: <4725A915.3020005@univ-savoie.fr>


On Mon, 2007-10-29 at 10:34 +0100, Christophe Raffalli wrote:
> Or
> 
> let split l n =
>   let rec aux acc l n =
>      if n = 0 then List.rev acc, l
>      else match l with
>         [] -> List.rev acc, []
>      | hd::l -> aux (hd::acc) l (n-1)
>   in aux [] l n
> 
> This is tail recursive, it does not use magic nor  references
> and keed the original ordering of the list.

Yes, but it double handles the head needlessly. Ignoring
the interesting observation you make below, this would
be expected to transfer head memory twice into the cache,
when once would suffice.

> Finally, I did in the past some testing  comparing a tail-rec append or map
> using magic and the composition of rev and rev-append or rev-map ...
> There was no noticable difference, because the extra cost  of
> mutability (regarding the managment of the grey val list) compensate
> the extra call to reverse.

However, you raise here an important issue not covered well anywhere:
when you have a garbage collection, the effect of some operation
should include not just the local algorithmic considerations
but also the cost of the garbage collection.

In the equivalent Felix code, there is no issue with write
barriers because Felix collector doesn't use them, so mutations
don't incur any cost. OTOH allocations are expensive.

Now, back to collectors .. the append is a special.
The mutations in the append are not mutations, but deferred
initialisations, that is, the write operation is guaranteed
to be replacing a NULL pointer.

Overwriting of NULL pointers is already possible in functional
coding without it implying mutation.

So one might imagine a collector with a minor and major heap,
in which the major heap requires special handling for mutations.
However the rule for moving an object out of the minor heap
is that the object must be fully initialised. (all pointers
in the block are non-null). 

In that design (I have no idea if it is viable!) the performance
might be different for the 'append' and other mutations.

The bottom line is that a good point is made that the 
usual fine detail performance analysis doesn't apply to
a garbage collected language.. thanks for pointing this out!


-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


  reply	other threads:[~2007-10-30 11:31 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-29 23:33 Robert Fischer
2007-10-30  0:45 ` [Caml-list] " Matthew William Cox
2007-10-30 13:18   ` Robert Fischer
2007-11-03  3:06     ` Nathaniel Gray
2007-10-30  1:20 ` Julien Moutinho
2007-10-30  1:38   ` Karl Zilles
2007-10-30  5:46   ` skaller
2007-10-30  7:58     ` Alain Frisch
2007-10-29  9:34       ` Christophe Raffalli
2007-10-30 11:31         ` skaller [this message]
2007-10-30 12:30         ` David Allsopp
2007-10-30 15:03           ` Christophe Raffalli
2007-10-30 11:15       ` skaller
2007-10-30 13:05         ` Brian Hurt
2007-10-30  7:50 ` Jon Harrop
2007-10-30 13:20   ` Robert Fischer

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=1193743880.6697.18.camel@rosella.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=Christophe.Raffalli@univ-savoie.fr \
    --cc=alain@frisch.fr \
    --cc=caml-list@yquem.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).