Why does wrapping an array in a composite type degrade performance?

Thanks @orialb for your suggestion. The reason I wanted to have vars (variables) and card (cardinality or dimension size) as type parameters is to avoid type instability. Here is one example of a function that takes the MyFactor type as argument:

function marginalize(A::MyFactor{T,N} where N, V::Vector{Int64}) where T 
  dims = indexin(V, collect(A.vars)) # map vars to dims
  r_size = ntuple(d->d in dims ? 1 : size(A.vals,d), getdims(A)) # assign 1 to summed out dims
  ret_size = filter(s -> s != 1, r_size)
  ret_vars = filter(v -> v ∉ V, A.vars)
  r_vals = similar(A.vals, r_size)
  ret_vals = sum!(r_vals, A.vals) |> x -> dropdims(x, dims=Tuple(dims))
  MyFactor{eltype(A.vals),length(ret_vars)}(ret_vars, ret_vals)
end

Using your MyFactor definition, the compiled code is not type stable.

So ideally what I would like, is to have vars and card in the type (which gives me type stability) but without having to allocate data on the heap (which apparently is being done based on the info of my first message)