Skip to content

Commit aebf174

Browse files
committed
NH-3857 - Improve performance of MySqlClientSqlCommandSet
Move reflection to the static constructor, so we do not need to waste time building delegates on instance creation.
1 parent 7046394 commit aebf174

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

src/NHibernate/AdoNet/MySqlClientSqlCommandSet.cs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,51 @@
22
using System.Data;
33
using System.Diagnostics;
44
using System.Reflection;
5+
using NHibernate.Util;
56

67
namespace NHibernate.AdoNet
78
{
8-
using Action = System.Action;
9-
109
public class MySqlClientSqlCommandSet : IDisposable
1110
{
1211
private static readonly System.Type adapterType;
12+
private static readonly Action<object> doInitialise;
13+
private static readonly Action<object, int> batchSizeSetter;
14+
private static readonly Action<object, IDbCommand> doAppend;
15+
private static readonly Func<object, int> doExecuteNonQuery;
16+
private static readonly Action<object> doDispose;
17+
1318
private readonly object instance;
14-
private readonly Action doInitialise;
15-
private readonly Action<int> batchSizeSetter;
16-
private readonly Func<IDbCommand, int> doAppend;
17-
private readonly Func<int> doExecuteNonQuery;
18-
private readonly Action doDispose;
1919
private int countOfCommands;
2020

2121
static MySqlClientSqlCommandSet()
2222
{
2323
var sysData = Assembly.Load("MySql.Data");
2424
adapterType = sysData.GetType("MySql.Data.MySqlClient.MySqlDataAdapter");
2525
Debug.Assert(adapterType != null, "Could not find MySqlDataAdapter!");
26+
27+
doInitialise = DelegateHelper.BuildAction(adapterType, "InitializeBatching");
28+
batchSizeSetter = DelegateHelper.BuildPropertySetter<int>(adapterType, "UpdateBatchSize");
29+
doAppend = DelegateHelper.BuildAction<IDbCommand>(adapterType, "AddToBatch");
30+
doExecuteNonQuery = DelegateHelper.BuildFunc<int>(adapterType, "ExecuteBatch");
31+
doDispose = DelegateHelper.BuildAction(adapterType, "Dispose");
2632
}
2733

2834
public MySqlClientSqlCommandSet(int batchSize)
2935
{
3036
instance = Activator.CreateInstance(adapterType, true);
31-
doInitialise = (Action) Delegate.CreateDelegate(typeof (Action), instance, "InitializeBatching");
32-
batchSizeSetter = (Action<int>) Delegate.CreateDelegate(typeof (Action<int>), instance, "set_UpdateBatchSize");
33-
doAppend = (Func<IDbCommand, int>) Delegate.CreateDelegate(typeof (Func<IDbCommand, int>), instance, "AddToBatch");
34-
doExecuteNonQuery = (Func<int>) Delegate.CreateDelegate(typeof (Func<int>), instance, "ExecuteBatch");
35-
doDispose = (Action)Delegate.CreateDelegate(typeof(Action), instance, "Dispose");
36-
37-
Initialise(batchSize);
38-
}
39-
40-
private void Initialise(int batchSize)
41-
{
42-
doInitialise();
43-
batchSizeSetter(batchSize);
37+
doInitialise(instance);
38+
batchSizeSetter(instance, batchSize);
4439
}
4540

4641
public void Append(IDbCommand command)
4742
{
48-
doAppend(command);
43+
doAppend(instance, command);
4944
countOfCommands++;
5045
}
5146

5247
public void Dispose()
5348
{
54-
doDispose();
49+
doDispose(instance);
5550
}
5651

5752
public int ExecuteNonQuery()
@@ -63,7 +58,7 @@ public int ExecuteNonQuery()
6358
return 0;
6459
}
6560

66-
return doExecuteNonQuery();
61+
return doExecuteNonQuery(instance);
6762
}
6863
catch (Exception exception)
6964
{
@@ -73,10 +68,7 @@ public int ExecuteNonQuery()
7368

7469
public int CountOfCommands
7570
{
76-
get
77-
{
78-
return countOfCommands;
79-
}
71+
get { return countOfCommands; }
8072
}
8173
}
8274
}

0 commit comments

Comments
 (0)