using System.Runtime.CompilerServices; using Jint.Native; namespace Jint.Pooling; /// /// Cache reusable array instances as we allocate them a lot. /// internal sealed class JsValueArrayPool { private const int PoolSize = 15; private readonly ObjectPool _poolArray1; private readonly ObjectPool _poolArray2; private readonly ObjectPool _poolArray3; public JsValueArrayPool() { _poolArray1 = new ObjectPool(Factory1, PoolSize); _poolArray2 = new ObjectPool(Factory2, PoolSize); _poolArray3 = new ObjectPool(Factory3, PoolSize); } private static JsValue[] Factory1() { return new JsValue[1]; } private static JsValue[] Factory2() { return new JsValue[2]; } private static JsValue[] Factory3() { return new JsValue[3]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public JsValue[] RentArray(int size) { if (size == 0) { return []; } if (size == 1) { return _poolArray1.Allocate(); } if (size == 2) { return _poolArray2.Allocate(); } if (size == 3) { return _poolArray3.Allocate(); } return new JsValue[size]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void ReturnArray(JsValue[] array) { // Ensure that the array contents are cleared // to allow garbage collecting the values of the // array if possible. Only the array itself // should be cached. if (array.Length == 1) { array[0] = null!; _poolArray1.Free(array); } else if (array.Length == 2) { array[0] = null!; array[1] = null!; _poolArray2.Free(array); } else if (array.Length == 3) { array[0] = null!; array[1] = null!; array[2] = null!; _poolArray3.Free(array); } } }