Type hints were one of the major features that Python 3 introduced. Their original limited use-cases centered around type checkers have gradually expended, as well as their complexity, but they are generally considered to not affect runtime behavior and to not have any cost.
Imagine my surprise when in one of the continuous profile reports I noticed a significant percentage of CPU cycles spent on a line returning a Thrift instance field casting it to dictionary from Thrift’s mapping to please the type checker:
Considering that cast should not have any effect except for pleasing the type checker, how expensive could it be? Let’s check
Ouch, almost a microsecond! It makes very little sense for cast to take so much time, so I immediately suspected Dict[str, str].
Sure enough, looks like most of the cost is indeed taken by Dict[str, str].
Now that we know where the problem comes from, the solution becomes pretty obvious - cache the type
Almost an order of magnitude improvement with such a small change. Depending on the type complexity, it can have even larger impact and may also improve readability.
This story reminded me yet another time that zero-cost abstraction is a scarce feature that often cannot be delivered even in C++, so it’s useful to not forget about this when dealing with other languages.
it's almost a microsecond, not a millisecond
Please be specific about the C++ situation.