in reply to link list trouble

Linked lists in Perl are far better implemented with a hash of all the elements -- it makes for much less memory, and allows you to cheat by accessing a point directly. I'll have some code up here in an hour or so to show it.

The only problem is elements with the same value, but this (ah, yes) can be gotten around.

japhy -- Perl Hacker and Regex Prince

Replies are listed 'Best First'.
Re: Re: link list trouble
by runrig (Abbot) on Nov 17, 2000 at 21:56 UTC
    The interpreter mentioned in Lisp-In-Perl implements them as two arrays. One for all the 'car|down' nodes, and one for all the 'cdr|right' nodes. But then the nodes don't free themselves when they are no longer being referenced. A garbage collector has to periodically compact the arrays.

    Update: I did a rough benchmark of memory used by the two methods, and using array refs used ~3-4 times more memory. Is this because perl over-allocates whenever it allocates an array? Since you can pre-extend an array you know will be large, would it help if it were possible to pre-de-extend an array you know will be small? Here's the code I used (for comments on the fairness of the comparisons):
    my @arr; my @arr1; my @arr2; my $i=0; for (1..1_000_000) { # This #push @arr, [undef, undef]; # Or this push @arr, [++$i, ++$i]; # Vs. this: #push @arr1, undef; #push @arr2, undef; # Or this: #push @arr1, ++$i; #push @arr2, ++$i; } print "Done\n"; # Then just do a 'ps -lu runrig' at another command prompt when I get +here my $str=<STDIN>;