Allowing iteration without generating any garbage

前端 未结 7 1303
时光取名叫无心
时光取名叫无心 2021-02-01 04:49

I have the following code in an object pool that implements the IEnumerable interface.

public IEnumerable ActiveNodes
{
    get
    {
        for (int i         


        
7条回答
  •  青春惊慌失措
    2021-02-01 05:19

    Dont be afraid of those tiny garbage objects. The ActiveNodes in the pool will (should) be much more costly. Therefore, if you get rid of recreating them it should be sufficient.

    @Edit: if you are made to use a managed platform and really want to archieve a zero-garbage state, disclaim the usage of the pool in a foreach loop and iterate over it in another manner, possibly utilizing an indexer. Or consider creating a list of potential nodes and return that instead.

    @Edit2: Of course, implementing IEnumerator und using Current(), Next() and so on, would work as well.

提交回复
热议问题