Skip to content

Commit

Permalink
Avoid Task allocation on sync path.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Aug 13, 2023
1 parent 7c4f727 commit 845461c
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/MySqlConnector/MySqlCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,22 @@ private MySqlCommand(MySqlCommand other)
/// <inheritdoc/>
public override void Cancel() => Connection?.Cancel(this, m_commandId, true);

#pragma warning disable CA2012 // OK to read .Result because the ValueTask is completed
/// <summary>
/// Executes this command on the associated <see cref="MySqlConnection"/>.
/// </summary>
/// <returns>The number of rows affected.</returns>
/// <remarks>For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.
/// For stored procedures, the return value is the number of rows affected by the last statement in the stored procedure,
/// or zero if the last statement is a SELECT. For all other types of statements, the return value is -1.</remarks>
public override int ExecuteNonQuery() => ExecuteNonQueryAsync(IOBehavior.Synchronous, CancellationToken.None).GetAwaiter().GetResult();
public override int ExecuteNonQuery() => ExecuteNonQueryAsync(IOBehavior.Synchronous, CancellationToken.None).Result;

/// <inheritdoc/>
public override object? ExecuteScalar() => ExecuteScalarAsync(IOBehavior.Synchronous, CancellationToken.None).GetAwaiter().GetResult();
public override object? ExecuteScalar() => ExecuteScalarAsync(IOBehavior.Synchronous, CancellationToken.None).Result;

#pragma warning disable CA2012 // OK to read .Result because the ValueTask is completed
public new MySqlDataReader ExecuteReader() => ExecuteReaderAsync(default, IOBehavior.Synchronous, default).Result;

public new MySqlDataReader ExecuteReader(CommandBehavior commandBehavior) => ExecuteReaderAsync(commandBehavior, IOBehavior.Synchronous, default).GetAwaiter().GetResult();
#pragma warning restore CA2012
#pragma warning restore CA2012 // OK to read .Result because the ValueTask is completed

/// <inheritdoc/>
public override void Prepare()
Expand Down Expand Up @@ -294,9 +293,9 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior) =>
/// For stored procedures, the return value is the number of rows affected by the last statement in the stored procedure,
/// or zero if the last statement is a SELECT. For all other types of statements, the return value is -1.</remarks>
public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken) =>
ExecuteNonQueryAsync(AsyncIOBehavior, cancellationToken);
ExecuteNonQueryAsync(AsyncIOBehavior, cancellationToken).AsTask();

internal async Task<int> ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
internal async ValueTask<int> ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
{
Volatile.Write(ref m_commandTimedOut, false);
this.ResetCommandTimeout();
Expand All @@ -312,9 +311,9 @@ internal async Task<int> ExecuteNonQueryAsync(IOBehavior ioBehavior, Cancellatio
}

public override Task<object?> ExecuteScalarAsync(CancellationToken cancellationToken) =>
ExecuteScalarAsync(AsyncIOBehavior, cancellationToken);
ExecuteScalarAsync(AsyncIOBehavior, cancellationToken).AsTask();

internal async Task<object?> ExecuteScalarAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
internal async ValueTask<object?> ExecuteScalarAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
{
Volatile.Write(ref m_commandTimedOut, false);
this.ResetCommandTimeout();
Expand Down

0 comments on commit 845461c

Please sign in to comment.