Array append performance

Sean Kelly sean at invisibleduck.org
Sat Aug 23 09:55:10 PDT 2008


== Quote from dsimcha (dsimcha at yahoo.com)'s article
> == Quote from superdan (super at dan.org)'s article
> > sorry to always be the one who puts the moose on the table. y'all who want to
> make capacity a field in the array are off base.
> > today each slice has length and data. why. because all a slice is *is* length
> and data. it's a view of a portion of memory. the slice don't care where memory
> came from. capacity is part of the memory that slices come from. it would be
> shared by several slices. and all of a sudden you realize capacity can't be a
> field in the slice.
> A capacity field would work fine with slicing if, whenever you take a slice into
> an array, the capacity field of the slice is set to zero implicitly, even though
> the length is longer than zero.  This is in effect how it works now.  Try calling
> std.gc.capacity() on a pointer to a slice.  It will always return 0 b/c a slice
> doesn't point to the start of a block.  This means that if you try to increase the
> size of a slice it will always be reallocated.  Simply setting the capacity field
> of slices to 0 when they are created would replicate this in arrays w/ a capacity
> field.

The Tango runtime is a bit different in that it checks to see if the array is a slice
explicitly--it seems safer than relying on the GC to always return a zero capacity
for an interior pointer.


Sean



More information about the Digitalmars-d mailing list