Why haven't you tried to profile it? That's is the first thing that anyone that would try to help you would do. Use https://github.com/timholy/ProfileView.jl see what is slow and see if it is explained in the performance guide.
Then you can ask a much better question, like "why is this statement" slow instead of posting a whole function and ask someone to optimize the whole thing.
