diff --git a/LibDmd/Converter/AbstractConverter.cs b/LibDmd/Converter/AbstractConverter.cs index e6b77749..441c8fd8 100644 --- a/LibDmd/Converter/AbstractConverter.cs +++ b/LibDmd/Converter/AbstractConverter.cs @@ -100,8 +100,8 @@ public virtual void Convert(DmdFrame frame) } #endif _lastDmdFrame = PadSmallFrames && frame.Dimensions.IsSmallerThan(Dimensions.Standard) - ? frame.Update(Dimensions.Standard, frame.CenterFrame(Dimensions.Standard, frame.Data, frame.BytesPerPixel), frame.BitLength) - : frame; + ? DmdFrame.GetFromPool().Update(Dimensions.Standard, frame.CenterFrame(Dimensions.Standard, frame.Data, frame.BytesPerPixel), frame.BitLength) + : DmdFrame.GetFromPool().Update(frame); } /// @@ -140,6 +140,7 @@ private void Tick(long _) { if (_lastDmdFrame != null) { ConvertClocked(_lastDmdFrame); + _lastDmdFrame.ReturnToPool(); } if (_lastAlphanumFrame != null) { diff --git a/LibDmd/Frame/DmdFrame.cs b/LibDmd/Frame/DmdFrame.cs index 9ee7413e..70742aa9 100644 --- a/LibDmd/Frame/DmdFrame.cs +++ b/LibDmd/Frame/DmdFrame.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Text; @@ -44,6 +45,8 @@ public class DmdFrame : BaseFrame, ICloneable, IEqualityComparer public static bool operator == (DmdFrame x, DmdFrame y) => Equals(x, y); public static bool operator != (DmdFrame x, DmdFrame y) => !Equals(x, y); + private static ObjectPool Pool = new ObjectPool(); + public FrameFormat Format { get { switch (BitLength) { @@ -554,6 +557,27 @@ public override string ToString() #endregion + #region Pool + + private class ObjectPool + { + private readonly ConcurrentBag _objects; + + public ObjectPool() + { + _objects = new ConcurrentBag(); + } + + public DmdFrame Get() => _objects.TryTake(out DmdFrame item) ? item : new DmdFrame(); + + public void Return(DmdFrame item) => _objects.Add(item); + } + + public static DmdFrame GetFromPool() => Pool.Get(); + public void ReturnToPool() => Pool.Return(this); + + #endregion + #region Debug public void Dump(string path, string prefix = null)