Search Unity

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

Discussion in 'Works In Progress - Archive' 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:
    1,056
    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.
     
  4. alpha_waves

    alpha_waves

    Joined:
    Oct 26, 2016
    Posts:
    3
    Did you ever finish this library? I would be interested in it.