using System; using System.Collections.Concurrent; using System.Runtime.CompilerServices; namespace UniRx.Async.Internal { internal static class StateTuple { public static StateTuple Create(T1 item1) { return StatePool.Create(item1); } public static StateTuple Create(T1 item1, T2 item2) { return StatePool.Create(item1, item2); } public static StateTuple Create(T1 item1, T2 item2, T3 item3) { return StatePool.Create(item1, item2, item3); } } internal class StateTuple : IDisposable { public T1 Item1; public void Deconstruct(out T1 item1) { item1 = this.Item1; } public void Dispose() { StatePool.Return(this); } } internal static class StatePool { static readonly ConcurrentQueue> queue = new ConcurrentQueue>(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static StateTuple Create(T1 item1) { if (queue.TryDequeue(out var value)) { value.Item1 = item1; return value; } return new StateTuple { Item1 = item1 }; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Return(StateTuple tuple) { tuple.Item1 = default; queue.Enqueue(tuple); } } internal class StateTuple : IDisposable { public T1 Item1; public T2 Item2; public void Deconstruct(out T1 item1, out T2 item2) { item1 = this.Item1; item2 = this.Item2; } public void Dispose() { StatePool.Return(this); } } internal static class StatePool { static readonly ConcurrentQueue> queue = new ConcurrentQueue>(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static StateTuple Create(T1 item1, T2 item2) { if (queue.TryDequeue(out var value)) { value.Item1 = item1; value.Item2 = item2; return value; } return new StateTuple { Item1 = item1, Item2 = item2 }; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Return(StateTuple tuple) { tuple.Item1 = default; tuple.Item2 = default; queue.Enqueue(tuple); } } internal class StateTuple : IDisposable { public T1 Item1; public T2 Item2; public T3 Item3; public void Deconstruct(out T1 item1, out T2 item2, out T3 item3) { item1 = this.Item1; item2 = this.Item2; item3 = this.Item3; } public void Dispose() { StatePool.Return(this); } } internal static class StatePool { static readonly ConcurrentQueue> queue = new ConcurrentQueue>(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static StateTuple Create(T1 item1, T2 item2, T3 item3) { if (queue.TryDequeue(out var value)) { value.Item1 = item1; value.Item2 = item2; value.Item3 = item3; return value; } return new StateTuple { Item1 = item1, Item2 = item2, Item3 = item3 }; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Return(StateTuple tuple) { tuple.Item1 = default; tuple.Item2 = default; tuple.Item3 = default; queue.Enqueue(tuple); } } }