Skip to content

Commit

Permalink
Elide async and await in pass-through asynchronous methods (#2053)
Browse files Browse the repository at this point in the history
* Elide async and await in JsonWriter asynchronous methods where applicable

* Elide async and await in NonIndentedTextWriter and its base class

Co-authored-by: John Gathogo <[email protected]>
  • Loading branch information
gathogojr and John Gathogo authored Apr 21, 2021
1 parent 78e3181 commit 1caecaf
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 79 deletions.
65 changes: 32 additions & 33 deletions src/Microsoft.OData.Core/Json/JsonValueUtilsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,44 +52,44 @@ internal static async Task WriteValueAsync(this TextWriter writer, char value, O
/// </summary>
/// <param name="writer">The text writer to write the output to.</param>
/// <param name="value">The boolean value to write.</param>
internal static async Task WriteValueAsync(this TextWriter writer, bool value)
internal static Task WriteValueAsync(this TextWriter writer, bool value)
{
Debug.Assert(writer != null, "writer != null");

await writer.WriteAsync(FormatAsBooleanLiteral(value)).ConfigureAwait(false);
return writer.WriteAsync(FormatAsBooleanLiteral(value));
}

/// <summary>
/// Write an integer value.
/// </summary>
/// <param name="writer">The text writer to write the output to.</param>
/// <param name="value">Integer value to be written.</param>
internal static async Task WriteValueAsync(this TextWriter writer, int value)
internal static Task WriteValueAsync(this TextWriter writer, int value)
{
Debug.Assert(writer != null, "writer != null");

await writer.WriteAsync(value.ToString(CultureInfo.InvariantCulture)).ConfigureAwait(false);
return writer.WriteAsync(value.ToString(CultureInfo.InvariantCulture));
}

/// <summary>
/// Write a float value.
/// </summary>
/// <param name="writer">The text writer to write the output to.</param>
/// <param name="value">Float value to be written.</param>
internal static async Task WriteValueAsync(this TextWriter writer, float value)
internal static Task WriteValueAsync(this TextWriter writer, float value)
{
Debug.Assert(writer != null, "writer != null");

if (JsonSharedUtils.IsFloatValueSerializedAsString(value))
{
await writer.WriteQuotedAsync(value.ToString(ODataNumberFormatInfo)).ConfigureAwait(false);
return writer.WriteQuotedAsync(value.ToString(ODataNumberFormatInfo));
}
else
{
// float.ToString() supports a max scale of six,
// whereas float.MinValue and float.MaxValue have 8 digits scale. Hence we need
// to use XmlConvert in all other cases, except infinity
await writer.WriteAsync(XmlConvert.ToString(value)).ConfigureAwait(false);
return writer.WriteAsync(XmlConvert.ToString(value));
}
}

Expand All @@ -98,23 +98,23 @@ internal static async Task WriteValueAsync(this TextWriter writer, float value)
/// </summary>
/// <param name="writer">The text writer to write the output to.</param>
/// <param name="value">Short value to be written.</param>
internal static async Task WriteValueAsync(this TextWriter writer, short value)
internal static Task WriteValueAsync(this TextWriter writer, short value)
{
Debug.Assert(writer != null, "writer != null");

await writer.WriteAsync(value.ToString(CultureInfo.InvariantCulture)).ConfigureAwait(false);
return writer.WriteAsync(value.ToString(CultureInfo.InvariantCulture));
}

/// <summary>
/// Write a long value.
/// </summary>
/// <param name="writer">The text writer to write the output to.</param>
/// <param name="value">Long value to be written.</param>
internal static async Task WriteValueAsync(this TextWriter writer, long value)
internal static Task WriteValueAsync(this TextWriter writer, long value)
{
Debug.Assert(writer != null, "writer != null");

await writer.WriteAsync(value.ToString(CultureInfo.InvariantCulture)).ConfigureAwait(false);
return writer.WriteAsync(value.ToString(CultureInfo.InvariantCulture));
}

/// <summary>
Expand Down Expand Up @@ -150,23 +150,23 @@ internal static async Task WriteValueAsync(this TextWriter writer, double value)
/// </summary>
/// <param name="writer">The text writer to write the output to.</param>
/// <param name="value">Guid value to be written.</param>
internal static async Task WriteValueAsync(this TextWriter writer, Guid value)
internal static Task WriteValueAsync(this TextWriter writer, Guid value)
{
Debug.Assert(writer != null, "writer != null");

await writer.WriteQuotedAsync(value.ToString()).ConfigureAwait(false);
return writer.WriteQuotedAsync(value.ToString());
}

/// <summary>
/// Write a decimal value
/// </summary>
/// <param name="writer">The text writer to write the output to.</param>
/// <param name="value">Decimal value to be written.</param>
internal static async Task WriteValueAsync(this TextWriter writer, decimal value)
internal static Task WriteValueAsync(this TextWriter writer, decimal value)
{
Debug.Assert(writer != null, "writer != null");

await writer.WriteAsync(value.ToString(CultureInfo.InvariantCulture)).ConfigureAwait(false);
return writer.WriteAsync(value.ToString(CultureInfo.InvariantCulture));
}

/// <summary>
Expand Down Expand Up @@ -222,59 +222,59 @@ internal static async Task WriteValueAsync(this TextWriter writer, DateTimeOffse
/// </summary>
/// <param name="writer">The text writer to write the output to.</param>
/// <param name="value">TimeSpan value to be written.</param>
internal static async Task WriteValueAsync(this TextWriter writer, TimeSpan value)
internal static Task WriteValueAsync(this TextWriter writer, TimeSpan value)
{
Debug.Assert(writer != null, "writer != null");

await writer.WriteQuotedAsync(EdmValueWriter.DurationAsXml(value)).ConfigureAwait(false);
return writer.WriteQuotedAsync(EdmValueWriter.DurationAsXml(value));
}

/// <summary>
/// Write a TimeOfDay value
/// </summary>
/// <param name="writer">The text writer to write the output to.</param>
/// <param name="value">TimeOfDay value to be written.</param>
internal static async Task WriteValueAsync(this TextWriter writer, TimeOfDay value)
internal static Task WriteValueAsync(this TextWriter writer, TimeOfDay value)
{
Debug.Assert(writer != null, "writer != null");

await writer.WriteQuotedAsync(value.ToString()).ConfigureAwait(false);
return writer.WriteQuotedAsync(value.ToString());
}

/// <summary>
/// Write a Date value
/// </summary>
/// <param name="writer">The text writer to write the output to.</param>
/// <param name="value">Date value to be written.</param>
internal static async Task WriteValueAsync(this TextWriter writer, Date value)
internal static Task WriteValueAsync(this TextWriter writer, Date value)
{
Debug.Assert(writer != null, "writer != null");

await writer.WriteQuotedAsync(value.ToString()).ConfigureAwait(false);
return writer.WriteQuotedAsync(value.ToString());
}

/// <summary>
/// Write a byte value.
/// </summary>
/// <param name="writer">The text writer to write the output to.</param>
/// <param name="value">Byte value to be written.</param>
internal static async Task WriteValueAsync(this TextWriter writer, byte value)
internal static Task WriteValueAsync(this TextWriter writer, byte value)
{
Debug.Assert(writer != null, "writer != null");

await writer.WriteAsync(value.ToString(CultureInfo.InvariantCulture)).ConfigureAwait(false);
return writer.WriteAsync(value.ToString(CultureInfo.InvariantCulture));
}

/// <summary>
/// Write an sbyte value.
/// </summary>
/// <param name="writer">The text writer to write the output to.</param>
/// <param name="value">SByte value to be written.</param>
internal static async Task WriteValueAsync(this TextWriter writer, sbyte value)
internal static Task WriteValueAsync(this TextWriter writer, sbyte value)
{
Debug.Assert(writer != null, "writer != null");

await writer.WriteAsync(value.ToString(CultureInfo.InvariantCulture)).ConfigureAwait(false);
return writer.WriteAsync(value.ToString(CultureInfo.InvariantCulture));
}

/// <summary>
Expand All @@ -285,7 +285,7 @@ internal static async Task WriteValueAsync(this TextWriter writer, sbyte value)
/// <param name="stringEscapeOption">The string escape option.</param>
/// <param name="buffer">Char buffer to use for streaming data.</param>
/// <param name="arrayPool">Array pool for renting a buffer.</param>
internal static async Task WriteValueAsync(
internal static Task WriteValueAsync(
this TextWriter writer,
string value,
ODataStringEscapeOption stringEscapeOption,
Expand All @@ -296,11 +296,11 @@ internal static async Task WriteValueAsync(

if (value == null)
{
await writer.WriteAsync(JsonConstants.JsonNullLiteral).ConfigureAwait(false);
return writer.WriteAsync(JsonConstants.JsonNullLiteral);
}
else
{
await writer.WriteEscapedJsonStringAsync(value, stringEscapeOption, buffer, arrayPool).ConfigureAwait(false);
return writer.WriteEscapedJsonStringAsync(value, stringEscapeOption, buffer, arrayPool);
}
}

Expand Down Expand Up @@ -477,11 +477,10 @@ internal static async Task WriteEscapedCharArrayAsync(
/// </summary>
/// <param name="writer">The text writer to write the output to.</param>
/// <param name="text">String value to be written.</param>
private static async Task WriteQuotedAsync(this TextWriter writer, string text)
private static Task WriteQuotedAsync(this TextWriter writer, string text)
{
await writer.WriteAsync(
string.Concat(JsonConstants.QuoteCharacter, text, JsonConstants.QuoteCharacter))
.ConfigureAwait(false);
return writer.WriteAsync(
string.Concat(JsonConstants.QuoteCharacter, text, JsonConstants.QuoteCharacter));
}
}
}
}
27 changes: 14 additions & 13 deletions src/Microsoft.OData.Core/Json/JsonWriterAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ namespace Microsoft.OData.Json
internal sealed partial class JsonWriter
{
/// <inheritdoc/>
public async Task StartPaddingFunctionScopeAsync()
public Task StartPaddingFunctionScopeAsync()
{
Debug.Assert(this.scopes.Count == 0, "Padding scope can only be the outer most scope.");
await this.StartScopeAsync(ScopeType.Padding).ConfigureAwait(false);
return this.StartScopeAsync(ScopeType.Padding);
}

/// <inheritdoc/>
Expand All @@ -46,9 +46,9 @@ public async Task EndPaddingFunctionScopeAsync()
}

/// <inheritdoc/>
public async Task StartObjectScopeAsync()
public Task StartObjectScopeAsync()
{
await this.StartScopeAsync(ScopeType.Object).ConfigureAwait(false);
return this.StartScopeAsync(ScopeType.Object);
}

/// <inheritdoc/>
Expand All @@ -66,9 +66,9 @@ public async Task EndObjectScopeAsync()
}

/// <inheritdoc/>
public async Task StartArrayScopeAsync()
public Task StartArrayScopeAsync()
{
await this.StartScopeAsync(ScopeType.Array).ConfigureAwait(false);
return this.StartScopeAsync(ScopeType.Array);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -106,9 +106,9 @@ await this.writer.WriteEscapedJsonStringAsync(name, this.stringEscapeOption, thi
}

/// <inheritdoc/>
public async Task WritePaddingFunctionNameAsync(string functionName)
public Task WritePaddingFunctionNameAsync(string functionName)
{
await this.writer.WriteAsync(functionName).ConfigureAwait(false);
return this.writer.WriteAsync(functionName);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -253,9 +253,9 @@ public async Task WriteRawValueAsync(string rawValue)
}

/// <inheritdoc/>
public async Task FlushAsync()
public Task FlushAsync()
{
await this.writer.FlushAsync().ConfigureAwait(false);
return this.writer.FlushAsync();
}

/// <inheritdoc/>
Expand Down Expand Up @@ -303,13 +303,14 @@ await this.writer.WriteAsync(JsonConstants.QuoteCharacter)
}

/// <inheritdoc/>
public async Task EndTextWriterValueScopeAsync()
public Task EndTextWriterValueScopeAsync()
{
if (!IsWritingJson)
{
await this.writer.WriteAsync(JsonConstants.QuoteCharacter)
.ConfigureAwait(false);
return this.writer.WriteAsync(JsonConstants.QuoteCharacter);
}

return TaskUtils.CompletedTask;
}

/// <summary>
Expand Down
13 changes: 6 additions & 7 deletions src/Microsoft.OData.Core/Json/NonIndentedTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public override void Write(string s)
/// </summary>
/// <param name="s">String value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
public override async Task WriteAsync(string s)
public override Task WriteAsync(string s)
{
await this.writer.WriteAsync(s).ConfigureAwait(false);
return this.writer.WriteAsync(s);
}

/// <summary>
Expand All @@ -57,9 +57,9 @@ public override void Write(char value)
/// </summary>
/// <param name="value">Char value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
public override async Task WriteAsync(char value)
public override Task WriteAsync(char value)
{
await this.writer.WriteAsync(value).ConfigureAwait(false);
return this.writer.WriteAsync(value);
}

/// <summary>
Expand All @@ -73,10 +73,9 @@ public override void WriteLine()
/// Writes a new line asynchronously.
/// </summary>
/// <returns>A task that represents the asynchronous write operation.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "ConfigureAwait has no effect on already completed task.")]
public override async Task WriteLineAsync()
public override Task WriteLineAsync()
{
await TaskUtils.CompletedTask;
return TaskUtils.CompletedTask;
}
}
}
Loading

0 comments on commit 1caecaf

Please sign in to comment.