[phobos] Array preallocate function

Steve Schveighoffer schveiguy at yahoo.com
Mon Mar 1 05:01:32 PST 2010


In changeset 254 (http://www.dsource.org/projects/druntime/changeset/254), I added three functions.  A capacity property, a "setCapacity" function that should really be a property except for the bug mentioned below, and a shrinkToFit function as discussed below.

Note that these functions are all template wrappers in object.di to runtime functions in rt/lifetime.d, they would be easy to switch to compiler builtins *hint hint*.  However, before that should happen, we should think of a good name for shrinkToFit.  The function shrinks the allocated space of a block so it ends at the end of the given array.  Essentially, it's there so you can re-use a buffer.  Usage looks like this:

buf.length = 0;
buf.shrinkToFit();

Please kick tires as desired.

-Steve



----- Original Message ----
> From: Steve Schveighoffer <schveiguy at yahoo.com>
>
> Ran into an issue with array properties:
> 
> http://d.puremagic.com/issues/show_bug.cgi?id=3857
> 
> I'm going to make the function not a property for now (at least for the "set 
> capacity" part).  Should I wait for this bug to be fixed, or should we discuss a 
> different scheme?
> 
> My plans were for this to work:
> 
> int[] x;
> 
> x.capacity = 10000; // set the capacity to 10000 elements
> assert(x.capacity >= 10000); // get the current capacity (elements allocated + 
> available)
> int *ptr = x.ptr;
> while(x.length < 10000)
>    x ~= 1;
> assert(x.ptr == ptr); // no reallocation
> x.length = 0;
> x.shrinkToFit(); // name up for debate
> x ~= 1;
> assert(x.ptr == ptr); // resized the allocation length, so it can be reused as a 
> buffer.
> 
> I'll have to change the write property 'capacity' to a function 'setCapacity' 
> for now.
> 
> -Steve
> 
> 
> 
> 
> ----- Original Message ----
> > From: Steve Schveighoffer 
> > To: Discuss the phobos library for D 
> > Sent: Tue, February 23, 2010 1:47:56 PM
> > Subject: Re: [phobos] Array preallocate function
> > 
> > I understand your objections to minimize.  I don't really have a preference, 
> but 
> > I don't really like shrinkToFit.  I'll use that for now, since it's probably 
> > easy to search/replace later.
> > 
> > -Steve
> > 
> > 
> > 
> > ----- Original Message ----
> > > From: Andrei Alexandrescu 
> > > > What about a "minimize" function, which simply truncates any
> > > > "allocated" length after an array.  So you would reset an array via:
> > > > 
> > > > arr.length = 0; arr.minimize();
> > > > 
> > > > The advantage here is the array's length is not affected, just the
> > > > allocated length is reduced to match the array's length.  There are
> > > > less invalid cases to worry about (i.e. "shrinking" to something
> > > > larger doesn't make any sense).
> > > 
> > > Sounds good. I'd choose a more specific name, e.g. shrinkToFit. Minimize has 
> 
> > me 
> > > think of optimization functions.
> > > 
> > > Andrei



      


More information about the phobos mailing list