heap or stack?

M

MattAhsworth

I'm develpoing a mapping app and want to store the point data that
makes up lines, points and polygons efficiently.

Am I better off using unsafe C# and creating structs to hold the data
on the stack (100K plus points total) or create classes for all the
data and put up with the overhead of boxing/unboxing?

Is there some middle ground - perhaps an array of structs containing
point data to represent a line wrapped in a class? - if I do that
will the data still be on the stack or will putting it in an array or
class make it an object and force it onto the heap.

Performance is a big factor.
 
J

Jon Skeet [C# MVP]

MattAhsworth said:
I'm develpoing a mapping app and want to store the point data that
makes up lines, points and polygons efficiently.

Am I better off using unsafe C# and creating structs to hold the data
on the stack (100K plus points total) or create classes for all the
data and put up with the overhead of boxing/unboxing?

Is there some middle ground - perhaps an array of structs containing
point data to represent a line wrapped in a class? - if I do that
will the data still be on the stack or will putting it in an array or
class make it an object and force it onto the heap.

Performance is a big factor.

You don't need unsafe code to create structs, and you don't get boxing
if you just use classes. Could you try explaining *exactly* what you
believe your two options are?

Note that if you have an array of structs, that array will be on the
heap but there'll be no boxing going on.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Matt,
Speaking of Points, Lines, Rectangles and Polygons. Here is what I found
based on my experience.
Points and Rectangles should be *structures*
Lines and polygons should be *classes*

Why?
Because points and rectangles are like primitive types for any graphics. A
lot of temporary objects of them are created passed as a function arguments
mostly by value, copy between each other and. arithmetic-like operation +/-
may make sense for them. That's all calls for *structure*. Otherwise you
will pollute the heap with small chunks of garbage.
At the other hand the very often reside in collections and as a fields of
classes, which may makes you think that it might be better to make them
classes.
Keep in mind that when value types are fileds of classes there is no boxing.
Structure's data is part of the objects data in the managed heap. About
collections... Don't forged that when value type objects are kept in array
they are in unboxed state so you don't have to worry about boxing/unboxing.
Thus, you can make your own strongly-typed collection classes and use
internaly arrays as a storage. This will solve your problem with
boxing/unboxing.

Lines and polygons at the other hand may be very big and heavy objects
usually you don create them as a local variables and don't pass them
by-value. Classes, I believe, are more appropriate for them.

--
HTH
B\rgds
100 [C# MVP]

MattAhsworth said:
I'm develpoing a mapping app and want to store the point data that
makes up lines, points and polygons efficiently.

Am I better off using unsafe C# and creating structs to hold the data
on the stack (100K plus points total) or create classes for all the
data and put up with the overhead of boxing/unboxing?

Is there some middle ground - perhaps an array of structs containing
point data to represent a line wrapped in a class? - if I do that
will the data still be on the stack or will putting it in an array or
class make it an object and force it onto the heap.

Performance is a big factor.
 
M

MattAhsworth

Thanks for the replies. (not used forums before in 6 years of VB
programming and am very impressed at the speed and quality of
response).

Going to use array of structs as a field of each class of the
geographic objects. This way the data is held as value type on the
heap and I can let the garbage collector worry about memory without
performance coming into it too much.

Next question is then which array type? I think an arraylist, as long
as I set the size at creation, would be fastest for forwards/read
only access, is this true :?:
 
J

Jon Skeet [C# MVP]

MattAhsworth said:
Thanks for the replies. (not used forums before in 6 years of VB
programming and am very impressed at the speed and quality of
response).

Going to use array of structs as a field of each class of the
geographic objects. This way the data is held as value type on the
heap and I can let the garbage collector worry about memory without
performance coming into it too much.

Next question is then which array type? I think an arraylist, as long
as I set the size at creation, would be fastest for forwards/read
only access, is this true :?:

ArrayList isn't an array. If you use ArrayList, you'll end up with
boxing. If you know the size at creation time, use a plain array
instead, as that will avoid boxing and be very fast.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top