[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] That was productive...



Ronald J Kimball <rjk@linguist.dartmouth.edu> writes:

> > Um, I'm talking about the internally allocated space for
> > the array, not the publicly reported size.
> > You know, the mechanism that allows arrays to be dynamically
> > sized in perl.
> > 
> 
> That would be a rather serious design error, wouldn't you say?  There's no
> need to allocate space for an array if the space isn't being used for
> anything.

Not at all.  Speed is the factor.  If you resize the array of SV
pointers behind an array every time you extend the array, then you
have a large amount of copying.  But if you increase it by more than
one entry, then you have fewer copies of the data.  Many dynamic array
algorithms/libraries do this.  Usually the next size is twice as large
as the previous size.  This results in temporarily unused memory for
the sake of speed.

So, no, it is not a serious design error to allocate more space for a
dynamic array than is absolutely necessary.  In fact, it's a serious
design asset :)  There's a good bit of literature on what exactly is
the best heuristic to decide the next array size.  I'm not sure
exactly what perl uses, but often the exponential method (double each
resize) is used.  Another interesting one is a fibonacci sequence to
determine the next size, but largely it depends on your application.

Of course this isn't much Fun with Perl.

Chip

-- 
Chip Turner                   chip@ZFx.com
                              Programmer, ZFx, Inc.  www.zfx.com
                              PGP key available at wwwkeys.us.pgp.net

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe