Major Tools
Other Tools
General Info

Issue #1021

avoid dynamic_cast for trivial casts

Added by Johan Bjork almost 2 years ago. Updated over 1 year ago.

% Done:



In most compilers dynamic_cast boils down to (at least) one string comparison. This showed up as a large bottleneck when verilating a medium/large sized design. For the cases where the type being casted to has no subclasses, we can simply re-use AstNode::type() and perform a static_cast.

This reduces verilation time on one of our designs by 10 seconds, (~60s->~50s)


#1 Updated by Wilson Snyder almost 2 years ago

  • Status changed from New to Resolved

No compiler I am aware of uses a string compare, the time delta comes from determining the child arangements. Anyhow the improvement is still good. I was a bit afraid that there might be some e.g. AstNUser cases where I had assumed that the data could take on more than just a node type (where this thus wouldn't work), but a audit didn't turn anything up, so I think this is clean.

Fixed in git towards 3.881.

#2 Updated by Wilson Snyder over 1 year ago

Note I may have to back this out due to bug1030 - it's illegal to test "this" for NULL, and it's important for code readability that pointer->castFoo work with pointer being null. So I think dynamic_cast must be used to be legal C.

#3 Updated by Johan Bjork over 1 year ago

I didn't quite realize what amount of work that new warning would entail, thanks for looking into it. Didn't know it was undefined until clang printed the warning. Anyway, I looked at why exactly doing the this compare is incorrect, and according to the standard it seems that undefined behaviour is caused by invoking a method on a null object, not the actual null compare, so I guess both this and the previous version would be undefined according to the standard for a null object. I'm obviously fine if you want to revert it for now, but might be worth thinking about another implementation that does not rely on calling methods on null objects. One idea would be to generate overloaded functions for each node type, i.e., Cast<NodeType>(nodep) for each type of node type. The implementation should be pretty much identical to the current one.

#4 Updated by Wilson Snyder over 1 year ago

  • Status changed from Resolved to Closed

In 3.882.

Also available in: Atom