-
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.
Removed allocation of async state machine
- Loading branch information
Showing
2 changed files
with
163 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Threading.Tasks.Sources; | ||
|
||
namespace DotNext.IO; | ||
|
||
using Intrinsics = Runtime.Intrinsics; | ||
|
||
public partial class FileReader : IDynamicInterfaceCastable | ||
{ | ||
private readonly Action readCallback, readDirectCallback; | ||
private ManualResetValueTaskSourceCore<int> source; | ||
private ConfiguredValueTaskAwaitable<int>.ConfiguredValueTaskAwaiter awaiter; | ||
private int extraCount; | ||
|
||
private int GetAsyncResult(short token) | ||
{ | ||
try | ||
{ | ||
return source.GetResult(token); | ||
} | ||
finally | ||
{ | ||
source.Reset(); | ||
} | ||
} | ||
|
||
private void OnRead() | ||
{ | ||
var awaiter = this.awaiter; | ||
this.awaiter = default; | ||
|
||
int count; | ||
try | ||
{ | ||
count = awaiter.GetResult(); | ||
|
||
bufferEnd += count; | ||
} | ||
catch (Exception e) | ||
{ | ||
source.SetException(e); | ||
return; | ||
} | ||
|
||
source.SetResult(count); | ||
} | ||
|
||
private void OnReadDirect() | ||
{ | ||
var awaiter = this.awaiter; | ||
this.awaiter = default; | ||
|
||
var extraCount = this.extraCount; | ||
this.extraCount = 0; | ||
|
||
int count; | ||
try | ||
{ | ||
count = awaiter.GetResult(); | ||
|
||
fileOffset += count; | ||
count += extraCount; | ||
} | ||
catch (Exception e) | ||
{ | ||
source.SetException(e); | ||
return; | ||
} | ||
|
||
source.SetResult(count); | ||
} | ||
|
||
private ValueTask<int> SubmitAsInt32(ValueTask<int> task, Action callback) | ||
{ | ||
awaiter = task.ConfigureAwait(false).GetAwaiter(); | ||
if (awaiter.IsCompleted) | ||
{ | ||
callback(); | ||
} | ||
else | ||
{ | ||
awaiter.UnsafeOnCompleted(callback); | ||
} | ||
|
||
return new((IValueTaskSource<int>)this, source.Version); | ||
} | ||
|
||
private ValueTask<bool> SubmitAsBoolean(ValueTask<int> task, Action callback) | ||
{ | ||
awaiter = task.ConfigureAwait(false).GetAwaiter(); | ||
if (awaiter.IsCompleted) | ||
{ | ||
callback(); | ||
} | ||
else | ||
{ | ||
awaiter.UnsafeOnCompleted(callback); | ||
} | ||
|
||
return new((IValueTaskSource<bool>)this, source.Version); | ||
} | ||
|
||
[DynamicInterfaceCastableImplementation] | ||
private interface IProxyValueTaskSource : IValueTaskSource<int>, IValueTaskSource<bool> | ||
{ | ||
ValueTaskSourceStatus IValueTaskSource<int>.GetStatus(short token) | ||
=> Unsafe.As<FileReader>(this).source.GetStatus(token); | ||
|
||
ValueTaskSourceStatus IValueTaskSource<bool>.GetStatus(short token) | ||
=> Unsafe.As<FileReader>(this).source.GetStatus(token); | ||
|
||
int IValueTaskSource<int>.GetResult(short token) | ||
=> Unsafe.As<FileReader>(this).GetAsyncResult(token); | ||
|
||
bool IValueTaskSource<bool>.GetResult(short token) | ||
=> Unsafe.As<FileReader>(this).GetAsyncResult(token) is not 0; | ||
|
||
void IValueTaskSource<int>.OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) | ||
=> Unsafe.As<FileReader>(this).source.OnCompleted(continuation, state, token, flags); | ||
|
||
void IValueTaskSource<bool>.OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) | ||
=> Unsafe.As<FileReader>(this).source.OnCompleted(continuation, state, token, flags); | ||
} | ||
|
||
[ExcludeFromCodeCoverage] | ||
bool IDynamicInterfaceCastable.IsInterfaceImplemented(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented) | ||
{ | ||
if (interfaceType.IsOneOf([Intrinsics.TypeOf<IValueTaskSource<int>>(), Intrinsics.TypeOf<IValueTaskSource<bool>>()])) | ||
return true; | ||
|
||
return throwIfNotImplemented ? throw new InvalidCastException() : false; | ||
} | ||
|
||
[ExcludeFromCodeCoverage] | ||
RuntimeTypeHandle IDynamicInterfaceCastable.GetInterfaceImplementation(RuntimeTypeHandle interfaceType) | ||
{ | ||
if (interfaceType.IsOneOf([Intrinsics.TypeOf<IValueTaskSource<int>>(), Intrinsics.TypeOf<IValueTaskSource<bool>>()])) | ||
return Intrinsics.TypeOf<IProxyValueTaskSource>(); | ||
|
||
throw new InvalidCastException(); | ||
} | ||
} |
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