Whenever you introduce a function alias, do so with const, otherwise it will be considered a non-const global and be bad for performance. When you call it just a few times in vectorized form it doesn’t matter so much but as soon as you call it in a hot loop it will be a disaster.
const γ = loggamma # let the γ represents the loggamma() function
In your function you compute a lot of values pointwise and then sum them up. There is no need to have all values available at the same time, and in particular not all the intermediate values in the calculations. Moreover, if you don’t vectorize each step, you can avoid computing both (A, B, C, D) and (E, F).
This is an easy transformation which avoids most of the unnecessary allocations and computations:
myFunc(Θ, M, Π, X) = sum(myFunc2.(Θ, M, Π, X))
function myFunc2(Θ, M, Π, X)
eps_ = 1f-10
if X < 1f-8
E = (Θ/( Θ + M + eps_))^Θ
F = -log(Π + (1f0 - Π) * E + eps_)
else
A = γ(Θ + eps_) + Float32(γ(X + 1)) - γ(X + Θ + eps_)
B = (Θ + X) * log( 1 + M / (Θ + eps_) ) +
X * log(Θ + eps_) - log(M + eps_)
C = A + B
D = C - log(1f0 - Π + eps_)
end
end