I come to Julia from R and have never thought much about compilation or compiler optimizations. As far as I am aware, a simple optimization that a compiler can implement is to take an operation outside of a loop, e.g.
data = [1, 2, 3] x = 1:100 for i in 1:100 N = length(data) sum(data - x[i]) / N end
data = [1, 2, 3] x = 1:100 N = length(data) for i in 1:100 sum(data - x[i]) / N end
Now suppose we define a function to do the same task:
foo(data, x) = sum(data - x) / length(data) for i in 1:100 foo(data, x[i]) end
or we broadcast
foo() over x, instead:
I think the following questions are all related.
- Would the compiler compute
foo()when called within a for-loop? Outside of the call to
- Does the answer depend on whether
- Does the compiler always respect the boundaries of non-inlined functions? I.e. Would the compiler ever inline only part of the body of a function.