Re: Object for variable array of fixed structures
Re: Object for variable array of fixed structures
- Subject: Re: Object for variable array of fixed structures
- From: "Michael Ash" <email@hidden>
- Date: Mon, 12 Jun 2006 23:32:39 -0400
On 6/12/06, Steve Sheets <email@hidden> wrote:
A number of people have pointed out that I could use NSMutableArray
to manage variable size array of NObjects. To use my example, I would
store NSRect inside an NSData wrapper, or create subclass of NSObject
with an NSRect field or store an NSRect inside an NSValue for each
instance of NSRect I want. I would then store X number of these
objects using one NSMutableArray.
Yes, this is true, and this is what I am trying to avoid.
Using the NSMutableArray approach, when I want an array with 1000
rectangles, I would be creating 1001 objects, one NSMutableArray &
one thousand NSValue objects (using that method). Instead if I use
NSMutableData to store allocate enough memory for an array of
rectangles, I would only be working with one object, though I will
give you that it is an large object.
I not sure what the over head for having a given NSValue object, but
I can not imagine that 1001 objects do not take up a bit more of
memory than 1 object.
Of course, memory is cheap now a days. I just can't stand storing
data that inefficiently.
My structures are strictly scalar values, without reference to
handles inside them (a typical example might contain 2 rectangles, a
pointer, and some flags, yes, these are for graphic animations).
Any other thoughts on this technique?
If NSValue is smart about storing structs, it should have a four-byte
overhead. This does not count malloc overheard, nor the storage space
needed for the pointer to the NSValue. But let's assume it's dumb, and
throwing in malloc overhead and storage to the pointer in the NSArray
takes it up to 32 bytes of extra overhead per struct. (16 bytes for
NSValue itself, 4 bytes for the pointer to it, and the rest is misc.
allocation overhead.) That makes for almost 32 kilobytes of "wasted"
space. By comparison, your application icon alone takes up perhaps
60k. A minimal Cocoa app that just throws up a window will allocate a
couple of megabytes just to start up.
By using actual Objective-C classes instead of structs, you can ensure
that your object overhead is kept to a minimum.
Using a plain C array, or a thin OO wrapper around one, can be a
useful optimization for large numbers of objects. But "large" requires
at least a couple more zeroes than you have here.
Mike
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden