This file contains hidden or 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
public static class FileComparer | |
{ | |
public static bool CompareEquals(string filePath1, string filePath2, int bufferSize = 65536) | |
{ | |
if (filePath1 == filePath2) return true; | |
var buffer1 = ArrayPool<byte>.Shared.Rent(bufferSize); | |
var buffer2 = ArrayPool<byte>.Shared.Rent(bufferSize); | |
try | |
{ |
This file contains hidden or 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
// You can use full feature of Generic Host(same as ASP.NET Core). | |
var builder = ConsoleApp.CreateBuilder(args); | |
builder.ConfigureServices((ctx,services) => | |
{ | |
// Register EntityFramework database context | |
services.AddDbContext<MyDbContext>(); | |
// Register appconfig.json to IOption<MyConfig> | |
services.Configure<MyConfig>(ctx.Configuration); |
This file contains hidden or 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
public static async Task ReadFromAsync(NativeMemoryArray<byte> buffer, Stream stream, CancellationToken cancellationToken = default) | |
{ | |
var writer = buffer.CreateBufferWriter(); | |
int read; | |
while ((read = await stream.ReadAsync(writer.GetMemory(), cancellationToken).ConfigureAwait(false)) != 0) | |
{ | |
writer.Advance(read); | |
} | |
} |
This file contains hidden or 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
// for example, load large file. | |
using var handle = File.OpenHandle("4GBfile.bin", FileMode.Open, FileAccess.Read, options: FileOptions.Asynchronous); | |
var size = RandomAccess.GetLength(handle); | |
// via .NET 6 Scatter/Gather API | |
using var array = new NativeMemoryArray<byte>(size); | |
await RandomAccess.ReadAsync(handle, array.AsMemoryList(), 0); | |
// iterate Span<byte> as chunk | |
foreach (var chunk in array) |
This file contains hidden or 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
using System.Buffers; | |
public class LargeArray<T> : IBufferWriter<T> | |
{ | |
const int MaxArrayLength = 0X7FEFFFFF; // 0x7FFFFFC7; | |
readonly List<Memory<T>> completedChunks; | |
readonly long length; | |
readonly long chunkSize; |
This file contains hidden or 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
public class LargeBuffer | |
{ | |
const int MaxArrayLength = 0x7FFFFFC7; | |
readonly byte[][] chunk; | |
readonly long length; | |
int writtenInCurrent; | |
int chunkIndex; |
This file contains hidden or 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
public class SynchronizedEnumerator<T> : IEnumerator<T> | |
{ | |
bool isDisposed; | |
readonly object gate; | |
readonly bool lockTaken; | |
readonly IEnumerator<T> enumerator; | |
public SynchronizedEnumerator(object gate, IEnumerator<T> enumerator) | |
{ | |
this.gate = gate; |
This file contains hidden or 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
// similar as ObservableCollection <-> CollectionView | |
public delegate void NotifyCollectionChangedEventHandler<T>(in NotifyCollectionChangedEventArgs<T> e); | |
public interface INotifyCollectionChanged<T> | |
{ | |
event NotifyCollectionChangedEventHandler<T>? CollectionChanged; | |
ISynchronizedView<T, TView> CreateView<TView>(Func<T, TView> transform, bool reverse = false); | |
ISynchronizedView<T, TView> CreateSortedView<TView>(Func<T, TView> transform, IComparer<T> comparer); | |
IGroupedSynchronizedView<T, TKey, TView> CreateGroupedView<TKey, TView>(Func<T, TKey> keySelector, Func<T, TView> transform); |
This file contains hidden or 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
public interface IRequestHandler<in TRequest, out TResponse> | |
{ | |
TResponse Invoke(TRequest request); | |
} | |
| |
public interface IAsyncRequestHandler<in TRequest, TResponse> | |
{ | |
ValueTask<TResponse> InvokeAsync(TRequest request, CancellationToken cancellationToken = default); | |
} |
This file contains hidden or 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
// don't send same value(Rx's DistinctUntilChanged) by Filter | |
public class ChangedValueFilter<T> : MessageHandlerFilter<T> | |
{ | |
T lastValue; | |
| |
public override void Handle(T message, Action<T> next) | |
{ | |
if (EqualityComparer<T>.Default.Equals(message, lastValue)) | |
{ | |
return; |