-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
255 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/DotNext.Benchmarks/Collections/Concurrent/IndexPoolBenchmark.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Engines; | ||
using BenchmarkDotNet.Order; | ||
using System.Collections.Concurrent; | ||
|
||
namespace DotNext.Collections.Concurrent; | ||
|
||
[SimpleJob(runStrategy: RunStrategy.Throughput, launchCount: 1)] | ||
[Orderer(SummaryOrderPolicy.Declared)] | ||
public class IndexPoolBenchmark | ||
{ | ||
private readonly ConcurrentBag<object> bag = new(CreateObjects()); | ||
private readonly object[] objects = CreateObjects(); | ||
private IndexPool pool = new(); | ||
|
||
[Benchmark(Description = "IndexPool Take/Return", Baseline = true)] | ||
public int IndexPoolTakeReturn() | ||
{ | ||
pool.TryTake(out var index); | ||
var hashCode = objects[index].GetHashCode(); | ||
|
||
pool.Return(index); | ||
return hashCode; | ||
} | ||
|
||
[Benchmark(Description = "ConcurrentBag Take/Return")] | ||
public int ConcurrentBagTakeReturn() | ||
{ | ||
bag.TryTake(out var obj); | ||
var hashCode = obj.GetHashCode(); | ||
|
||
bag.Add(obj); | ||
return hashCode; | ||
} | ||
|
||
private static object[] CreateObjects() | ||
{ | ||
var result = new object[64]; | ||
Span.Initialize<object>(result); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System.Buffers; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace DotNext.IO; | ||
|
||
using static Buffers.Memory; | ||
|
||
internal sealed class SharedReadOnlyMemoryStream(ReadOnlySequence<byte> sequence) : ReadOnlyStream | ||
{ | ||
// don't use BoxedValue due to limitations of AsyncLocal | ||
private readonly AsyncLocal<StrongBox<SequencePosition>> position = new(); | ||
|
||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | ||
private SequencePosition LocalPosition | ||
{ | ||
get => position.Value?.Value ?? sequence.Start; | ||
set => (position.Value ??= new()).Value = value; | ||
} | ||
|
||
private ReadOnlySequence<byte> GetRemainingSequence(out SequencePosition start) | ||
=> sequence.Slice(start = LocalPosition); | ||
|
||
public override bool CanSeek => true; | ||
|
||
public override long Length => sequence.Length; | ||
|
||
public override long Position | ||
{ | ||
get => sequence.GetOffset(LocalPosition); | ||
set | ||
{ | ||
ArgumentOutOfRangeException.ThrowIfGreaterThan((ulong)value, (ulong)sequence.Length, nameof(value)); | ||
|
||
LocalPosition = sequence.GetPosition(value); | ||
} | ||
} | ||
|
||
public override async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken token) | ||
{ | ||
ValidateCopyToArguments(destination, bufferSize); | ||
|
||
foreach (var segment in GetRemainingSequence(out _)) | ||
await destination.WriteAsync(segment, token).ConfigureAwait(false); | ||
|
||
LocalPosition = sequence.End; | ||
} | ||
|
||
public override void CopyTo(Stream destination, int bufferSize) | ||
{ | ||
ValidateCopyToArguments(destination, bufferSize); | ||
|
||
foreach (var segment in GetRemainingSequence(out _)) | ||
destination.Write(segment.Span); | ||
|
||
LocalPosition = sequence.End; | ||
} | ||
|
||
public override void SetLength(long value) => throw new NotSupportedException(); | ||
|
||
public override int Read(Span<byte> buffer) | ||
{ | ||
GetRemainingSequence(out var startPos).CopyTo(buffer, out var writtenCount); | ||
LocalPosition = sequence.GetPosition(writtenCount, startPos); | ||
return writtenCount; | ||
} | ||
|
||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
var newPosition = origin switch | ||
{ | ||
SeekOrigin.Begin => offset, | ||
SeekOrigin.Current => sequence.GetOffset(LocalPosition) + offset, | ||
SeekOrigin.End => sequence.Length + offset, | ||
_ => throw new ArgumentOutOfRangeException(nameof(origin)) | ||
}; | ||
|
||
if (newPosition < 0L) | ||
throw new IOException(); | ||
|
||
ArgumentOutOfRangeException.ThrowIfGreaterThan(newPosition, sequence.Length, nameof(offset)); | ||
|
||
LocalPosition = sequence.GetPosition(newPosition); | ||
return newPosition; | ||
} | ||
|
||
public override string ToString() => sequence.ToString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.