• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ryan_001

Fast dyanamic_cast

11 posts in this topic

I'm working on a project that requires dynamic_cast. I understand its quite slow and should generally be avoided, that said I see no other way. To that end I thought I could perhaps speed up dynamic_cast by caching the results. So I put together a small class that seems to work for the limited test scenarios I threw at it. None-the-less its on the borderline of kosher and was wondering what other people think.

 

class FastDynamicCast {

	private:
		typedef const std::type_info* Type;
		typedef std::tuple<Type,Type,Type> InterfaceId;		// actual object type, src interface, dest interface
		typedef std::map<InterfaceId,int> Table;
		Table table;

	public:
		template <typename TR, typename T> TR Cast (T* ptr) {
			static_assert(std::is_pointer<TR>::value,"TR must be a pointer type.");
			static const int flag = std::numeric_limits<int>::max();

			InterfaceId id(&typeid(*ptr),&typeid(T*),&typeid(TR));
			auto i = table.find(id);
			if (i == table.end()) {
				TR r = dynamic_cast<TR>(ptr);
				if (r) {
					unsigned char* t0 = reinterpret_cast<unsigned char*>(ptr);
					unsigned char* t1 = reinterpret_cast<unsigned char*>(r);
					int diff = static_cast<int>(t1 - t0);
					table.insert(Table::value_type(id,diff));
					}
				else {
					table.insert(Table::value_type(id,flag));
					}
				return r;
				}
			else if (i->second == flag) return nullptr;
			else {
				unsigned char* t = reinterpret_cast<unsigned char*>(ptr);
				t += i->second;
				return reinterpret_cast<TR>(t);
				}
			}
	};

 

Despite the fact that the underlying implementation isn't specified in the standard, for a given object, src, and dest interface the pointer transformation needs to be the same... right? I mean you can't have objects magically changing around their structure at runtime.  Anyways I just thought I'd throw it out here to see what people thought and/or critique it.

0

Share this post


Link to post
Share on other sites

Have you profiled this? I highly doubt that this would outperform the compiler's implementation. In particular, what makes you think that typeid() will be significantly faster than dynamic_cast?

Not just typeid(), but the cost in terms of cache performance of using a map to hold the lookups is going to bite you pretty quick.

dynamic_cast<> on "slow" compilers is really just a strcmp. If you're really hurting for performance, rolling your own RTTI is the only way to really win.


But 99% of the time, you're not hurting that bad :-)
1

Share this post


Link to post
Share on other sites

I always wondered about something related. Assuming at some Point you Need to know what class an object actually is of. Then, as far as I understand, I could try dynamic_cast<> to all possible classes. And that's O(n), if n is the number of possible classes. So having a map or hashtable to just look it up should theoretically be faster for large enough n. Is this correct or am I missing a possible way to use dynamic_cast<> efficiently for this?

0

Share this post


Link to post
Share on other sites

I always wondered about something related. Assuming at some Point you Need to know what class an object actually is of. Then, as far as I understand, I could try dynamic_cast<> to all possible classes. And that's O(n), if n is the number of possible classes. So having a map or hashtable to just look it up should theoretically be faster for large enough n. Is this correct or am I missing a possible way to use dynamic_cast<> efficiently for this?

Or have the object tell you its type directly.

0

Share this post


Link to post
Share on other sites

I always wondered about something related. Assuming at some Point you Need to know what class an object actually is of. Then, as far as I understand, I could try dynamic_cast<> to all possible classes. And that's O(n), if n is the number of possible classes. So having a map or hashtable to just look it up should theoretically be faster for large enough n. Is this correct or am I missing a possible way to use dynamic_cast<> efficiently for this?

If you are really hurting by this roll your own and type checking then boils down to a pointer address compare, which you can find in Game Programming Gems 2. This relies on staticly declared pointers in your RTTI capable classes, which you have to register your self. This is all the DECLARE_CLASS(CEnemy, CMoveableObject) macros that you see in the Half-Life codebase (https://developer.valvesoftware.com/wiki/Authoring_a_Logical_Entity).
0

Share this post


Link to post
Share on other sites

dynamic_cast<> on "slow" compilers is really just a strcmp.

Well it's more like a bunch of strcmps in a loop. Worst case that I know of is a linear search of the target type in a list of all the legal class names of the dynamic type.
1

Share this post


Link to post
Share on other sites

I'm working on a project that requires dynamic_cast. I understand its quite slow and should generally be avoided, that said I see no other way.

There is almost certainly another way. In my experience, 'unavoidable' use of dynamic_cast is due to design flaws elsewhere in the system.

 

In your usage, do you only need exact type matches, or do you need to take inheritance hierarchies into account?

e.g.

Given the inheritance hierarchy:

C is a B
B is a A

And the code:

C derived;
A* basePtr = &derived;
B* cast = dynamic_cast<B*>( basePtr );
Using regular dynamic_cast (or a custom implementation that also takes inheritance hierarchies into account), then this cast will succeed, because the object is an A, a B and a C.

 

With a custom implementation based on exact matches only, then this cast would fail, because the object only identifies as a C -- the advantage of these kinds of RTTI systems is that they are a lot faster.

 

Well it's more like a bunch of strcmps in a loop. Worst case that I know of is a linear search of the target type in a list of all the legal class names of the dynamic type.

And this is why I ban dynamic_cast in my coding guidelines...

Imagine the language didn't have RTTI at all -- instead there was a free, cross-platform, but closed-source library that gave you easy RTTI. Despite it being closed source, people had stepped through the Asm to see how it worked, and found that on Windows it often resulted in a long loop of string comparisons.
How many users would that library have, compared to a simpler library, or compared to people just using their own extremely simple home-made RTTI systems based on enums, etc?
The only reason this horrible library does have any users at all, is because it's shipped with and integrated into the language. I believe in Keep-It-Simple-Stupid as a strong guideline, but in this case, keeping it simple for me would be to reinvent the wheel, to replace an over-complicated and bloated system with a simple one.

Edited by Hodgman
0

Share this post


Link to post
Share on other sites

I think it is worth pointing out an additional item in regards to some of the suggestions that using dynamic_cast is not so bad because it won't be a notable performance problem later.  This is unfortunately a very bad way to look at things, especially with this particular bit of functionality.  You need to approach this from the usage side of things to see why accepting dynamic_cast performance is a bad idea.  How do you usually use dynamic_cast?  Well, usually you have a list of some object and you are processing them in order using dynamic_cast to perform various functionality on each object.  So, in your code you might only be using dynamic_cast once in a while, the calls to it become huge and the performance issues add up.  This is so common a pattern in games that I simply don't bother with standard RTTI anymore, well in any core processing systems that is.

 

This is a case of massive gains in the long run if you correct the problem early on.  Mikes rule: "Early optimization is the root of all evil.", well ignoring the obvious is beyond evil in this case.  Especially if you consider that in games the most likely usage is in entities which will be called thousands of times a game loop.

 

.02

0

Share this post


Link to post
Share on other sites

But why do you "need" dynamic_cast<> or its homemade equivalent, as opposed to virtual functions to avoid leaking unneeded type information, segregation of objects of different classes to avoid identifying each one, members and methods providing the information you actually need, and other leaner and cleaner approaches?

1

Share this post


Link to post
Share on other sites

I always wondered about something related. Assuming at some Point you Need to know what class an object actually is of. Then, as far as I understand, I could try dynamic_cast<> to all possible classes. And that's O(n), if n is the number of possible classes. So having a map or hashtable to just look it up should theoretically be faster for large enough n. Is this correct or am I missing a possible way to use dynamic_cast<> efficiently for this?

If you are really hurting by this roll your own and type checking then boils down to a pointer address compare, which you can find in Game Programming Gems 2. This relies on staticly declared pointers in your RTTI capable classes, which you have to register your self. This is all the DECLARE_CLASS(CEnemy, CMoveableObject) macros that you see in the Half-Life codebase (https://developer.valvesoftware.com/wiki/Authoring_a_Logical_Entity).

If std::type_info::hash_code wasn't made deliberately stupid and useless, you wouldn't even need that. You could just use the language's built-in feature. It would even work perfectly for serialization.

 

Shame that usability was apparently not intended with N2530. A hash code that explicitly gives no guarantee about being the same between different invocations of the same program is entirely useless for the majority of things.

0

Share this post


Link to post
Share on other sites
To be fair to the poorly performing dynamic_cast implementations, they generally designed to work in corner situations that game developers rarely run into. Such as dynamic_casts from an object created in a DLL from code in another DLL that may have accidentally declared a type of the same name. In general C++'s dynamic_cast is meant for systems that look very different from games; systems that are long lived and require binary compatibility between revisions are places where dynamic_cast become a more reasonable option. Fortunately for everyone involved these kinds of systems are becoming more frequently implemented in managed languages.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0