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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Badly optimised: new List<Class1>() { instance1 }

Discussion in 'Scripting' started by andyz, Apr 9, 2018.

  1. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,129
    looking closely at List initialising it seems easy to make poor choices.

    Essentially if you want to init a List with 1 item you should create a list of capacity 1 and then add the first item.

    Either allocate a list of size 1 and add, or combine the statement (VS always prompts you to do this) to:
    list = new List<Class1>(1){instance1};

    If you miss out the 1 capacity the compiler does not realise you will need capacity 1 and does a whole load of stuff - growIfNeeded, set_Capacity, Resize etc. - visible in the Profiler with extra garbage.

    Could the compiler not be more clever and look at the initial input count or is this issue just common knowledge among C# speedsters?
     
    eisenpony likes this.
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    If you don't need the convenience of List then just use an array.
     
  3. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,129
    I know but I often add more items, just need to start with 1.
    Just have to remember to specify capacity as the compiler can not see it needs be 1 and adds a little overhead because of it, it would seem.
     
  4. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    That's why the compiler can't make any assumptions about the capacity unless you explicitly tell it the capacity should be 1.
     
    LaneFox likes this.
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    TBH I'm having a really hard time imagining why a List would have its capacity default to 0...
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, from what I can gather, it's zero with its default constructor, regardless of the initialized elements.
    At size zero, plus an 'Add' you get a capacity of 4.

    Setting the capacity is good, if you know it ahead of time. :)
     
    eisenpony likes this.
  7. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,129
    Yes I think I am expecting the compiler to do more than it can. Well I will just tell it explicitly
     
  8. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    It's because a collection initializer is syntactic sugar. All that's required to make use of the initializer syntax is a class that implement IEnumerable and a method called "Add" which accepts the appropriate parameters.

    What's not required is a way to tell the object how many items are in the initializer, so the compiler has no way to "be smart" as you describe.

    I doubt it's terribly important because hopefully you don't do too much initialization, especially in a tight loop, but it's still a good tip to be aware of. Thanks.