public override void RecycleByteBlocks(byte[][] blocks, int start, int end) { int numBlocks = Math.Min(maxBufferedBlocks - freeBlocks, end - start); int size = freeBlocks + numBlocks; if (size >= freeByteBlocks.Length) { var newBlocks = new byte[ArrayUtil.Oversize(size, RamUsageEstimator.NUM_BYTES_OBJECT_REF)][]; Array.Copy(freeByteBlocks, 0, newBlocks, 0, freeBlocks); freeByteBlocks = newBlocks; } int stop = start + numBlocks; for (int i = start; i < stop; i++) { freeByteBlocks[freeBlocks++] = blocks[i]; blocks[i] = null; } for (int i = stop; i < end; i++) { blocks[i] = null; } bytesUsed.AddAndGet(-(end - stop) * m_blockSize); Debug.Assert(bytesUsed.Get() >= 0); }
public override void RecycleByteBlocks(sbyte[][] blocks, int start, int end) { int numBlocks = Math.Min(MaxBufferedBlocks_Renamed - FreeBlocks_Renamed, end - start); int size = FreeBlocks_Renamed + numBlocks; if (size >= FreeByteBlocks.Length) { sbyte[][] newBlocks = new sbyte[ArrayUtil.Oversize(size, RamUsageEstimator.NUM_BYTES_OBJECT_REF)][]; Array.Copy(FreeByteBlocks, 0, newBlocks, 0, FreeBlocks_Renamed); FreeByteBlocks = newBlocks; } int stop = start + numBlocks; for (int i = start; i < stop; i++) { FreeByteBlocks[FreeBlocks_Renamed++] = blocks[i]; blocks[i] = null; } for (int i = stop; i < end; i++) { blocks[i] = null; } BytesUsed_Renamed.AddAndGet(-(end - stop) * BlockSize); Debug.Assert(BytesUsed_Renamed.Get() >= 0); }
public virtual void TestReadAndWrite() { Counter bytesUsed = Util.Counter.NewCounter(); ByteBlockPool pool = new ByteBlockPool(new ByteBlockPool.DirectTrackingAllocator(bytesUsed)); pool.NextBuffer(); bool reuseFirst = Random.NextBoolean(); for (int j = 0; j < 2; j++) { IList <BytesRef> list = new List <BytesRef>(); int maxLength = AtLeast(500); int numValues = AtLeast(100); BytesRef @ref = new BytesRef(); for (int i = 0; i < numValues; i++) { string value = TestUtil.RandomRealisticUnicodeString(Random, maxLength); list.Add(new BytesRef(value)); @ref.CopyChars(value); pool.Append(@ref); } // verify long position = 0; foreach (BytesRef expected in list) { @ref.Grow(expected.Length); @ref.Length = expected.Length; pool.ReadBytes(position, @ref.Bytes, @ref.Offset, @ref.Length); Assert.AreEqual(expected, @ref); position += @ref.Length; } pool.Reset(Random.NextBoolean(), reuseFirst); if (reuseFirst) { Assert.AreEqual(ByteBlockPool.BYTE_BLOCK_SIZE, bytesUsed.Get()); } else { Assert.AreEqual(0, bytesUsed.Get()); pool.NextBuffer(); // prepare for next iter } } }
/// <summary> /// Read in a single partition of data </summary> internal int ReadPartition(ByteSequencesReader reader) { long start = Environment.TickCount; var scratch = new BytesRef(); while ((scratch.Bytes = reader.Read()) != null) { scratch.Length = scratch.Bytes.Length; Buffer.Append(scratch); // Account for the created objects. // (buffer slots do not account to buffer size.) if (RamBufferSize.Bytes < BufferBytesUsed.Get()) { break; } } sortInfo.ReadTime += (Environment.TickCount - start); return(Buffer.Size()); }