new Type[count] takes too much?

Marco Leise Marco.Leise at gmx.de
Thu Oct 31 13:08:44 PDT 2013


Am Thu, 31 Oct 2013 02:52:49 -0700
schrieb Jonathan M Davis <jmdavisProg at gmx.com>:

> On Thursday, October 31, 2013 10:15:51 Namespace wrote:
> > I'm sure we had already this conversation but I don't find the
> > thread.
> > 
> > T[] buffer = new T[N]; assumes more space than stated (in average
> > 2010 elements more. See: http://dpaste.dzfl.pl/af92ad22c). It
> > behaves exactly like reserve and that is IMO wrong. If I reserve
> > memory with buffer.reserve(N), I want to have at least N
> > elements. That behaviour is correct. But if I use new T[N] I
> > mostly want exactly N elements and no extra space.
> > 
> > Thoughts?

Maybe run a comparison with another popular GC enabled
language and check the memory use to find pathological cases.
I'd assume that D's GC doesn't waste more memory than any
other for dynamic arrays.

> You're making the assumption that it would be normal to not want to then 
> append to something you allocated with new T[N], and I don't think that that's 
> a valid assumption.

That doesn't happen much in my programming practice. I'm with
the OP on this.

> Also, from what I understand of how allocators work, they 
> normally grab memory in sizes which are a multiple of 2, so I would expect it 
> to be highly abnormal to ever get exactly the amount of memory that you 
> requested. You're pretty much always going to get extra.
> 
> - Jonathan M Davis

That's true. Although now I wonder if you could gain anything
from having memory pages split into chunks of T.sizeof from
which you can allocate single objects or whole arrays of T
with a compact layout...

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|T|T|T[6]       |T|T|           |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

-- 
Marco



More information about the Digitalmars-d-learn mailing list