ferram4 / Ferram-Aerospace-Research

Aerodynamics model for Kerbal Space Program
Other
238 stars 131 forks source link

Add null-check around access of prefab-part's model transforms #150

Closed shadowmage45 closed 7 years ago

shadowmage45 commented 7 years ago

For cases where transforms do not exist on the prefab (procedural/modular parts).

PR-fix for #148

Please let me know if anything needs changed/fixed.

ferram4 commented 7 years ago

Try switching it from prefabTransform == null to (object)prefabTransform == null and see if it still functions correctly. Checking null directly on some Unity objects involves a lot more than just seeing if the reference itself is null (such as checking if it's been destroyed by the game, because the native Unity stuff is weird), and that can add additional overhead for the cases where it isn't null. If you don't need that functionality, cast it to object first for performance improvements.

shadowmage45 commented 7 years ago

Noted, and will do.

Good point on the null-check, as in this case it really only matters if it is actually a null reference as there shouldn't be any destroyed-yet-uncollected references on the prefab.

I wasn't aware that there could be a performance improvement from additional casting, but in this situation you are likely correct; the Unity derived equals/null overrides come at a higher penalty than the casting, and casting upwards to a base class is usually fairly cheap anyway.

Will get this updated after I get home from work this evening.

blowfishpro commented 7 years ago

Out of curiosity, would object.ReferenceEquals(prefabTransform, null) have the same effect of avoiding the Unity internals?

shadowmage45 commented 7 years ago

Have updated with the (object) cast and the null-check still appears to function properly.

ferram4 commented 7 years ago

@blowfishpro I have no idea, I suspect yes. Probably should get some profiling in to see if there is a difference in performance.

@shadowmage45 Alright, I'll merge it when I get a chance to test it myself. Thanks.