Peculiar Issue: The same function when defined in a package is 10x slower

Hello!

I do not have a MWE since it is something I have spotted in developing a nested package. What I noticed is that if I have a function such as;

function SPHExample.ComputeInteractions!(Position, Kernel, KernelGradient, Density, Velocity, dρdtI, dvdtI, i, j, MotionLimiter, ρ₀, h, h⁻¹, m₀, αD, α, g, c₀, δᵩ, η², H², Cb⁻¹, ν₀, dx, SmagorinskyConstant, BlinConstant, ViscosityTreatment, BoolDDT, OutputKernelValues)
    xᵢⱼ² = evaluate(SqEuclidean(), Position[i], Position[j])
    if  xᵢⱼ² <= H²
        xᵢⱼ  = Position[i] - Position[j]
        
        dᵢⱼ  = sqrt(xᵢⱼ²)
        q    = clamp(dᵢⱼ * h⁻¹,0.0,2.0)
        invd²η² = inv(dᵢⱼ*dᵢⱼ+η²)
        ∇ᵢWᵢⱼ = @fastpow (αD*5*(q-2)^3*q / (8h*(q*h+η²)) ) * xᵢⱼ 
        ρᵢ        = Density[i]
        ρⱼ        = Density[j]
    
        vᵢ        = Velocity[i]
        vⱼ        = Velocity[j]
        vᵢⱼ       = vᵢ - vⱼ
        density_symmetric_term = dot(-vᵢⱼ, ∇ᵢWᵢⱼ) # = dot(vᵢⱼ , -∇ᵢWᵢⱼ)
        dρdt⁺          = - ρᵢ * (m₀/ρⱼ) *  density_symmetric_term
        dρdt⁻          = - ρⱼ * (m₀/ρᵢ) *  density_symmetric_term
        # Do a lot more computations

If I define it inside my package then it runs 10x as slow as if I overload it and use it in a new script?

Has anyone experienced something similar and can give some tips?

Kind regards

Could you show us how you are timing it?

2 Likes