-
Notifications
You must be signed in to change notification settings - Fork 357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid using Dynamic_cast #2604
Avoid using Dynamic_cast #2604
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from the fact that the wholemodelrange_manager
design is quite an antipattern in itself, this change actually makes sense.
Node* vt = kernel().node_manager.get_node_or_proxy( vtnode_id, tid ); | ||
vt_ = dynamic_cast< volume_transmitter* >( vt ); | ||
if ( not vt_ ) | ||
std::string model_name = ClassToString( volume_transmitter ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the macro necessary; why can't we just use the const string literal "volume_transmitter"
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that you say it, using the macro is actually incorrect here, as the name of the class does not have to match the name of the model. This works as a workaround, but what we would actually need would be a way to retrieve the type_id of a model from the type, so a function of the form:
index ModelManager::get_type_id<volume_transmitter>();
However, in this case, we could just assume that volume_transmitter
is also registered as "volume_transmitter"
, even though this isn't the cleanest way and might break the code if the registration was to be changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was expecting this comment, and I was planning to implement a static function that stores a default name of the model, but then this function must be implemented in all models.
I think, this raises the issue that models should have default names regardless of the registered name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not understand the purpose of this change. Why do you not want to use the dynamic cast? Isn't that C++'s built-in way of testing whether a pointer points to a given type? Or is there a problem when applying CopyModel
to a volume transmitter?
@@ -31,6 +31,8 @@ | |||
// Includes from sli: | |||
#include "dictdatum.h" | |||
|
|||
#define ClassToString( cls ) #cls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this macro work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's just a matter of performance, but now that Charl mentioned the problem with these changes, I don't think this change makes sense anymore, even if it would change performance at all. Not that this part of the code would be performance-critical/-relevant at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The macro simply takes the input and interprets it as the actual characters that were given to that macro. So all it does is change volume_transmitter
to "volume_transmitter"
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we can apply certain checks if the underlying Node
object can be converted to volume_transmitter
, we can avoid using the dynamic_cast
and therefore avoid the runtime overhead used for the conversion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You would have to perform some profiling, though. All the overhead required for the manual type-checking might actually outweigh the overhead of the dynamic_cast
, as calling the RTTI might actually not consume that much more time. The branching will be what costs us a lot of cycles as well, and the if will still persist with your solution. So in total there might not even be an advantage of doing manual type-checking here.
The
Model
class provides atype_id
field that can be used for applying type checking on theNode
instance, and avoid using thedynamic_cast
.