Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Casting performance damage

Discussion in 'Getting Started' started by mikle-mikle200, Oct 10, 2023.

  1. mikle-mikle200

    mikle-mikle200

    Joined:
    May 2, 2023
    Posts:
    3
    I'm learning unity(as well as programming) for 6 months. Recently I've discovered Casey Muratory's "Clean code, terrible performance". After a while, I understood, that type casting in polymorphism is main issue why this may damage performance. However, there's a couple of questions, which I simply can't find an answer across the internet:

    1.As I know, C# itself is using polymorphism(interfaces especially as I know) almost everywhere. Doesn't it significantly damages its performance? Where can I read more about it?

    2.Does Unity itself use a lot of polymorphism under the hood? Isn't it an issue? Just imagine that unity is 25 times faster than now...

    3.Unity highly recommends using all of these Inheritance and Interfaces(as I understand), which means that Polymorphism and type casting involved. Why? How could I know, where it damages performance and where not?

    4.I used unity profiler to check performance of my code. I have 10k objects in a hashset. They are non-mono-behaviour, but a separate c# class, even not derived from something. They have some function, what they're doing is adding deltatime to float, so waiting 5 seconds and then doing some calculations and if checks. All of them are managed by MonoBehaviour script - 10k times every update. So, when these objects are pure c# class, this manager's update takes 1.2ms(worst case), but if Manager handles them as Interfaces(so type casting involved as I understand), I get the same 1.2ms(in worst case), and I can't even say if regular case isn't the exact same. So, what am I missing? Should I use different profiler to get better data or I simply got mad?

    And after all,
    5.Assuming that all these stuff doesn't damage neither c# performance, nor Unity's one, nor even my own in my project, do I even have to care about how should I use OOP?

    I'm really sorry if any of these questions look some kind of stupid. I was really impressed by 25 times performance increased by Casey, and would like to know how should I use these new cool concept that I've learned by him.
    Thanks!
     
  2. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    538
    If you want to use an interface, use an interface. Don't let that silly video confuse you. Just use the most appropriate tool to engineer what you want in the way that seems best. Measure the performance for yourself, and if there is a genuine problem, go from there. If not, just keep coding.
     
  3. mikle-mikle200

    mikle-mikle200

    Joined:
    May 2, 2023
    Posts:
    3
    Seems it's really simpler and maybe better to use an interface, but since I've already spent on this topic a bunch of time, I have to end up giving myself some certain answers on what's actually going on!
    Anyway, thanks for a good advice. It definitely calms me down, at least in terms of code designing.