Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the performance hit with downcasting?

Just trying to get my head around Generics by reading this enlightening article by Juval Lowy

Paraphrasing.. When you define a Generic class definition, it is compiled into IL.

  • For value-types, as soon as you request for a specific value-type, it substitutes the T with your specific value type to obtain the IL for that specific configuration e.g. MyList<int> Benefit: No boxing and unboxing penalties.
  • All good.. for reference types, the compiler replaces all instances of T in your definition with Object and creates the IL which is used for all ref types. Instances however are allocated based on the actual requested ref type e.g. MyList<String>

Now pre-generics we could have written methods that take Object parameters. Generics claims 100% performance improvement because 'it avoids the performance penalty you incur when you downcast the object type to your specific type when you want to use it'

 // assume GetItem returns an Object
 string sMyPreciousString = (string) obList.GetItem(); 

What is this performance hit when you downcast from Object to specific reference type? Also it seems like up-casting to Object (even Generics would do this) isn't a performance hit.. why?

like image 975
Gishu Avatar asked Nov 20 '08 15:11

Gishu


People also ask

What is downcasting in C++?

Downcasting. In class-based programming, downcasting or type refinement is the act of casting a reference of a base class to one of its derived classes. In many programming languages, it is possible to check through type introspection to determine whether the type of the referenced object is indeed the one being cast to or a derived type...

What is the difference between upcasting and downcasting an object?

Casting does not change the actual object type. Only the reference type gets changed. Upcasting is always safe and never fails. Downcasting can risk throwing a ClassCastException, so the instanceof operator is used to check type before casting.

Why do downcasts have to be manifest in the code?

That's why the downcasting operation is required to be manifest in the text of the program. It's so that you can more easily notice the smell and spend code review attention on it. in what circumstance [s] is it appropriate to write code which downcasts?

What is the difference between static cast and compile time downcasting?

In C++, run-time type checking is implemented through dynamic_cast. Compile-time downcasting is implemented by static_cast, but this operation performs no type check. If it is used improperly, it could produce undefined behavior.


3 Answers

Upcasting to object doesn't require an execution time check - it will always work, and is just a no-op basically.

Downcasting requires an execution time check to make sure you're not casting a Stream to a String for example. It's a pretty small penalty, and very unlikely to be a bottleneck - but avoiding it is just one extra benefit for generics.

like image 172
Jon Skeet Avatar answered Oct 17 '22 02:10

Jon Skeet


The performance hit comes from the need for a run time type check. If B is a subclass of A, then when you cast a B into an A, you know at compile time that it will be safe, since all Bs are As. Therefore, you do not need to generate any runtime code to check the type.

However, when you cast an A into a B, you do not know at compile time whether the A is actually a B or not. It might just be an A, it might be of type C, a different subtype of A. Therefore, you need to generate runtime code that will make sure the object is actually a B and throw an exception if it isn't.

Generics don't have this problem, because the compiler knows, at compile time, that only Bs were put into the data structure, so when you pull something out, the compiler knows that it will be a B, so there's no need for the type check at run time.

like image 30
Andru Luvisi Avatar answered Oct 17 '22 03:10

Andru Luvisi


Reading up on the IL that gets generated (this article mentions it)... aha - isinst.

If you weren't downcasting, you wouldn't have to call isinst.

like image 28
Amy B Avatar answered Oct 17 '22 03:10

Amy B