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

Feature Request Renaming GameObjects without generating garbage.

Discussion in 'Scripting' started by adehm, Jun 9, 2023.

  1. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    I was reading a thread from 2018 on this and it was dismissed but I think this is still a valid request. For 3D objects animations are tied to the names of their parts, the bone structure, so how can I build characters in real time without generating garbage? It would be far too much memory to have all of the characters I would ever use preloaded so instead I build characters in real time from their parts but to do that I must rename their bones and in doing so generate garbage which any of which could cause a lag spike. I am using incremental garbage collection now but fear it will not work, if it does work at all, when creating dozens of characters in a few seconds. In my preliminary tests inside the editor I did have lag spikes when generating one hundred characters, one per a second.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Have you even instrumented this in any way shape or form to even see if it is causing you an issue??

    This is like worrying about how thick the wax polish is on your sports car because it might slow you down.

    As a point of reference, my KurtMaster2D game streams back up to 2k worth of strings every single frame.

    Those strings look like this:

    Code (csharp):
    1. userintent:29,114,action3;userintent:110,114,action4;userintent:175,114,accept;playing:0;ok:oneframe
    I use string.Split() to chop it up and process each of the parts... EVERY SINGLE FRAME.

    I have zero issues from garbage collection.

    It looks like:

    Code (csharp):
    1.     static void process_oneframe_results( string results)
    2.     {
    3.         string[] sl = results.Split( ';');
    4.         foreach (string s in sl)
    5.         {
    6.             if (s.StartsWith( "snd:"))    // play a sound
    7.             {
    8. ...etc
    DO NOT OPTIMIZE "JUST BECAUSE..." If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler first means you're just guessing, making a mess of your code for no good reason.

    Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413

    At a minimum you want to clearly understand what performance issues you are having:

    - running too slowly?
    - loading too slowly?
    - using too much runtime memory?
    - final bundle too large?
    - too much network traffic?
    - something else?

    If you are unable to engage the profiler, then your next solution is gross guessing changes, such as "reimport all textures as 32x32 tiny textures" or "replace some complex 3D objects with cubes/capsules" to try and figure out what is bogging you down.

    Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

    This sort of speculative optimization assumes you're properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.
     
  3. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    I already said I did run it and and did notice it, I move the camera back and forth with my mouse while they are spawning and I get hiccups of imperfect smoothness. I went further and made a windows build, the target platform, and although it is very slight now I do notice these hiccups.

    Sounds like it would be really laggy and I experimented with that idea when first starting to use the .net framework and ruled it out as a possibility; why don't you atleast use character arrays and iterate over the data yourself?

    I notice the choppiness that comes from generating garbage and I would hope most people do and I'm sure your game is no exemption. Do you have a web build I could test? If you prove it is smooth to me then do you have a source to prove you are in fact doing that?

    I hear this spiel from you all the time and will always disagree. What's the point of having to rewrite all your code? Makes sense for prototyping, sure, but that is bad advice.
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    I tend to half-agree if we are discussing this on a level of professional programming, however I can't agree with your sentiment based on the fact that Kurt is typically addressing people who are below the level of professional programming.

    For them this is 99.99999% of times the absolute truth, and even your case might be the case of over-analysis and OCD. First make sure that this garbage is indeed the source of any problems. Don't philosophize about it without any evidence.

    Noticing is not evidence. Maybe it's Unity's fault. Maybe your OS is running an anti-virus scan in the background. Try investigating into this, and provide some evidence, and who knows, your feedback might push Unity to fix some things.

    If you're just angry and superstitious without any evidence to back any of this up, I doubt this will yield any results.

    Finally, learn how strings work in C#. Referring to the same strings over and over does not produce any garbage. Strings are immutable reference objects and compiler resolves all literal strings in a process called string interning.
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    How's that Unity's fault btw? That's your responsibility. As said already, make sure you don't produce names by concatenating, and also can you think of a way to do this more gracefully? You can't just plow with your "solution" like a bulldozer and then be disgruntled with it.
     
  6. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    You're right. I turned off all character functionality and these hiccups didn't happen. I jumped to blame that because that was something I know could be the cause but in my case it is just not generating enough to be the problem and must be caused by behavior or pathing somewhere.

    When did I say that? Kurt is most definitely generating garbage with that snippet by creating a string array and splitting into it.
     
  7. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    I pull the names from preloaded strings that do not change but a new one is created when naming a GameObject and the bone animation systems works off of names and not hierarchy.
     
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Okay, first, make sure how exactly you're making the names (use StringBuilder as mentioned above). Second, make your strings ahead of time, additionally recycle them with a separate service, see if this will make any difference.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Ferociously I am doing this.

    But it doesn't MATTER.

    That's the point.

    My theory is that it does not matter for two primary reasons:

    - strings are small
    - strings are interned (and a lot of my strings are the same)

    You might THINK your string renaming of the parts is a problem but unless you are doing something crazy like renaming them every frame, I still stand by my thesis.

    Let me state my thesis:

    You renaming the parts of your character should NEVER be a source of garbage collection that actually impacts performance.

    Disclaimer: unless you are seriously doing something wrong (eg, unusual, out of the normal Unity Way(tm) of working).

    THEREFORE: if you are seeing performance issues it is NOT because of string-generated garbage. It is something else. Go find it.
     
    orionsyndrome likes this.