Re: Value Types and Reference Types
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Tue, 1 Apr 2008 22:37:32 +0200
"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message news:MPG.225c85d418d335dfc3a@xxxxxxxxxxxxxxxxxxxxxxx
Jon Slaughter <Jon_Slaughter@xxxxxxxxxxx> wrote:> No, the compiler doesn't have a list of them. The type metadata
> determines whether or not it's a value type.
So your saying that an int is not special? What I mean is that the compiler
has internal types that essentially are representable by the cpu's logic.
int is only special to the C# compiler in two ways:
1) It knows that it's an alias for System.Int23.
2) It knows that there are specific IL instructions for handling
System.Int32
The C# compiler (at least Microsoft's one targeting .NET) doesn't
usually know what CPU your code will eventually run on.
However, you picked an unfortunate example - one where the compiler
does genuinely have extra information. Now let's looks at
System.Drawing.Point. The compiler has no information about that struct
built into it - it has to find out that it's a value type the same way
it finds out about its methods etc, by looking at the metadata.
The cpu doesn't know how to add a two classes together even if one defines
an operator... but it knows how to add two words. I'm not sure of the
terminology though but there definitely are special types that can be dealt
with by the cpu itself. These are the most basic types that everything else
is built up from.
Sure - but that's irrelevant to your original claim that "The compiler
basically has a list of types that are special and will stick them on
the stack instead of the heap when used."
> Struct isn't a type in itself, it's just the way you declare a value
> type in C#.
well, maybe not but a meta type at least. Its a "container" of value
types(although this isn't necessary I suppose).
You may be meaning System.ValueType, but really the word "struct" does
not represent a "container" of value types in any way.
>> int x;
>>
>> gives a value type x.
>>
>> System.Int32 x;
>>
>> will give a referenec type x;
>
> No it won't. int is just an alias for System.Int32. The two lines of
> code above are exactly equivalent.
Ok, well, there are objects of int type. I thought Int32 was the object
representative of int. I thought there was some object type for int in
.NET? If not then there is at least an internal type that is used? Else
there would be no way to box?
Every value type automatically has a boxed type with the same name. C#
makes it hard to spot the difference really, but in C++/CLI I believe
you can distinguish between the two types. (Ben, confirm or deny?)
unless boxing doesn't create an object on the heap but just copiest the
value type? In that cause its still like an paired down object.
Boxing definitely creates an object on the heap.
> Well, I wouldn't say it's actually slow. It will cause a performance
> hit if you do it millions and millions of times, but it's no slower
> than creating other objects.
Yes, but the main point was that boxing is slow. Creating objects from
classes is necessary but boxing isn't(maybe in certain cases of course but
there is a difference)
Boxing is slower than not boxing, in that doing something is almost
always slower than not doing it. But that's not the same as saying it's
slow. Is adding two numbers slow? How about calling a method? Just how
slow do you think boxing is?
Here's a micro-benchmark - unreliable as all micro-benchmarks are, but
it genuinely does do boxing, a billion times:
using System;
using System.Diagnostics;
public class Test
{
const int Iterations = 1000*1000*1000;
static void Main()
{
// Just to make sure there's no initial hit
int dummy = 10;
object o2 = dummy;
Stopwatch sw = Stopwatch.StartNew();
int j = 5;
for (int i=0; i < Iterations; i++)
{
object o = j;
}
sw.Stop();
Console.WriteLine (sw.ElapsedMilliseconds);
}
}
On my laptop (running on "balanced" power mode, even) it executes in
about 8.6 seconds. In other words, it's boxing over 100 *million* times
per second. Is that really your idea of a slow operation?
You can make the above even faster when building with /o+ ;-)
Actually a boxing operation takes 11 machine instruction (assuming is no GC is introduced), 10 instruction to allocate the object space on the heap and 1 instruction to move the value into the object. So, boxing performance is merely a matter of memory latency.
Willy.
.
- References:
- Re: Value Types and Reference Types
- From: Jon Slaughter
- Re: Value Types and Reference Types
- From: Jon Skeet [C# MVP]
- Re: Value Types and Reference Types
- From: Jon Slaughter
- Re: Value Types and Reference Types
- From: Jon Skeet [C# MVP]
- Re: Value Types and Reference Types
- Prev by Date: Re: events and backgroundworker thread
- Next by Date: Re: debugging slows down other actions
- Previous by thread: Re: Value Types and Reference Types
- Next by thread: Re: Silverlight
- Index(es):
Relevant Pages
|