private static void Byte_Memory_PosPos(int bufSize, int bufOffset) { var manager = new CustomMemoryForTest <byte>(new byte[bufSize], bufOffset, bufSize - 2 * bufOffset); var buffer = new ReadOnlySequence <byte>(manager.Memory); long offset = buffer.Length / 20; foreach (BenchmarkIteration iteration in Benchmark.Iterations) { int localInt = 0; using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { ReadOnlySequence <byte> sliced = buffer; long sliceLen = sliced.Length; SequencePosition start = sliced.GetPosition(0); SequencePosition end = sliced.GetPosition(0, sliced.End); while (sliceLen > 0) { sliceLen -= 2 * offset; start = new SequencePosition(start.GetObject(), start.GetInteger() + (int)offset); end = new SequencePosition(end.GetObject(), end.GetInteger() - (int)offset); sliced = sliced.Slice(start, end); localInt ^= sliced.Start.GetInteger(); } } } _volatileInt = localInt; } }
public void OwnedBufferDisposedAfterFinalizerGCKeepAliveTest() { MemoryManager <byte> owned = new CustomMemoryForTest <byte>(new byte[1024]); var buffer = owned.Memory; var slice = buffer.Slice(1); var span = buffer.Span; var sliceSpan = slice.Span; span[5] = 42; sliceSpan[10] = 24; GC.Collect(2); GC.WaitForPendingFinalizers(); try { span = buffer.Span; sliceSpan = slice.Span; } catch (ObjectDisposedException ex) { Assert.True(false, "There shouldn't be any Object Disposed Exception here. " + ex.Message); } Assert.Equal(42, span[5]); Assert.Equal(24, sliceSpan[10]); }
private static void Byte_Memory_Long(int bufSize, int bufOffset) { var manager = new CustomMemoryForTest <byte>(new byte[bufSize], bufOffset, bufSize - 2 * bufOffset); var buffer = new ReadOnlySequence <byte>(manager.Memory); long offset = buffer.Length / 10; foreach (BenchmarkIteration iteration in Benchmark.Iterations) { int localInt = 0; using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { ReadOnlySequence <byte> sliced = buffer; long sliceLen = sliced.Length; while (sliceLen > 0) { sliceLen -= offset; sliced = sliced.Slice(offset); localInt ^= sliced.Start.GetInteger(); } } } _volatileInt = localInt; } }
private static void Byte_Memory(int bufSize, int bufOffset) { var manager = new CustomMemoryForTest <byte>(new byte[bufSize], bufOffset, bufSize - 2 * bufOffset); var buffer = new ReadOnlySequence <byte>(manager.Memory); foreach (BenchmarkIteration iteration in Benchmark.Iterations) { int localInt = 0; using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { ReadOnlyMemory <byte> first = buffer.First; localInt ^= first.Length; } } _volatileInt = localInt; } }
private static void Byte_Memory_Pos(int bufSize, int bufOffset) { var manager = new CustomMemoryForTest <byte>(new byte[bufSize], bufOffset, bufSize - 2 * bufOffset); var buffer = new ReadOnlySequence <byte>(manager.Memory); SequencePosition start = buffer.GetPosition(0); foreach (BenchmarkIteration iteration in Benchmark.Iterations) { int localInt = 0; using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { ReadOnlySequence <byte> temp = buffer.Slice(start); localInt ^= temp.Start.GetInteger(); } } _volatileInt = localInt; } }
private static void Char_Memory(int bufSize, int bufOffset) { var manager = new CustomMemoryForTest <char>(new char[bufSize], bufOffset, bufSize - 2 * bufOffset); var buffer = new ReadOnlySequence <char>(manager.Memory); foreach (BenchmarkIteration iteration in Benchmark.Iterations) { int localInt = 0; using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { foreach (ReadOnlyMemory <char> memory in buffer) { localInt ^= memory.Length; } } } _volatileInt = localInt; } }
private static void Byte_Memory(int bufSize, int bufOffset) { var manager = new CustomMemoryForTest <byte>(new byte[bufSize], bufOffset, bufSize - 2 * bufOffset); var buffer = new ReadOnlySequence <byte>(manager.Memory); foreach (BenchmarkIteration iteration in Benchmark.Iterations) { int localInt = 0; using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { SequencePosition p = buffer.Start; while (buffer.TryGet(ref p, out ReadOnlyMemory <byte> memory)) { localInt ^= memory.Length; } } } _volatileInt = localInt; } }