SVector and Type Stabilty

I am struggling to understand the type instability of

function foo(x,y,z,r)
    r_prime = Point3D(x,y,z)
    result = r - rprime 
end 

given as input

x = 0.; y = 0.; z = 0.;
r = Point3D(1.,1.,1.)
@code_warntype foo(x,y,z,r)

The output I obtain is

MethodInstance for foo(::Float64, ::Float64, ::Float64, ::SVector{3, Float64})
from foo(x, y, z, r::SVector{3, Float64}) in Main at In[78]:1
Arguments
#self#::Core.Const(foo)
x::Float64
y::Float64
z::Float64
r::SVector{3, Float64}
Locals
result::Any
r_prime::SVector{3, Float64}
Body::Any
1 ─ (r_prime = Main.Point3D(x, y, z))
│ %2 = (r - Main.rprime)::Any
│ (result = %2)
└── return %2

Help is much appreciated.

Where is Point3D defined?

const Point3D = SVector{3,Float64}

It is very likely that Point3D is somehow an alias for SVector, and the source of type instability is rprime (in result = r - rprime), which is some leftover from the running session (of type Any) and should be r_prime instead.

2 Likes

Apologies for the noice.