From b2a217d4a3248d8b76b86f9021b49280901daf09 Mon Sep 17 00:00:00 2001 From: wrenge Date: Wed, 13 Nov 2024 13:21:41 +0300 Subject: [PATCH] Object pool --- src/DotRecast.Core/Buffers/RcObjectPool.cs | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/DotRecast.Core/Buffers/RcObjectPool.cs diff --git a/src/DotRecast.Core/Buffers/RcObjectPool.cs b/src/DotRecast.Core/Buffers/RcObjectPool.cs new file mode 100644 index 0000000..c37cf60 --- /dev/null +++ b/src/DotRecast.Core/Buffers/RcObjectPool.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; + +namespace DotRecast.Core.Buffers +{ + // This implementation is thread unsafe + public class RcObjectPool where T : class + { + private readonly Queue _items = new Queue(); + private readonly Func _createFunc; + + public RcObjectPool(Func createFunc) + { + _createFunc = createFunc; + } + + public T Get() + { + if (_items.TryDequeue(out var result)) + return result; + + return _createFunc(); + } + + public void Return(T obj) + { + _items.Enqueue(obj); + } + } +} \ No newline at end of file