Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor I/O read/write APIs to return ValueTask instead of Task #2975

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
6 changes: 3 additions & 3 deletions src/Microsoft.OData.Core/Batch/ODataBatchOperationMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,16 @@ public override Stream GetStream()
/// Asynchronously get the stream backing this message.
/// </summary>
/// <returns>The stream for this message.</returns>
public override Task<Stream> GetStreamAsync()
public override ValueTask<Stream> GetStreamAsync()
{
this.VerifyNotCompleted();

return GetStreamInnerAsync();

async Task<Stream> GetStreamInnerAsync()
async ValueTask<Stream> GetStreamInnerAsync()
{
// notify the listener that the stream has been requested
Task listenerTask = this.operationListener.StreamRequestedAsync();
ValueTask listenerTask = this.operationListener.StreamRequestedAsync();

// now remember that we are done processing the part header data (and only the payload is missing)
Stream contentStream = this.contentStreamCreatorFunc();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public Stream GetStream()

/// <summary>Asynchronously get the stream backing for this message.</summary>
/// <returns>The stream backing for this message.</returns>
public Task<Stream> GetStreamAsync()
public ValueTask<Stream> GetStreamAsync()
{
return this.message.GetStreamAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public Stream GetStream()

/// <summary>Asynchronously get the stream backing for this message.</summary>
/// <returns>The stream backing for this message.</returns>
public Task<Stream> GetStreamAsync()
public ValueTask<Stream> GetStreamAsync()
{
return this.message.GetStreamAsync();
}
Expand Down
44 changes: 22 additions & 22 deletions src/Microsoft.OData.Core/Batch/ODataBatchReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public bool Read()

/// <summary>Asynchronously reads the next part from the batch message payload.</summary>
/// <returns>A task that when completed indicates whether more items were read.</returns>
public Task<bool> ReadAsync()
public ValueTask<bool> ReadAsync()
{
this.VerifyCanRead(false);
return this.InterceptExceptionAsync((thisParam) => thisParam.ReadAsynchronously());
Expand All @@ -164,7 +164,7 @@ public ODataBatchOperationRequestMessage CreateOperationRequestMessage()

/// <summary>Asynchronously returns an <see cref="ODataBatchOperationRequestMessage" /> for reading the content of a batch operation.</summary>
/// <returns>A task that when completed returns a request message for reading the content of a batch operation.</returns>
public async Task<ODataBatchOperationRequestMessage> CreateOperationRequestMessageAsync()
public async ValueTask<ODataBatchOperationRequestMessage> CreateOperationRequestMessageAsync()
{
this.VerifyCanCreateOperationRequestMessage(synchronousCall: false);
ODataBatchOperationRequestMessage result = await this.InterceptExceptionAsync(
Expand All @@ -188,7 +188,7 @@ public ODataBatchOperationResponseMessage CreateOperationResponseMessage()

/// <summary>Asynchronously returns an <see cref="ODataBatchOperationResponseMessage" /> for reading the content of a batch operation.</summary>
/// <returns>A task that when completed returns a response message for reading the content of a batch operation.</returns>
public async Task<ODataBatchOperationResponseMessage> CreateOperationResponseMessageAsync()
public async ValueTask<ODataBatchOperationResponseMessage> CreateOperationResponseMessageAsync()
{
this.VerifyCanCreateOperationResponseMessage(synchronousCall: false);
ODataBatchOperationResponseMessage result = await this.InterceptExceptionAsync(
Expand All @@ -214,10 +214,10 @@ void IODataStreamListener.StreamRequested()
/// A task representing any action that is running as part of the status change of the reader;
/// null if no such action exists.
/// </returns>
Task IODataStreamListener.StreamRequestedAsync()
ValueTask IODataStreamListener.StreamRequestedAsync()
{
this.operationState = OperationState.StreamRequested;
return TaskUtils.CompletedTask;
return ValueTask.CompletedTask;
}

/// <summary>
Expand All @@ -232,10 +232,10 @@ void IODataStreamListener.StreamDisposed()
/// This method is called asynchronously to notify that the content stream of a batch operation has been disposed.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
Task IODataStreamListener.StreamDisposedAsync()
ValueTask IODataStreamListener.StreamDisposedAsync()
{
this.operationState = OperationState.StreamDisposed;
return TaskUtils.CompletedTask;
return ValueTask.CompletedTask;
}

/// <summary>
Expand Down Expand Up @@ -380,9 +380,9 @@ protected ODataBatchOperationResponseMessage BuildOperationResponseMessage(
/// The value of the TResult parameter contains an <see cref="ODataBatchOperationRequestMessage"/>
/// that can be used to read the content of the batch request operation from.
/// </returns>
protected virtual Task<ODataBatchOperationRequestMessage> CreateOperationRequestMessageImplementationAsync()
protected virtual ValueTask<ODataBatchOperationRequestMessage> CreateOperationRequestMessageImplementationAsync()
{
return TaskUtils.GetTaskForSynchronousOperation(this.CreateOperationRequestMessageImplementation);
return ValueTask.FromResult(this.CreateOperationRequestMessageImplementation());
}

/// <summary>
Expand All @@ -394,9 +394,9 @@ protected virtual Task<ODataBatchOperationRequestMessage> CreateOperationRequest
/// The value of the TResult parameter contains an <see cref="ODataBatchOperationResponseMessage"/>
/// that can be used to read the content of the batch response operation from.
/// </returns>
protected virtual Task<ODataBatchOperationResponseMessage> CreateOperationResponseMessageImplementationAsync()
protected virtual ValueTask<ODataBatchOperationResponseMessage> CreateOperationResponseMessageImplementationAsync()
{
return TaskUtils.GetTaskForSynchronousOperation(this.CreateOperationResponseMessageImplementation);
return ValueTask.FromResult(this.CreateOperationResponseMessageImplementation());
}

/// <summary>
Expand All @@ -406,9 +406,9 @@ protected virtual Task<ODataBatchOperationResponseMessage> CreateOperationRespon
/// A task that represents the asynchronous read operation.
/// The value of the TResult parameter contains the batch reader state after the read.
/// </returns>
protected virtual Task<ODataBatchReaderState> ReadAtStartImplementationAsync()
protected virtual ValueTask<ODataBatchReaderState> ReadAtStartImplementationAsync()
{
return TaskUtils.GetTaskForSynchronousOperation(this.ReadAtStartImplementation);
return ValueTask.FromResult(this.ReadAtStartImplementation());
}

/// <summary>
Expand All @@ -418,9 +418,9 @@ protected virtual Task<ODataBatchReaderState> ReadAtStartImplementationAsync()
/// A task that represents the asynchronous read operation.
/// The value of the TResult parameter contains the batch reader state after the read.
/// </returns>
protected virtual Task<ODataBatchReaderState> ReadAtOperationImplementationAsync()
protected virtual ValueTask<ODataBatchReaderState> ReadAtOperationImplementationAsync()
{
return TaskUtils.GetTaskForSynchronousOperation(this.ReadAtOperationImplementation);
return ValueTask.FromResult(this.ReadAtOperationImplementation());
}

/// <summary>
Expand All @@ -430,9 +430,9 @@ protected virtual Task<ODataBatchReaderState> ReadAtOperationImplementationAsync
/// A task that represents the asynchronous read operation.
/// The value of the TResult parameter contains the batch reader state after the read.
/// </returns>
protected virtual Task<ODataBatchReaderState> ReadAtChangesetStartImplementationAsync()
protected virtual ValueTask<ODataBatchReaderState> ReadAtChangesetStartImplementationAsync()
{
return TaskUtils.GetTaskForSynchronousOperation(this.ReadAtChangesetStartImplementation);
return ValueTask.FromResult(this.ReadAtChangesetStartImplementation());
}

/// <summary>
Expand All @@ -442,9 +442,9 @@ protected virtual Task<ODataBatchReaderState> ReadAtChangesetStartImplementation
/// A task that represents the asynchronous read operation.
/// The value of the TResult parameter contains the batch reader state after the read.
/// </returns>
protected virtual Task<ODataBatchReaderState> ReadAtChangesetEndImplementationAsync()
protected virtual ValueTask<ODataBatchReaderState> ReadAtChangesetEndImplementationAsync()
{
return TaskUtils.GetTaskForSynchronousOperation(this.ReadAtChangesetEndImplementation);
return ValueTask.FromResult(this.ReadAtChangesetEndImplementation());
}

/// <summary>
Expand Down Expand Up @@ -495,7 +495,7 @@ private bool ReadSynchronously()
/// </summary>
/// <returns>A task that when completed indicates whether more information was read.</returns>
[SuppressMessage("Microsoft.MSInternal", "CA908:AvoidTypesThatRequireJitCompilationInPrecompiledAssemblies", Justification = "API design calls for a bool being returned from the task here.")]
private Task<bool> ReadAsynchronously()
private ValueTask<bool> ReadAsynchronously()
{
return this.ReadImplementationAsync();
}
Expand Down Expand Up @@ -614,7 +614,7 @@ private bool ReadImplementation()
/// A task that represents the asynchronous read operation.
/// The value of the TResult parameter contains true if more items were read; otherwise false.
/// </returns>
private async Task<bool> ReadImplementationAsync()
private async ValueTask<bool> ReadImplementationAsync()
{
Debug.Assert(this.ReaderOperationState != OperationState.StreamRequested, "Should have verified that no operation stream is still active.");

Expand Down Expand Up @@ -856,7 +856,7 @@ private T InterceptException<T>(Func<ODataBatchReader, T> action)
/// A task that represents the asynchronous operation.
/// The value of the TResult parameter contains the result of executing the <paramref name="action"/>.
/// </returns>
private async Task<TResult> InterceptExceptionAsync<TResult>(Func<ODataBatchReader, Task<TResult>> action)
private async ValueTask<TResult> InterceptExceptionAsync<TResult>(Func<ODataBatchReader, ValueTask<TResult>> action)
{
try
{
Expand Down
Loading
Loading