caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Ropes and rope-like functional extensible vectors with O(1) prepend/append.
@ 2007-07-28 23:33 Mauricio Fernandez
  2007-07-30  0:46 ` [Caml-list] " Jon Harrop
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Mauricio Fernandez @ 2007-07-28 23:33 UTC (permalink / raw)
  To: caml-list

In the aftermath of the ICFP contest, during which I used Luca de Alfaro's
Vec, I felt like implementing ropes, based on Boehm's paper and the well-known
code included in his conservative garbage collector.

I later realized that the basic implementation strategies ("dense" leaves,
bounded tree height and amortized constant time concatenation of small
strings) could be generalized to build general extensible vectors similar to
Vec.

Such vectors (tentatively named "Vect" until I find a better name) have some
interesting properties:
* lower space overhead than other structures based on balanced trees such as
  Vec.  The overhead can be adjusted, allowing to make "get" faster at the
  expense of "set" and viceversa.
* appending or prepending a small vector to an arbitrarily large one in
  amortized constant time
* concat, subarray, insert, remove operations in amortized logarithmic time
* access and modification (get, set) in logarithmic time

The first one is probably the most compelling. Currently, Vec imposes a 6-word
overhead per element. Even after the obvious modification consisting in adding
a new constructor for leaves, the overhead would still be 350%... Vect uses
compact leaves with a configurable number of elements (32-64 seem good
choices, leading to worst-case overheads of 100% and 50% respectively), which
also helps with "get" due to the improved spatial locality.

You can find the code for both Rope and Vect at
http://eigenclass.org/repos/oropes/head/ 
It is still young and experimental, but it's maybe worth a look. Any feedback
is very welcome. 

The documentation can be found under
http://eigenclass.org/repos/oropes/head/doc/

I've spent some time benchmarking it against Vec; you can also find the
code I used and the resulting graphs at the above address.

To summarise how it compares to Vec:
* Vec can be used when persistence is required, but Vect would probably be a
  poor choice in this case (until that is fixed using lazy rebuilding, which
  doesn't seem too hard), unless rebalancing explicitly before "taking the
  snapshot" is an option
* Vect can append/prepend single elements or small vectors very quickly, in
  amortized constant time. See
  http://eigenclass.org/repos/oropes/head/append.png
* as expected, Vec.set is faster than Vect's in general
  http://eigenclass.org/repos/oropes/head/set.png 
  However, if the vector is balanced explicitly shortly before an update
  burst, Vect is somewhat surprisingly faster 
  http://eigenclass.org/repos/oropes/head/set-balanced.png
  This might be attributed to Vect's smaller memory profile and the fact that
  it allows better use of the L2 cache, but there seems to be another factor
  that I have yet to discover.
* Vect.get is considerably faster than Vec.get
  http://eigenclass.org/repos/oropes/head/get.png

The above URL is a darcs repository, so if anybody shoots me a patch I'll be
happy to apply it :)

Regards,

-- 
Mauricio Fernandez  -   http://eigenclass.org


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2007-08-22  2:55 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-28 23:33 Ropes and rope-like functional extensible vectors with O(1) prepend/append Mauricio Fernandez
2007-07-30  0:46 ` [Caml-list] " Jon Harrop
2007-07-30 11:51   ` Mauricio Fernandez
2007-07-30 13:47     ` Jon Harrop
2007-07-31 23:55       ` Mauricio Fernandez
2007-08-04 10:36         ` Jon Harrop
2007-08-09 22:07 ` Nathaniel Gray
2007-08-21 21:39 ` Luca de Alfaro
2007-08-22  2:54   ` skaller

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).