caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Stephan Houben <stephan@pcrm.win.tue.nl>
To: Chris Hecker <checker@d6.com>
Cc: caml-list@inria.fr
Subject: Re: practical functional programming
Date: Tue, 7 Nov 2000 08:54:01 +0100	[thread overview]
Message-ID: <00110709260602.01341@pcrm> (raw)
In-Reply-To: <4.3.2.7.2.20001106100700.00c447b0@shell16.ba.best.com>

On Mon, 06 Nov 2000, Chris Hecker wrote:
 
> I guess my next question then would be related to efficiency: isn't there a lot of copying going on if you've got to make a new instance of your symbol table just to add an element?  

Well, one of the advantages of having a purely functional data structure is
that you can freely share substructures. So there is very little copying.
Because the updated structure shares most of its memory with the original
structure, holding on to both isn't prohibitively expensive.

Again, this makes it simple to "roll back" to a previous version.

> Again, I'm not saying efficiency is the only metric for software, but it does concern me when I see a large datastructure copied.

No copying. Just pointer sharing.

>  Is there some sort of reference counted sharing going on, 

Reference counting is a (bad) substitute for real garbage collection, such as
O'Caml has. No need for refcounting, since we have GC.

> or are there actually two copies of the data in memory if you wrote your
> example in caml?  

No, this is not the case. Most of the data is shared.

> I guess my hope would be that in the case where you
> didn't reference the old datastructure ever again you would get performance
> close to the equivalent imperative datastructure.

It depends on the data structure, but some of Okasaki's data structures come
close to this ideal.

> In the case where you did
> reference both old and new you'd get performance similar to writing an
> "undo-able" imperative datastructure from scratch, 

Writing an undo-able imperative data-structure is no easy job.
Most people would probably go for a functional data structure then.

For example: the new ReiserFs in Linux (journalling filesystem) needs
for its journalling a similar commit/roll-back functionality. Therefore, it
is based on balanced trees, which are a somwehat functional data structure.
(I definitely do not want to suggest that ReiserFs is somehow a purely
functional file system, just that the basic idea is somewhat similar to some
functional data structures.)

> possibly with an
> optimization for not accessing the old datastructure until the new one had
> been undone (like in your example, and I would assume that's the common case
> for using persistence).

I am afraid that compilers aren't that smart yet.

I think the bottom line is this: imperative datastructures are often (somewhat)
faster, but purely functional datastructures are more general. This shows the
importance of Okasaki's work: sometimes it is possible to have your cake and
eat it, too.

So now it is up to you to decide whether you need a purely functional or an 
imperative data structure. There is no hard&fast rule here: it depends
completely on the application. But the nice thing of O'Caml is that it gives you
the choice. Which is also the bad thing: if we were writing Perl, we would
almost always use hashes and not have to think about these issues.

Good luck with your decision,

Stephan

--  ir. Stephan H.M.J. Houben tel.
+31-40-2474358 / +31-40-2743497 e-mail: stephanh@win.tue.nl



  reply	other threads:[~2000-11-08 16:33 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-03 22:44 Chris Hecker
2000-11-04 18:48 ` Chet Murthy
2000-11-05 14:25 ` John Max Skaller
2000-11-06 22:26   ` Michael Hicks
2000-11-06  6:55 ` Francisco Reyes
2000-11-06 13:16 ` Jean-Christophe Filliatre
2000-11-06 18:15   ` Chris Hecker
2000-11-07  7:54     ` Stephan Houben [this message]
2000-11-11 14:32       ` John Max Skaller
2000-11-12 13:17         ` Ken Wakita
2000-11-12 20:09           ` John Max Skaller
2000-11-13  0:19             ` Ken Wakita
2000-11-13  7:45         ` STARYNKEVITCH Basile
2000-11-07  9:06     ` Xavier Leroy
2000-11-07 10:13       ` Chris Hecker
2000-11-09 10:56         ` Stephan Houben
2000-11-09 21:56           ` Chris Hecker
2000-11-07 18:05     ` Thorsten Ohl
2000-11-07 19:19     ` Marcin 'Qrczak' Kowalczyk
2000-11-06 23:10 Hao-yang Wang
2000-11-06 23:24 Hao-yang Wang

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=00110709260602.01341@pcrm \
    --to=stephan@pcrm.win.tue.nl \
    --cc=caml-list@inria.fr \
    --cc=checker@d6.com \
    --cc=stephanh@win.tue.nl \
    /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).