Boxing Questions

I know that boxing is a popular concept with a lot of information available on it, but I have a few questions that I cannot find answers to:

1) If boxing results in a value type (struct) being converted to an object (link type) or link type, then why use a value type that will be placed in a box and will have a performance penalty? I am aware of the benefits and suitability in certain cases of both structure and class. It is said (1) that values ​​(value types) tend to live on the stack in temporary storage, but for how long? If I do not need a type, how can I provide his care and care at this moment? Or is it when a one-time pattern comes into play? I assume that the reason for using the structure will be related to its advantages.

Interestingly, if I use a structure to store two strings and a DateTime field, the structure will contain two links (strings) and DateTime together. I obviously assume this is faster than scattered values. Is there something I need to know in this project? (2).

1) http://en.csharp-online.net/Classes, Structs and Objects-Boxing and Unboxing

2) http://dotnetperls.com/Content/Struct-Examples.aspx

I did a search here for the answers I need, but no luck. I usually search this site for topics such as GC, generics, exception handling, etc., since there is a lot of wisdom to learn and share.

Thanks for the (potential) education of all the posters! Please excuse any potential naivete. Studying the insides, I enjoy spending time understanding the IL, etc. (Something needs to be decided soon).

+1
c # boxing
Mar 06 '09 at 22:27
source share
4 answers

If you never pass a value type to a reference variable, then boxing will not happen. When you do not know, answer the following questions :

  • Act as primitive types.
  • Specify an instance size of 16 bytes.
  • Immutable.
  • The semantics of meanings are desirable.

I also usually consider what the lifetime of such a variable is. If this is a local variable used in a method, I would like to use a struct (otherwise a class).

+4
Mar 06 '09 at 22:36
source share

You should use value types because of their logical benefit, rather than improving performance. In this case, since the types of values ​​are managed on the stack, you do not need to participate in garbage collection. If you have a type that is constantly being created and discarded (for example, int, float, double, etc.), you can get a good boost by turning them into structures. The thing to keep in mind is that you should really consider this only if you can also make the structure immutable.

+1
Mar 06 '09 at 22:35
source share

A few more things to consider -

First, you want to make sure that the structures are immutable (in general). Because of this, a good rule is the absence of structures containing reference types. Lines can be an exception to this, since they are immutable in C #, but from the point of view of the universal rule of thumb for design, I would be wary of this.

Secondly, there is another use case for structures that have not been mentioned so far - a large number of small objects. If you have a large list or an array of small objects, structures provide significantly better cache consistency and are absolutely critical. This is why most 3D engines use structures for points / vectors - they tend to have large arrays of points for vertices, etc.

It is worth noting that performance is an important part of your application. For example, in one of my applications, changing one type from a class to a structure aligned with 40% of the long (> 5-minute runtime) process. If objects that are close to each other in memory, if you use them repeatedly in heavy mathematical calculations, you can make huge profits.

Now - in your case, having 2 lines and a DateTime will probably not see any improvement from this. The type of routines that will run on strings will probably not perform heavy calculations (hopefully), i.e. Convert half a million points in space or make a great matrix decision, etc.

Finally, you will notice that .net3.5sp1 made the structures more useful. Prior to 3.5sp1 (on x86), there were no built-in methods with struct calls. This has limited performance capabilities through structures. Updating your structure can make the old structure code much, much faster (in some cases).

+1
Mar 07 '09 at 2:10
source share

You don’t always need boxing, and with generics it is not enough for this.
The memory used by the value types (struct is the value type) will be declared as
as soon as the method ends / returns and you do not need
do anything for this.

Value types declared as instance members will be in memory until the object
GC is deleted.

Link types are stored on a managed heap.
Link types created within the method will be deleted using
garbage collector when no object holds a link to it.

GC works on its own, and for the most part you should leave it alone.
You cannot predict when an object will be deleted by the GC.

The Dispose pattern is used for reference types, but will not force the removal of the GC
an object. It is usually used to free unmanaged resources.

For stack values, consider the following:
Say you have a simple program with three methods, for example:
When this program is running, the Main method starts, etc. Please follow
numbers below:

Main { // (0) Stack is empty int firstInt = 0; // (1) Stack now contains: // firstInt DoSomething1(); // (7) Stack still contains: // firstInt } // Program ends DoSomething() { int anInteger = 0; // (2) Stack now contains: // anInteger // firstInt DoMore() // (5) Stack now contains: // anInteger // firstInt } // (6) anInteger goes out of scope DoMore { int anotherInteger = 1; // (3) Stack now contains: // anotherInteger // anInteger // firstInt } // (4) anotherInteger goes out of scope 

0
Mar 06 '09 at 23:23
source share



All Articles