Currently I am using Microsoft Sync Framework to synchronize databases. I need to gather information per record which is inserted/updated/deleted by Microsoft Sync Framework and do something with this information.
The sync speed can go over 50.000 records per minute. So that means my additional code need to be very lightweight otherwise it will be a huge performance penalty.
Microsoft Sync Framework raises an SyncProgress event for each record. I am subscribed to that code like this:
// Assembly1
SyncProvider.SyncProgress += OnSyncProgress;
// ....
private void OnSyncProgress(object sender, DbSyncProgressEventArgs e)
{
switch (args.Stage)
{
case DbSyncStage.ApplyingInserts:
// MethodCall/Delegate/Action<>/EventHandler<> => HandleInsertedRecordInformation
// Do something with inserted record info
break;
case DbSyncStage.ApplyingUpdates:
// MethodCall/Delegate/Action<>/EventHandler<> => HandleUpdatedRecordInformation
// Do something with updated record info
break;
case DbSyncStage.ApplyingDeletes:
// MethodCall/Delegate/Action<>/EventHandler<> => HandleDeletedRecordInformation
// Do something with deleted record info
break;
}
}
Somewhere else in another assembly I have three methods:
// Assembly2
public class SyncInformation
{
public void HandleInsertedRecordInformation(...) {...}
public void HandleUpdatedRecordInformation(...) {...}
public void HandleInsertedRecordInformation(...) {...}
}
Assembly2 has a reference to Assembly1. So Assembly1 does not know anything about the existence of the SyncInformation class which need to handle the gathered information. So I have the following options to trigger this code:
- use events and subscribe on it in
Assembly2
1.1. EventHandler<>
1.2. Action<>
1.3. Delegates - using dependency injection:
public class Assembly2.SyncInformation : Assembly1.ISyncInformation - Other?
I know the performance depends on:
- OnSyncProgress
- switch
- using a method call, delegate, Action<> or EventHandler<>
- Implementation of SyncInformation class
I currently don’t care about the implementation of the SyncInformation class. I am mainly focused on the OnSyncProgress method and how to call the SyncInformation methods.
So my questions are:
- What is the most efficient approach?
- What is the most in-efficient approach?
- Is there a better way than using a switch in
OnSyncProgress?
No, it won’t.
50k / minute is not a significant number of method calls, unless you need to do thousands of such calls per record. This reeks of premature optimization. If we ignore intentionally badly written code and reflection-from-scratch-per-call, the absolute slowest way of calling a method is via untyped delegate
DynamicInvoke, and that can do 50k calls in 42ms on my machine. Anything else (typed delegateInvoke, directcallvirton a class or interface,dynamic, staticcalletc) will not even be measurable (as in: it will be literally 0ms for 50k calls).You’d do better to use a profiler to see what actually matters, IMO. It will almost certainly be the “doing” code, rather than the plumbing.