Saving class objects in array suddenly tanking performance (2025)

William 2024 年 8 月 2 日 13:41

  • リンク

    この質問への直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2142436-saving-class-objects-in-array-suddenly-tanking-performance

  • リンク

    この質問への直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2142436-saving-class-objects-in-array-suddenly-tanking-performance

コメント済み: William 2024 年 8 月 2 日 14:44

MATLAB Online で開く

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.

5 件のコメント

3 件の古いコメントを表示3 件の古いコメントを非表示

Matt J 2024 年 8 月 2 日 13:54

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2142436-saving-class-objects-in-array-suddenly-tanking-performance#comment_3227611

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2142436-saving-class-objects-in-array-suddenly-tanking-performance#comment_3227611

After making some code changes yesterday

I doubt troubleshooting will be possible here without seeing what these changes were

William 2024 年 8 月 2 日 14:08

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2142436-saving-class-objects-in-array-suddenly-tanking-performance#comment_3227636

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2142436-saving-class-objects-in-array-suddenly-tanking-performance#comment_3227636

Didn't want to have to add tons of code, but also like I said the other changes weren't related to the object being saved or how it's saved.

I actually had a similar issue earlier in the project related to saving dwell parameters in an object that I thought I'd fixed, but actually looking at it again now it wasn't. Uncommenting that code was even worse performance-wise than saving the Detection objects. I just don't get why saving a class that is equivalent of a small struct is having terrible performance impacts when saving structs doesn't in my experience (and have worked on analysis code doing frankly horrific things with struct arrays that was mostly fine performance-wise).

Steven Lord 2024 年 8 月 2 日 14:37

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2142436-saving-class-objects-in-array-suddenly-tanking-performance#comment_3227691

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2142436-saving-class-objects-in-array-suddenly-tanking-performance#comment_3227691

Didn't want to have to add tons of code, but also like I said the other changes weren't related to the object being saved or how it's saved.

They may not have been directly related to the object being saved, but does that object have a property containing an instance of a class that you changed? That could indirectly affect the saving process of the object that contains the instance of that second class, particularly if handle objects are involved.

William 2024 年 8 月 2 日 14:42

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2142436-saving-class-objects-in-array-suddenly-tanking-performance#comment_3227696

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2142436-saving-class-objects-in-array-suddenly-tanking-performance#comment_3227696

MATLAB Online で開く

By "the object being saved", I meant instances of the Detection class above. The snippet I posted is the entire .m file of the Detection class, it's just a data holder for some doubles as I was trying to make the code a bit nicer to compared to just saving a bunch of matrices of data. The other object for dwell params is also just a data holder of primitives (i.e. all properties are double scalars), with the addition of some getter methods that all do things like

function duration_s = getDuration_s(this)

duration_s = this.numPulses * this.pri_s;

end

Those were originally dependent properties but I was testing out if swapping them to actual getter methods would affect anything.

William 2024 年 8 月 2 日 14:44

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2142436-saving-class-objects-in-array-suddenly-tanking-performance#comment_3227701

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2142436-saving-class-objects-in-array-suddenly-tanking-performance#comment_3227701

Also just wanna mention I edited the post with slightly more details on the performance impact, and that I tried swapping these value classes to handles and that has made the performance fine, but feels counterintuitive to me as I expected handles to have more overhead, not less (AFAIK this was historically the case with Matlab).

サインインしてコメントする。

Saving class objects in array suddenly tanking performance (2025)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Domingo Moore

Last Updated:

Views: 6461

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Domingo Moore

Birthday: 1997-05-20

Address: 6485 Kohler Route, Antonioton, VT 77375-0299

Phone: +3213869077934

Job: Sales Analyst

Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.