in reply to Re: Sparse arrays?
in thread Sparse arrays?
Update: If you didn't catch it, I was being sarcastic on the "wasted" space. It's itsy-bitsy by perl standards.
Exactly! I was missing the forest for the trees. Take a close look at the memory addresses being used here. I swapped the undef elements for numeric 1 just to see a real SvIV. I think what actually happens here is that the ARRAY pointer array is fully allocated out to the limit of @ary. The unused elements appear to be null pointers or at least all share a single empty Sv struct with no allocated svpv structures. So actually... that array with 1001 elements appears to have consumed enough memory for five elements plus an additional 994 32 bit pointers for a whopping 3K of "wasted" space. If I'm wrong on what this actually looks like then that'd be really good to know.
My interpretation is that arrays are sparse by default when there are uninitialized elements. Getting a sparse array to work nicely under iteration operators appears to require some additional footwork. This looks like the sort of thing you'd want to restrict to knowledgable perl users since it doesn't quite work with the normal perl idioms and is highly likely to confuse (but is a useful thing to have). If anyone knows of a different way to go directly from real element to real element that'd really keen.
@ary[1,100,500,1000] = (1) x 4; # Iterate a sparse array exists($ary[$_]) and print "$_\n" for 0 .. $#ary; # Iterate a normal array print "$_\n" for @ary
Elt No. 994 0x4284 SV = NULL(0x0) at 0x4284 REFCNT = 2147483634 FLAGS = (READONLY) Elt No. 995 0x4284 SV = NULL(0x0) at 0x4284 REFCNT = 2147483634 FLAGS = (READONLY) Elt No. 996 0x4284 SV = NULL(0x0) at 0x4284 REFCNT = 2147483634 FLAGS = (READONLY) Elt No. 997 0x4284 SV = NULL(0x0) at 0x4284 REFCNT = 2147483634 FLAGS = (READONLY) Elt No. 998 0x4284 SV = NULL(0x0) at 0x4284 REFCNT = 2147483634 FLAGS = (READONLY) Elt No. 999 0x706c SV = IV(0x12094) at 0x706c REFCNT = 1 FLAGS = (IOK,pIOK) IV = 1
Fun Fun Fun in the Fluffy Chair
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re^2: Sparse arrays?
by paulbort (Hermit) on Dec 18, 2002 at 16:47 UTC | |
by diotalevi (Canon) on Dec 18, 2002 at 16:52 UTC |