From: "Quôc Peyrot" <chojin@lrde.epita.fr>
To: Brian Hurt <bhurt@janestcapital.com>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Book about functional design patterns
Date: Wed, 27 Jun 2007 22:55:33 +0200 [thread overview]
Message-ID: <25B2FF5F-7C58-4F70-B1AE-0FD7577FB521@lrde.epita.fr> (raw)
In-Reply-To: <4682CA02.4040006@janestcapital.com>
On Jun 27, 2007, at 10:35 PM, Brian Hurt wrote:
> Quôc Peyrot wrote:
>
>>
>> On Jun 27, 2007, at 9:48 PM, Brian Hurt wrote:
>>
>>> In Ocaml, allocations are relatively cheap- a cost similiar to
>>> that of allocating on the stack. Which is why when you tell
>>> long-time Ocaml programmers that you want to avoid an allocation
>>> cost by allocating on the stack, they tend to go "um, why?"
>>>
>>> Mutable data structures have their cost as well. When you assign
>>> a pointer into an object old enough to be in the major heap,
>>> Ocaml kicks off a minor collection. For small N, this can often
>>> make O (log N) purely functional structures faster than their O
>>> (1) imperative counterparts.
>>>
>>> No to mention the correctness advantages, plus other advantages.
>>
>>
>> If I have a tree/map datastructure and I add an element to it, my
>> understanding it that, when building the new tree, all the node up
>> to the root are going to be replaced. Is my understanding correct?
>
> No. Only those elements that change need to be reallocated- the
> rest can be shared between the old and new tree. So, assuming the
> tree is a balanced tree, only the log N or so nodes between the
> root and the changed node will need to be reallocated, of the N
> nodes in the tree- so you're sharing N - log N nodes between the
> two trees.
That's exactly what I meant with "all the nodes up to the root" (you
need to change a leaf, for that you need to change its parent, but
for that you ... and so on up to the root).
>> Now let's say I want to build a tree with millions elements, and
>> I'm only interested in the final result, i.e. I don't need to be
>> able to rollback to a previous state or fancy stuff like that (I
>> therefore never keep a reference to the root of the old tree,
>> each time I add a new element).
>> Is there a way of writing the building code (with a fold-type of
>> construct or something like that) such that the compiler will
>> understand it, and will generate a code equivalent to the
>> imperative code (i.e. we don't allocate new nodes up to the root
>> each time we insert an element)?
>>
> With a million element tree, you're only reallocating 20-30 nodes
> (assuming it's balanced), and sharing the other 999,970-999,980
> nodes between the two trees. Allocating each new node is only
> going to be a small number of clock cycles, like 5-10 clocks. So
> the total allocation cost is only 100-200 clocks or so.
I don't understand that part. Each time you add a node, log2(n) node
needs to be re-allocated. Hence the total number of reallocations is:
Sum(ceil(log2(i)), i = 1..N) with N = 1000000, which is significant.
Where is my mistake in these maths?
--
Best Regards,
Quôc
next prev parent reply other threads:[~2007-06-27 20:55 UTC|newest]
Thread overview: 60+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-06-27 12:14 The Implicit Accumulator: a design pattern using optional arguments 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
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 [this message]
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=25B2FF5F-7C58-4F70-B1AE-0FD7577FB521@lrde.epita.fr \
--to=chojin@lrde.epita.fr \
--cc=bhurt@janestcapital.com \
--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