Re: Cons cell archaic!?
- From: Scott <xscottg@xxxxxxxxx>
- Date: Fri, 24 Oct 2008 10:04:48 -0700 (PDT)
On Oct 23, 1:35 pm, seeWebInst...@xxxxxxxxxxxxxxxx (Robert Maas,
http://tinyurl.com/uh3t) wrote:
From: smallpond <smallp...@xxxxxxxx>
You could imagine a language similar to lisp in which everything is
either an atom or a null terminated list, and not having dotted pairs.
Under that plan, it'd be impossible to build lists incrementally,
which is essential for efficiently implementing READ and other
parser for other variable-width list representations. The best that
could be done is to either pre-allocated the largest reasoanble
array then copy it to a smaller array after the parse is done, and
switch to a larger array whenever an unexpectedly large list is
seen, or allocate blocks of elements that are chained together, and
then re-copy the chain to a single array when the READ operation is
done, or to emulate CDR-linked cells by using CADR-linked cells
instead and re-copy to a single array when done, or somehow emulate
a binary search tree to emulate an extensible array and re-copy to
a single array when done. [...]
There are better strategies than you list (pun not originally
intended :-). You can realloc a vector as you go, allocating some
extra slots each time in anticipation of future appends. If you bump
up the size exponentially (making it a constant multiple larger at
each realloc), the amortized cost for appending to a vector is only
O(1). Moreover, if you choose a factor less than 2 (say 1.5 or
something) for your reallocations, you'll use less memory than a
linked list when you're finished. If you're memory allocator is
smart, you might occasionally get a larger vector without any
copying. Finally, traversing a vector will cause less cache grief
than traversing a linked list.
.
- References:
- Cons cell archaic!?
- From: Anticomuna
- Re: Cons cell archaic!?
- From: smallpond
- Re: Cons cell archaic!?
- From: Robert Maas, http://tinyurl.com/uh3t
- Cons cell archaic!?
- Prev by Date: Re: Common Lisp, the abstract syntax tree, introspection.
- Next by Date: Re: Common Lisp, the abstract syntax tree, introspection.
- Previous by thread: Re: Cons cell archaic!?
- Next by thread: Re: Cons cell archaic!?
- Index(es):
Relevant Pages
|