Re: heap or stack?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Stoitcho Goutsev \(100\) [C# MVP] (100_at_100.com)
Date: 03/04/04


Date: Thu, 4 Mar 2004 11:14:40 -0500

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" <MattAshworth@hotmail-dot-com.no-spam.invalid> wrote in
message news:404708b6_2@Usenet.com...
> 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.
>
>
>  Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
>     ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ---------------------------------------------------------- 
>                 http://www.usenet.com


Relevant Pages

  • Re: PtInRegion
    ... I was thinking rectangles although you clearly said "polygons". ... The problem is that Windows CE does not ... regions used by SetWindowRegion from a bitmap containing a mask. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: polygon objects are clipped to the plot area if specified with graph coordinates
    ... You are correct that the clipping rules for rectangles and for ... Maybe it would be best if all objects (rectangles, polygons, etc.) had ... If "clip" is given, the object would clip to the plot area. ...
    (comp.graphics.apps.gnuplot)
  • Re: looking for code
    ... > The polygons contain no slanted lines. ... Find one of these rectangles and complete it by extending the partial ... Repeat. ... I'm wondering, though, if there are some hidden requirements here. ...
    (comp.unix.programmer)
  • Re: Intersecting Polygons
    ... >just rectangles. ... The polygons can have 3 or more points/sides, up to infinite. ...
    (microsoft.public.vb.general.discussion)
  • Re: Intersecting Polygons
    ... I should have asked about the intersection of 2 POLYGONS, ... > just rectangles. ... The polygons can have 3 or more points/sides, ...
    (microsoft.public.vb.general.discussion)