When trying to write type-stable code, examining using @code_warntype
, should I only worry about not having any Any
or Union
in the variables section or I should also check the body section?
Depending on your version of julia, @code_warntype
can sometimes flag certain expressions that are harmless. With a little bit of practice you can easily recognize these expressions (e.g., they often have to do with module/name lookup).
The easy path, as Reza says, is to look at the variables section and the function's return-type; if you don't see any type-uncertain quantities (either Any
or Union
s) you probably have nothing to worry about. If you want to dig deeper, start trying to make sense of the body expressions that it's flagging. For learning, one useful thing to do is compare the results against those obtained with track-allocation
, since true type instability is associated with memory allocation.
However, I think the issues with @code_warntype
have been fixed on current master, so in any case the next release of julia should make it easier to interpret.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With