Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Feedback new Texture2D() has bad C# allocations and should be fixed

Discussion in '2022.2 Beta' started by nishikinohojo, Sep 2, 2022.

  1. nishikinohojo

    nishikinohojo

    Joined:
    Aug 31, 2014
    Posts:
    46
    new Texture2D() creates three GC allocations in total in Development build. Like this.

    2022y09m03d_060207084.png 2022y09m03d_060247949.png

    Two of them stem from SupportsTextureFormat which is internally used in Texture2D constructor.

    We can see SupportsTextureFormat causes problem just by calling it like below.
    This is entirely from bad C# code in Unity and should be avoided.
    Edit: Maybe I should mention what "bad" is. Boxing of enum causing it. Bad.

    2022y09m03d_060232754.png 2022y09m03d_060219087.png

    Third allocation is something I did not check where it from. Maybe new itself?

    This problem became serious to me when I have written custom solution for loading assets.
    Help me Unity guys;(

    Thanks!
     
    Last edited: Sep 3, 2022
  2. nishikinohojo

    nishikinohojo

    Joined:
    Aug 31, 2014
    Posts:
    46
    I put the culprit here just to clarify how sad this is.

    https://github.com/Unity-Technologi...time/Export/SystemInfo/SystemInfo.bindings.cs

    Code (CSharp):
    1.  
    2.         // The enums are only marked as obsolete in the editor.
    3.         /// <summary>
    4.         /// Determine if enum value is obsolete.
    5.         /// If multiple enum members refer to the same value,
    6.         /// the value is considered obsolete if all members are marked obsolete.
    7.         /// </summary>
    8.         internal static bool IsEnumValueObsolete(Enum value)
    9.         {
    10.             foreach (var enumMember in value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public))
    11.             {
    12.                 if (!object.Equals(enumMember.GetValue(null), value)) continue;
    13.  
    14.                 var isObsolete = enumMember.GetCustomAttributes(typeof(ObsoleteAttribute), false).Length != 0;
    15.                 if (!isObsolete)
    16.                 {
    17.                     return false;
    18.                 }
    19.             }
    20.  
    21.             return true;
    22.         }
    23.  
    24.  
    25.         static bool IsValidEnumValue(Enum value)
    26.         {
    27.             if (!Enum.IsDefined(value.GetType(), value))
    28.                 return false;
    29.  
    30.             if (IsEnumValueObsolete(value))
    31.                 return false;
    32.  
    33.             return true;
    34.         }

    SupportsTextureFormat using these with boxing.
    At the very least, we need an option to avoid this weird safety check.

    Maybe I have to use dnspy to obliterate it in the meantime? I do not know it is allowed or not though.
    But even if it is, it gonna be flipping tough because different dlls used for editor and runtime.
     
    Last edited: Sep 3, 2022
  3. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    505
    Have you submitted a bug report?
     
  4. nishikinohojo

    nishikinohojo

    Joined:
    Aug 31, 2014
    Posts:
    46
    Can I call this a bug without confirming by officials?
    This is, well, let's just say horrible but maybe not a bug as at least it does its job as expected.

    I thought bug report was only possible when officials ask us to do so.
    I've done reporting once very recently because they told me to do so. Processing takes long and it seems serious. I feel like if many amateurs send their unreliable repporting at their will as Bug Report, it would give Unity a load of stress.

    At the very least forum threads can be ignored.
     
    Last edited: Sep 4, 2022
  5. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    815
    A part of the bug reporting process is that it will be confirmed or not. You just have to submit it.

    Performance issues *are* bugs, they have a whole QA department just waiting for you. If you dont make a bugreport they wont see this at all!
     
  6. nishikinohojo

    nishikinohojo

    Joined:
    Aug 31, 2014
    Posts:
    46
    TBW paying money to them and "having to" report easy bugs makes no sense. May sounds harsh though. I'm not paid for it.
    Reporting takes time as a reporter. I don't have that much time.
    Edit: I thought reporting must be done with reproducible project attached but maybe not? If so easy thing can be reported more easily.

    This time I decided to ignore this issue by using dnSpy as I've already done that.
    Anyway, now I understand easy problem like this should not be reported here. I am not going to use Bug Reporter neighter, though.
     
    Last edited: Sep 4, 2022
  7. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    949
    No, the usual process is that you use the bug reporter (you don't need to attach your whole project), and then post about it here with a bug number.
     
  8. nishikinohojo

    nishikinohojo

    Joined:
    Aug 31, 2014
    Posts:
    46
    What I meant by saying that is I do not want to report this specific incident as a bug because I am lazy now. That is all. Sorry.
    But thank you for teaching me the usual process. I initially recognize this is not a bug though.

    Edit: I'm not going to do anything about this as I've already sorted it out for me. And I do not appear in this thread anymore. If you care this problem, please do bug report instead.
     
    Last edited: Sep 5, 2022
  9. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,009
    Summary:

    1. You take the trouble to investigate the problem: profiling, look for the source code in github, ...

    2. They tell you to report it or else this thread will be forgotten. You have almost a repro project with your first post and your answer is "I don't have time and it's not my job".

    3. You solve your problem, you do not provide useful information for someone in your same situation and on top of that you are rude answering that you will not post more here and that we are the ones to report it.

    OK o_O
     
  10. nishikinohojo

    nishikinohojo

    Joined:
    Aug 31, 2014
    Posts:
    46
    Thank you for summarizing:) Very kind of you. What a civilized world.

    Well sorry, now I stop being sarcastic. I just decided to apologize sincerely and reappear here.
    I just intentionally decided not to do anything useful in this therad, you surely have already noticed. Sorry about this.

    Things went wrong when I just irritated with TJHeuvel-net. I felt like being pused too hard. Maybe I just am not good at English though. Or just I easily get annoyed.

    Yes I am a jerk, but you guys really ought to know how to communicate each other more peacefully, too. (Somebody said fish and human can coexist peacefully:) But seems humans cannot.) I am sure this is exactly what you guys do not want to hear from ME... But after a few post, I now be condemned so badly. Yes I am rude but It is mutual in some capacity. Not that funny.
    Basically not all people are keen to contribute to Unity. Of course it is not duty nor something so important. Community is supported by charitable people decided to do so. I know you guys are too. But you should be a little bit more patient and try to support newbies more carefully to grow the community for the greater good. I am a newbie here, too.

    Anyway, if I should do, I will report when I have time. Maybe at this point someone else has done that? If not I will do.
     
    Last edited: Sep 6, 2022
  11. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    815
    Just want to say i agree with you; it takes a lot of time to report issues, and i also sometimes feel like i am Unity's QA. That is frustrating.
     
    Prodigga, PutridEx and nishikinohojo like this.