Saving class objects in array suddenly tanking performance
7 views (last 30 days)
Show older comments
So, I have this tool I've developed to do some radar simulations. One small part of it is I use this class just for bundling and passing around detection data instead of a struct:
classdef Detection
% Data holder for a radar detection
properties
time (1,1) double
snr (1,1) double
raer (1,4) double
sigmas (1,4) double
end
methods
function obj = Detection(time,snr,raer,sigmas)
if nargin > 0 % Allow no args constructor to be called to make empty detections
obj.time = time;
obj.snr = snr;
obj.raer = raer;
obj.sigmas = sigmas;
end
end
end
end
These get created in one handle class then passed off to another that saves data on a given target. In that object, I save them into a (pre-allocated* array):
this.detections(i) = det;
After making some code changes yesterday, suddenly this one line went from being trivial computationally to 70% of the run-time of a basic sim run when profiled, more than tripling the run time of said sim from ~1s to ~3s for 761 detections saved. I can't figure out how to pull out any more details about what's suddenly making a simple operation like that so slow, or how to work around it. I've run into some weird behavior like this in the past when saving simple data holder objects caused bizarre performance issues, but usually I could tweak something or find a work around, here I'm just stumped as to what caused it because I didn't even change anything about the object or how they're saved.
*Specifically, they start with a base size and then if the sim runs long enough to outstrip that I start doubling the data arrays, just thought I'd add that in case there's some weird potential behavior there.
Edit: I tried making this class and the Dwell class (other one mentioned in comments) handles, and that seems to have alleviated the problem (although still seems noticeably slower than saving structs), but I'm curious as to why saving value classes is seemingly so slow.
6 Comments
Kevin Holly
on 9 Aug 2024
While handle classes might have some inherent overhead due to memory management, the benefits of avoiding deep copies often outweigh these costs, especially for large or complex objects. In your case, the significant performance improvement after switching to handle classes suggests that the overhead of copying value class objects was the primary bottleneck.
For more information, see Comparison of Handle and Value Classes and Avoid Unnecessary Copies of Data.
MATLAB's handling of structs vs. classes (especially value classes) can lead to significant performance differences due to copying and memory management overhead. For simple data holders, using structs is often more efficient. If you need the features of classes, handle classes can help mitigate copying overhead.
Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!