While Julia is designed as a dynamic language, it also aims for high performance. The magic comes from its state-of-the-art compiler. When the type of variables is known in a function, the compiler can generate highly optimized code. However, when the type of a variable is unstable, the compiler has to compile more generic code that works with any data types. In some sense, Julia can be forgiving—it never fails on you even when it comes with a cost against runtime performance.
What makes the type of a variable unstable? It means that in some circumstances the variable may be one type, and in other circumstances, it may be another type. This section will discuss such a type instability problem, how it may arise, and what we can do about it.
Barrier function is a pattern that can be used to solve performance problems due to type instability...