Water Cooler v2
Water Cooler v2

Reputation: 33880

Why are type conversions expensive?

Why are type conversions expensive? What does a type conversion entail?

Okay, I get it for value types but let's exclude them for a moment. Let's just talk about casts between reference types. If I write this:

class Animal { }

class Dog : Animal { }

Example 1

var dog = new Dog();

object obj = dog;

Example 2

public Animal GetAnimal()
{
    return new Dog();
}

object obj = GetAnimal();

How many type conversions do the above examples contain and why are they expensive?

I understand that it's only a new 4-byte pointer that has to be allocated on the thread's local argument stack and the object reference of the newly created reference pointer points to that same old address. Why is this an expensive thing to do? Is allocating a new object reference CPU intensive? Then even copying same object references must entail that cost, like in the below example:

Animal fish = new Animal();

Animal anotherFish = fish;

That must be equally expensive then?

Upvotes: 2

Views: 587

Answers (1)

usr
usr

Reputation: 171206

Casts to a base type are always free at runtime. When you write:

Dog dog = new Dog();
object obj = dog;

The JIT sees:

void* dog = new Dog();
void* obj = dog;

The JIT does not particularly care about the different reference types in this case. To it, all ref types are just pointers. The JIT is perfectly capable of dealing with trivially redundant variables and assignments.

A cast to a derived class often has a runtime cost because a runtime check must be made. This can sometimes be optimized out. If a check is actually necessary, it looks a little like this:

object obj = new Dog();
AssertCastValid<Dog>(obj);
Dog dog = obj; //Invalid C#, but OK for the JIT internally

With AssertCastValid being code to verify the runtime type. Here's what it could look like (although it probably doesn't):

bool AssertCastToDogValid(object obj) { return obj.GetType() == typeof(Dog); }

(This is incorrect, but illustrates the idea.)

As you can see, upcasts are always free and downcasts are often not free. How expensive? That's relative to what else you have going on in your code and it depends on the exact case (depth of hierarchy, cast to interface, ...).

Upvotes: 6

Related Questions