Skip to content

Commit 266fe62

Browse files
committed
Reduce allocations when executing a command.
1 parent f3508e6 commit 266fe62

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public ServerSession(ConnectionPool? pool, int poolGeneration, int id)
4545
Pool = pool;
4646
PoolGeneration = poolGeneration;
4747
HostName = "";
48+
SingleCommandList = new IMySqlCommand[1];
4849
m_logArguments = new object?[] { "{0}".FormatInvariant(Id), null };
4950
Log.Debug("Session{0} created new session", m_logArguments);
5051
}
@@ -65,6 +66,7 @@ public ServerSession(ConnectionPool? pool, int poolGeneration, int id)
6566
public bool SupportsDeprecateEof => m_supportsDeprecateEof;
6667
public bool SupportsSessionTrack => m_supportsSessionTrack;
6768
public bool ProcAccessDenied { get; set; }
69+
public IMySqlCommand[] SingleCommandList { get; }
6870

6971
public void ReturnToPool()
7072
{

src/MySqlConnector/MySql.Data.MySqlClient/MySqlCommand.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,17 @@ internal async Task<DbDataReader> ExecuteReaderAsync(CommandBehavior behavior, I
267267
throw exception;
268268

269269
m_commandBehavior = behavior;
270-
return await CommandExecutor.ExecuteReaderAsync(new IMySqlCommand[] { this }, SingleCommandPayloadCreator.Instance, behavior, ioBehavior, cancellationToken).ConfigureAwait(false);
270+
var commandList = m_connection!.Session.SingleCommandList;
271+
var oldCommand = commandList[0];
272+
try
273+
{
274+
commandList[0] = this;
275+
return await CommandExecutor.ExecuteReaderAsync(commandList, SingleCommandPayloadCreator.Instance, behavior, ioBehavior, cancellationToken).ConfigureAwait(false);
276+
}
277+
finally
278+
{
279+
commandList[0] = oldCommand;
280+
}
271281
}
272282

273283
public MySqlCommand Clone() => new MySqlCommand(this);

0 commit comments

Comments
 (0)