P
P
Pragma Games2021-06-07 13:33:52
Algorithms
Pragma Games, 2021-06-07 13:33:52

What is the most efficient implementation of the ObjectPooler design pattern?

Simplified ObjectPooler code, the question is which is better to use for storing objects, List or Queue or something else, what's the difference in performance? The implementation of the GetObject functions, depending on the data structure that we use, the RemoveActivity function is only for working with the queue. When working with a sheet, we check the entire sheet to find the first active element and return it (The sheet contains both active and inactive elements), we change the activity after the element has worked in the code that called the GetObject function. The queue contains only active elements, so we simply extract the element if there are available, but when the element has completed it must be returned to this queue again.

For those who are not familiar with Unity, but are familiar with the pattern
.activeInHierarchy - returns true if the object is active on the stage
GameObject is the base class for all objects on the stage

public class ObjectPoolItem
{
    public List<GameObject> Objects;

    public ObjectPoolItem()
    {
        Objects = new List<GameObject>();
    }
}

private Dictionary<Types, ObjectPoolItem> _pool;

public GameObject GetObjectList(Types type)
{
        for(int i = 0; i < _pool[type].Objects.Count; i++)
        {
              if (!_pool[type].Objects[i].activeInHierarchy)
              {
                    return _pool[type].Objects[i];
              }
        }

        return null;
}

public GameObject GetObjectQueue(Types type)
{
        if (_pool[type].Objects.Count > 0)
        {
                return  _pool[type].Objects.Dequeue();
        }

        return null;
}

// Только при работе с очередью
public void RemoveActivity(GameObject obj)
{
        _pool[obj.GetComponent<IPooleable>().Type].Objects.Enqueue(obj);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2021-06-08
@sarapinit

What is the most efficient implementation of the ObjectPooler design pattern?

Microsoft.Extensions.ObjectPool<T>probably
from the Microsoft.Extensions.ObjectPool package

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question