Boxing Question

  • Thread starter Thread starter Nathan Neitzke
  • Start date Start date
N

Nathan Neitzke

I am writing an app that might largely benefit from using a struct instead
of a class. However, it needs to be rich enough where there are methods
available.

My question is - every time you call a method on a struct, is it boxed?
Because that would be a huge perf hit.

I am assuming that it does have to be boxed, but if anyone knows for sure
that would be great!
 
Nathan,
I am writing an app that might largely benefit from using a struct instead
of a class.
Why?


My question is - every time you call a method on a struct, is it boxed?

No



Mattias
 
Hi Nathan,
I am writing an app that might largely benefit from using a struct instead
of a class.

There is little or no performance reason to prefer a struct over a class,
little or no efficiency reason, and little or no readability reason.
Structs are useful for making API calls to unmanaged code.

Structs are useful in only the smallest number of cases, and personally,
I've never used them in C# outside of unmanaged API calls. In fact, I've
never even _seen_ them used in the hundreds of thousands of lines of code
that have been delivered in the systems that I've overseen, reviewed, or
participated in.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
There is little or no performance reason to prefer a struct over a
class,
little or no efficiency reason

Correct me if I am wrong, but struct/value type in managed code is
mainly to improve performance. If you have large amount of data,
creating it on the stack is considerably less expensive than going on
the heap.

I have personally never used structs in my code (which is not really
saying much) but I have always been looking for some reason which will
justify it based solely from performance point of view. I have always
thought of the sceanrio where a large number of objects are getting
allocated/deallocated to be the driving force behind using a value type
in managed code.
 
....
Structs are useful in only the smallest number of cases, and personally,
I've never used them in C# outside of unmanaged API calls. In fact, I've
never even _seen_ them used in the hundreds of thousands of lines of code
that have been delivered in the systems that I've overseen, reviewed, or
participated in.


I can think of cases where using classes instead of structs would make a
semantic difference. In the example below, if Currency was a reference type
the price of the product would be increased too, which is wrong:

lineItem.Price = product.Price;
lineItem.Price += new Currency(20, "CHF");

But as you Nick, I never saw it in real systems...


Alexander
 
Oops, it should be:

lineItem.Price = product.Price;
lineItem.Price += new Currency(20, "CHF");
 
Argh...

Something somewhere eats my plus signs!

Anyway it's the increment on the second line, "plus equals".
 
Nick Malik said:
There is little or no performance reason to prefer a struct over a class,
little or no efficiency reason, and little or no readability reason.
Structs are useful for making API calls to unmanaged code.

So you'd be happy if Int32 were a reference type, for example? I have
to disagree - the performance cost there would be dreadful. Imagine:

for (int i=0; i < 100000; i++)
{
....
}

You'd have created 100,000 objects on the heap, just for the iteration!
Structs are useful in only the smallest number of cases, and personally,
I've never used them in C# outside of unmanaged API calls. In fact, I've
never even _seen_ them used in the hundreds of thousands of lines of code
that have been delivered in the systems that I've overseen, reviewed, or
participated in.

I agree that they're very rarely useful outside interop, but they do
occasionally have their uses. I have two examples:

1) http://www.pobox.com/~skeet/csharp/threads/alternative.shtml

2) Things which are units of measure. I've recently been writing some
code to do with coverage, and it's handy to have a Coverage struct
which is basically two ints ("total sequence points" and "hit sequence
points"). It's logically a value type in the same way that Int32 is,
and it's nice not to have to create a new (separate) object on the heap
every time I create one of them.
 
Nathan,

I find that about boxing and unboxing always very interesting.

I made a simple test, can you try it.
In this test there is by the way 400,000,000 times a boxing or an unboxing
the results are milliseconds

{
int end1=0;
int end2=0;
for (int y = 0; y<1000;y++)
{
int start1 = Environment.TickCount;
for (object i=0; (int)i < 100000; i = (int) i + 1)
//This is very inefficient because the increment of the indexer
{
//Generated ILS code
//IL_000d: stloc.3
//IL_000e: ldc.i4.0
//IL_000f: box [mscorlib]System.Int32
//IL_0014: stloc.s i
//IL_0016: br.s IL_0029
//IL_0018: ldloc.s i
//IL_001a: unbox [mscorlib]System.Int32
//IL_001f: ldind.i4
//IL_0020: ldc.i4.1
//IL_0021: add
//IL_0022: box [mscorlib]System.Int32
//IL_0027: stloc.s i
//IL_0029: ldloc.s i
//IL_002b: unbox [mscorlib]System.Int32
//IL_0030: ldind.i4
//IL_0031: ldc.i4 0x186a0
//IL_0036: blt.s IL_0018
//IL_0038: ldloc.0
}
end1 += Environment.TickCount-start1;
int start2 = Environment.TickCount;
for (int i = 0; i < 100000; i++)
{
//Generated Ils code
//IL_003e: ldloc.3
//IL_003f: sub
//IL_0040: add
//IL_0041: stloc.0
}
end2 += Environment.TickCount-start2;
}
MessageBox.Show("Object: " + end1.ToString()
+ " Integer: " + end2.ToString());
}

Cor
 
Wow, great analysis. Thanks much for the code! Nice to see so IL in action
too. I like getting down to this level.

Actually, to reply to the posts above - they are right. In 99% of the
instances I would use a class instead of a struct.

However this is a very specialized case as I am writing a particular
algorithm for a scientific library. Just turns out that structs are more
efficient in this case.

Thanks again!
 

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

Back
Top