Speed up the implementation of hasattr() checks (#14333)
This makes the implementation of hasattr() checks faster (introduced in
#13544).
In particular, since the `extra_attrs` attribute used for hasattr()
checks is usually None, I micro-optimized the codepaths to avoid
expensive operations whenever there are no hasattr() checks.
Also avoid expensive operations on simple unions and order `isinstance`
checks so that common types are checked first.
I measured a 2% performance uplift in self-check.