improve deepclone performance by expressiontree compiler · SharpRepository/SharpRepository@ccac5e3 · GitHub
Skip to content

Commit ccac5e3

Browse files
committed
improve deepclone performance by expressiontree compiler
1 parent 41a72bd commit ccac5e3

5 files changed

Lines changed: 162 additions & 77 deletions

File tree

SharpRepository.Benchmarks.Configuration/Program.cs

Lines changed: 55 additions & 15 deletions

SharpRepository.CacheRepository/CacheCompoundKeyRepositoryBase.cs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using SharpRepository.Repository;
77
using SharpRepository.Repository.Caching;
88
using SharpRepository.Repository.FetchStrategies;
9+
using SharpRepository.Repository.Helpers;
910

1011
namespace SharpRepository.CacheRepository
1112
{
@@ -72,11 +73,13 @@ private static IEnumerable<T> CloneDictionary(ConcurrentDictionary<string, T> li
7273

7374
foreach (var keyValuePair in list)
7475
{
75-
var newItem = new T();
76-
foreach (var propInfo in properties)
77-
{
78-
propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
79-
}
76+
//var newItem = new T();
77+
//foreach (var propInfo in properties)
78+
//{
79+
// propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
80+
//}
81+
//use new deep clone by lambda compiler to improved performance most.
82+
var newItem = keyValuePair.Value.DeepClone();
8083

8184
clonedList.Add(newItem);
8285
}
@@ -206,18 +209,22 @@ private static IEnumerable<T> CloneDictionary(ConcurrentDictionary<CompoundKey,
206209
// when you Google deep copy of generic list every answer uses either the IClonable interface on the T or having the T be Serializable
207210
// since we can't really put those constraints on T I'm going to do it via reflection
208211

209-
var type = typeof(T);
210-
var properties = type.GetProperties();
212+
//var type = typeof(T);
213+
//var properties = type.GetProperties();
211214

212215
var clonedList = new List<T>(list.Count);
213216

214217
foreach (var keyValuePair in list)
215218
{
216-
var newItem = new T();
217-
foreach (var propInfo in properties)
218-
{
219-
propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
220-
}
219+
//var newItem = new T();
220+
//foreach (var propInfo in properties)
221+
//{
222+
// // Don't try and set a value to a property w/o a setter
223+
// if (propInfo.CanWrite)
224+
// propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
225+
//}
226+
//use new deep clone by lambda compiler to improved performance most.
227+
var newItem = keyValuePair.Value.DeepClone();
221228

222229
clonedList.Add(newItem);
223230
}
@@ -343,18 +350,22 @@ private static IEnumerable<T> CloneDictionary(ConcurrentDictionary<CompoundKey,
343350
// when you Google deep copy of generic list every answer uses either the IClonable interface on the T or having the T be Serializable
344351
// since we can't really put those constraints on T I'm going to do it via reflection
345352

346-
var type = typeof(T);
347-
var properties = type.GetProperties();
353+
//var type = typeof(T);
354+
//var properties = type.GetProperties();
348355

349356
var clonedList = new List<T>(list.Count);
350357

351358
foreach (var keyValuePair in list)
352359
{
353-
var newItem = new T();
354-
foreach (var propInfo in properties)
355-
{
356-
propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
357-
}
360+
//var newItem = new T();
361+
//foreach (var propInfo in properties)
362+
//{
363+
// // Don't try and set a value to a property w/o a setter
364+
// if (propInfo.CanWrite)
365+
// propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
366+
//}
367+
//use new deep clone by lambda compiler to improved performance most.
368+
var newItem = keyValuePair.Value.DeepClone();
358369

359370
clonedList.Add(newItem);
360371
}
@@ -450,17 +461,21 @@ private static IEnumerable<T> CloneDictionary(ConcurrentDictionary<CompoundKey,
450461
// when you Google deep copy of generic list every answer uses either the IClonable interface on the T or having the T be Serializable
451462
// since we can't really put those constraints on T I'm going to do it via reflection
452463

453-
var type = typeof(T);
454-
var properties = type.GetProperties();
464+
//var type = typeof(T);
465+
//var properties = type.GetProperties();
455466
var clonedList = new List<T>(list.Count);
456467

457468
foreach (var keyValuePair in list)
458469
{
459-
var newItem = new T();
460-
foreach (var propInfo in properties)
461-
{
462-
propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
463-
}
470+
//var newItem = new T();
471+
//foreach (var propInfo in properties)
472+
//{
473+
// // Don't try and set a value to a property w/o a setter
474+
// if (propInfo.CanWrite)
475+
// propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
476+
//}
477+
//use new deep clone by lambda compiler to improved performance most.
478+
var newItem = keyValuePair.Value.DeepClone();
464479

465480
clonedList.Add(newItem);
466481
}

SharpRepository.CacheRepository/CacheRepositoryBase.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using SharpRepository.Repository.Caching;
77
using SharpRepository.Repository.FetchStrategies;
88
using System.Reflection;
9+
using SharpRepository.Repository.Helpers;
910

1011
namespace SharpRepository.CacheRepository
1112
{
@@ -72,11 +73,15 @@ private static IEnumerable<T> CloneDictionary(ConcurrentDictionary<TKey, T> list
7273

7374
foreach (var keyValuePair in list)
7475
{
75-
var newItem = new T();
76-
foreach (var propInfo in properties)
77-
{
78-
propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
79-
}
76+
//var newItem = new T();
77+
//foreach (var propInfo in properties)
78+
//{
79+
// // Don't try and set a value to a property w/o a setter
80+
// if (propInfo.CanWrite)
81+
// propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
82+
//}
83+
//use new deep clone by lambda compiler to improved performance most.
84+
var newItem = keyValuePair.Value.DeepClone();
8085

8186
clonedList.Add(newItem);
8287
}

SharpRepository.InMemoryRepository/InMemoryCompoundKeyRepositoryBase.cs

Lines changed: 34 additions & 21 deletions

0 commit comments

Comments
 (0)