avoid dynamic_cast for trivial casts
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) https://github.com/phb/verilator-dev/tree/less_dyncast
#1 Updated by Wilson Snyder about 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.
#3 Updated by Johan Bjork about 2 years 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.
Also available in: Atom