High performance data structure for use in image modelling

Hi there,
I am writing some code which fits a model consisting of two-dimensional gaussian functions to an image. At the moment, I am using a mutable struct which holds the information about the model as well as a vector of immutable structs which each correspond to one of the gaussian functions. I then use the Optimization.jl package to optimize the parameters of each of the gaussians to achieve the minimum least squares difference between the model and the image.
The code is working as intended, but the number of heap allocations is quite high and a lot of time is spent in GC. In principle, the number of parameters for each gaussian function is fixed (6) and the number of gaussians in the model is known at the moment the model is instantiated, so I feel like some optimization can be done. Is there a better way to encode the parameters of the gaussians so that my code performs better? Two options I have in mind are these:

  • An n-element vector of static vectors of size 6
  • A 6 x n matrix containing the parameters of each gaussian function

Which of these would be best, or is there some other data structure that is better to use?

We really need to see code to make suggestions for something like this, unless you want a lot of “it depends”

1 Like

Your first step should be to profile it and visualize the resulting flame graph, looking for red bars (which indicates “type-instability”). Instructions are at Profiler · Julia in VS Code or GitHub - timholy/ProfileView.jl: Visualization of Julia profiling data