public override void Return(T[] array, bool clearArray = false) { if (array is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } // Determine with what bucket this array length is associated int bucketIndex = Utilities.SelectBucketIndex(array.Length); // Make sure our TLS buckets are initialized. Technically we could avoid doing // this if the array being returned is erroneous or too large for the pool, but the // former condition is an error we don't need to optimize for, and the latter is incredibly // rare, given a max size of 1B elements. ThreadLocalArray[] tlsBuckets = t_tlsBuckets ?? InitializeTlsBucketsAndTrimming(); bool haveBucket = false; bool returned = true; if ((uint)bucketIndex < (uint)tlsBuckets.Length) { haveBucket = true; // Clear the array if the user requested it. if (clearArray) { Array.Clear(array); } // Check to see if the buffer is the correct size for this bucket. if (array.Length != Utilities.GetMaxSizeForBucket(bucketIndex)) { throw new ArgumentException(SR.ArgumentException_BufferNotFromPool, nameof(array)); } // Store the array into the TLS bucket. If there's already an array in it, // push that array down into the per-core stacks, preferring to keep the latest // one in TLS for better locality. ref ThreadLocalArray tla = ref tlsBuckets[bucketIndex]; T[]? prev = tla.Array; tla = new ThreadLocalArray(array); if (prev is not null) { PerCoreLockedStacks stackBucket = _buckets[bucketIndex] ?? CreatePerCoreLockedStacks(bucketIndex); returned = stackBucket.TryPush(prev); } }
public void GlobalSetup() { this.threadLocalArray = new ThreadLocalArray(new byte[128]); }