views:

295

answers:

4

Very simple question, what would be your way of storing 100 KB - 2 MB objects in memory? Object is made of 3 doubles and two strings (both mostly under 5 chars long). Would using struct instead of class be any better?

EDIT: I don't know why I said double, it is float .. :S

+2  A: 

Any chance of using the Flyweight pattern ?

You can use a struct if your type represents a value type. Structs are cheaper to allocate and deallocate. Considering the design guidelines, you can use a struct if:

  • the type has an instance size less then 16 bytes.
  • instances are short lived
  • instances are immutable (which all value types should be imho).
Frederik Gheysels
first 3 doubles are point coords, all different, so any chance is on these string, but it is very doubtful :/
Sapphire
A: 

I doubt there is a difference between a struct and a class, certainly not in C++ where they are the essentially the same thing. 2M objects of the size you suggest is not that large on a modern Desktop system.

Clifford
In .NET there is a big difference, since structs are value types and allocated on the stack, while classes are reference types and allocated on the heap.
Frederik Gheysels
The question is tagged .NET so I don't think the OP is asking about C++. But you are totally right, 2M objects of that size is nothing unusual.
0xA3
@Frederik: This is only partially correct. Value type != allocated on the stack!!! See http://stackoverflow.com/questions/1130468/memory-allocation-of-value-types-and-reference-types-in-net-framework
0xA3
divo: a value type is allocated on the stack; what Jon Skeet is saying in the post you're linking to, is a different situatin where a value type is a member of a reference type ... That's a different story.
Frederik Gheysels
My point was that your statement seems to suggest that structs are stored on the stack *because* they are value types. This is not correct. Also have a look at Eric Lippert's great explanation: http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx
0xA3
...and not to forget that types like array of a value type are allocated on the heap.
0xA3
I realise that it is tagged .NET, but no language specified. It could have been C++/CLI which is a .NET language, but point taken.
Clifford
+2  A: 

There are two main issues to consider when dealing with a large number of objects.

The first has been addressed in Frederik Gheysels' answer through the use of the Fly weight pattern, which is the memory issue.

The second issue is how to efficiently add and retrieve these objects/structs (depending which path you chose to follow). Obviously I am assuming you don't just create these objects and never want to retrieve them again ;)

To answer this question though, it really depends on how you want to add and access your data. From there you can decide which data structure is best suited for your task. For example maybe you want to process these objects in LIFO order, then a stack would be the most efficient way.

hhafez
+1  A: 

I don't know much about what you're doing but how about these rules of thumb?

If you're having to store the points in a Collection of some sort, then make them classes to save the overhead of boxing structs for storage.

If you're just using the points as input data to another object for processing, e.g. as points in a mesh for processing, then prefer an array to save the Collection class overheads.

If you're storing the points in an array then just make them structs as this is more efficient than making them classes.

Trevor Tippins