Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

[WIP] - Memory Safe Collection - No more heap allocation using Foreach

Discussion in 'Works In Progress' started by giovannicampo, Dec 5, 2015.

  1. giovannicampo

    giovannicampo

    Joined:
    Sep 16, 2015
    Posts:
    5
    Hi all,

    I've been working on class library that would allow to use safely foreach's in Unity without incurring in the penalty of generating garbage. As most developers know, a foreach in C# calls GetEnumerator() on a list type, which will allocate an enumerator on the heap. This process is notorious for generating garbage and it is highly avoided, especially when targeting mobile platforms as the cost of running the garbage collector often might be too high to keep a good frame rate. I based my project on the The C5 Generic Collection Library for C#. C5 provides functionality and data structures not provided by the standard .Net System.Collections.Generic namespace, such as persistent tree data structures, heap based priority queues, hash indexed array lists and linked lists, and events on collection changes. My work was to use the C5 library as a base to implement a memory safe enumerator for most collections. Not all the data structures are currently memory safe as I'm working on converting them to use memory safe enumerators.

    I have a couple of screenshot here, just to explain in more detail what I'm doing. Look at the pictures below:

    code.png allocationperframe.png allocationperframe.png

    As you can see no memory is allocated using foreach's on collection like array list, sorted array list or dictionaries. This is what would happen if you do the same using the built-in collection in the .NET/Mono framework (or the standard C5 without my changes):

    netcode.png
    netscriptgalloc.png

    Iterating on .NET collections and dictionaries using a foreach allocates memory on the heap. The allocated memory size may vary depending on the size of the enumerator of the specific collection. Clearly the same result could be obtained using a normal for-loop which doesn't allocate any garbage but it cannot be used on dictionaries, keys and values.

    I hope you've got the gist of it. I would love to hear your opinions, and if you think such library could be worthwhile or not.

    Thank you all.
    Giovanni
     

    Attached Files:

  2. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    825
    What advantage does this have over just using a normal 'for' loop?
     
  3. giovannicampo

    giovannicampo

    Joined:
    Sep 16, 2015
    Posts:
    5
    There are several advantages of using foreach instead of for loops. A foreach increases the abstraction level - instead of having to express the low-level details of how to loop around a list or array (with an index or iterator) and offers a concise higher level syntax to loop over a list or array which improves clarity and readability.

    Apart from that, dictionaries don't implement indexers, therefore if you need to access keys or values of a dictionary the only option if using an enumerator and a foreach. I've been working with C# for almost 10 years and I was really surprised when I switched to Unity that foreach's are not a good practice because of the garbage generated.