Skip to content

Commit e910ada

Browse files
committed
(Perf) Replaced Fasterflect with custom reflection utilities (much faster & less mem usage)
1 parent 2289b5c commit e910ada

37 files changed

Lines changed: 113 additions & 358 deletions

File tree

src/Libraries/SmartStore.Core/ComponentModel/SerializationUtils.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
using System.IO;
3636
using System.Text;
3737
//using System.Reflection;
38-
using Fasterflect;
3938

4039
using System.Xml;
4140
using System.Xml.Serialization;
@@ -419,17 +418,17 @@ public static object DeSerializeObject(byte[] buffer, Type objectType, bool thro
419418
/// <returns></returns>
420419
public static string ObjectToString(object instance, string separator, ObjectToStringTypes type)
421420
{
422-
var fi = instance.GetType().Fields();
421+
var fi = instance.GetType().GetFields();
423422

424423
string output = string.Empty;
425424

426425
if (type == ObjectToStringTypes.Properties || type == ObjectToStringTypes.PropertiesAndFields)
427426
{
428-
foreach (var property in instance.GetType().Properties())
427+
foreach (var property in instance.GetType().GetProperties())
429428
{
430429
try
431430
{
432-
output += property.Name + ":" + instance.GetPropertyValue(property.Name).ToString() + separator;
431+
output += property.Name + ":" + property.GetValue(instance, null).ToString() + separator;
433432
}
434433
catch
435434
{
@@ -444,7 +443,7 @@ public static string ObjectToString(object instance, string separator, ObjectToS
444443
{
445444
try
446445
{
447-
output = output + field.Name + ": " + instance.GetFieldValue(field.Name).ToString() + separator;
446+
output = output + field.Name + ": " + field.GetValue(instance).ToString() + separator;
448447
}
449448
catch
450449
{

src/Libraries/SmartStore.Core/Extensions/DictionaryExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Web.Routing;
66
using System.Globalization;
77
using System.Dynamic;
8+
using SmartStore.Utilities;
89

910
namespace SmartStore
1011
{
@@ -34,7 +35,7 @@ public static void Merge(this IDictionary<string, object> instance, string key,
3435

3536
public static void Merge(this IDictionary<string, object> instance, object values, bool replaceExisting = true)
3637
{
37-
instance.Merge(new RouteValueDictionary(values), replaceExisting);
38+
instance.Merge(CollectionHelper.ObjectToDictionary(values), replaceExisting);
3839
}
3940

4041
public static void Merge<TKey, TValue>(this IDictionary<TKey, TValue> instance, IDictionary<TKey, TValue> from, bool replaceExisting = true)

src/Libraries/SmartStore.Core/Extensions/TypeExtensions.cs

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Reflection;
55
using System.Collections;
66
using System.Diagnostics;
7-
using Fasterflect;
87

98
namespace SmartStore
109
{
@@ -393,63 +392,6 @@ public static bool IsGenericDictionary(this Type type)
393392
// return false;
394393
//}
395394

396-
/// <summary>
397-
/// Gets the member's value on the object.
398-
/// </summary>
399-
/// <param name="member">The member.</param>
400-
/// <param name="target">The target object.</param>
401-
/// <returns>The member's value on the object.</returns>
402-
public static object GetValue(this MemberInfo member, object target)
403-
{
404-
Guard.ArgumentNotNull(member, "member");
405-
Guard.ArgumentNotNull(target, "target");
406-
407-
var type = target.GetType();
408-
409-
switch (member.MemberType)
410-
{
411-
case MemberTypes.Field:
412-
return target.GetFieldValue(member.Name);
413-
//return ((FieldInfo)member).GetValue(target);
414-
case MemberTypes.Property:
415-
return target.GetPropertyValue(member.Name);
416-
default:
417-
throw new ArgumentException("MemberInfo '{0}' is not of type FieldInfo or PropertyInfo".FormatInvariant(member.Name), "member");
418-
}
419-
}
420-
421-
/// <summary>
422-
/// Sets the member's value on the target object.
423-
/// </summary>
424-
/// <param name="member">The member.</param>
425-
/// <param name="target">The target.</param>
426-
/// <param name="value">The value.</param>
427-
public static void SetValue(this MemberInfo member, object target, object value)
428-
{
429-
Guard.ArgumentNotNull(member, "member");
430-
Guard.ArgumentNotNull(target, "target");
431-
432-
switch (member.MemberType)
433-
{
434-
case MemberTypes.Field:
435-
target.SetFieldValue(member.Name, value);
436-
break;
437-
//return ((FieldInfo)member).GetValue(target);
438-
case MemberTypes.Property:
439-
try
440-
{
441-
target.SetPropertyValue(member.Name, value);
442-
}
443-
catch (TargetParameterCountException e)
444-
{
445-
throw new ArgumentException("PropertyInfo '{0}' has index parameters".FormatInvariant(member.Name), "member", e);
446-
}
447-
break;
448-
default:
449-
throw new ArgumentException("MemberInfo '{0}' is not of type FieldInfo or PropertyInfo".FormatInvariant(member.Name), "member");
450-
}
451-
}
452-
453395
/// <summary>
454396
/// Gets the underlying type of a <see cref="Nullable{T}" /> type.
455397
/// </summary>

src/Libraries/SmartStore.Core/Infrastructure/ComparableObject.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Linq.Expressions;
55
using System.Reflection;
66
using System.ComponentModel;
7-
using Fasterflect;
87

98
namespace SmartStore
109
{
@@ -67,7 +66,7 @@ public override int GetHashCode()
6766

6867
foreach (var pi in signatureProperties)
6968
{
70-
object value = this.GetPropertyValue(pi.Name); // pi.GetValue(this);
69+
object value = pi.GetValue(this);
7170

7271
if (value != null)
7372
hashCode = (hashCode * HashMultiplier) ^ value.GetHashCode();
@@ -102,8 +101,8 @@ protected virtual bool HasSameSignatureAs(ComparableObject compareTo)
102101

103102
foreach (var pi in signatureProperties)
104103
{
105-
object thisValue = this.GetPropertyValue(pi.Name);
106-
object thatValue = compareTo.GetPropertyValue(pi.Name);
104+
object thisValue = pi.GetValue(this);
105+
object thatValue = pi.GetValue(compareTo);
107106

108107
if (thisValue == null && thatValue == null)
109108
continue;

src/Libraries/SmartStore.Core/Infrastructure/DateRange.cs

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/Libraries/SmartStore.Core/Infrastructure/Error.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using System.Globalization;
33
using System.Diagnostics;
44

5-
using Fasterflect;
6-
75
namespace SmartStore
86
{
97
public static class Error

src/Libraries/SmartStore.Core/Infrastructure/ValueObject.cs

Lines changed: 0 additions & 90 deletions
This file was deleted.

src/Libraries/SmartStore.Core/SmartStore.Core.csproj

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@
7777
<SpecificVersion>False</SpecificVersion>
7878
<HintPath>..\..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll</HintPath>
7979
</Reference>
80-
<Reference Include="Fasterflect, Version=2.1.3.0, Culture=neutral, PublicKeyToken=38d18473284c1ca7, processorArchitecture=MSIL">
81-
<SpecificVersion>False</SpecificVersion>
82-
<HintPath>..\..\packages\fasterflect.2.1.3\lib\net40\Fasterflect.dll</HintPath>
83-
</Reference>
8480
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
8581
<Private>True</Private>
8682
<HintPath>..\..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
@@ -441,7 +437,6 @@
441437
<Compile Include="Data\IRepository.cs" />
442438
<Compile Include="IHideObjectMembers.cs" />
443439
<Compile Include="Infrastructure\ComparableObject.cs" />
444-
<Compile Include="Infrastructure\DateRange.cs" />
445440
<Compile Include="Infrastructure\DisposableObject.cs" />
446441
<Compile Include="Infrastructure\EnumFriendlyNameAttribute.cs" />
447442
<Compile Include="Infrastructure\Error.cs" />
@@ -451,7 +446,6 @@
451446
<Compile Include="Infrastructure\IStartupTask.cs" />
452447
<Compile Include="Infrastructure\Misc.cs" />
453448
<Compile Include="Infrastructure\RegularExpressions.cs" />
454-
<Compile Include="Infrastructure\ValueObject.cs" />
455449
<Compile Include="IWebHelper.cs" />
456450
<Compile Include="Domain\Security\DefaultPermissionRecord.cs" />
457451
<Compile Include="Domain\Security\PermissionRecord.cs" />
@@ -599,6 +593,7 @@
599593
<Compile Include="Packaging\Updater\AppUpdater.cs" />
600594
</ItemGroup>
601595
<ItemGroup>
596+
<None Include="app.config" />
602597
<None Include="packages.config" />
603598
</ItemGroup>
604599
<ItemGroup>

src/Libraries/SmartStore.Core/Utilities/CollectionHelper.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
using System.Text;
77
using System.Collections;
88
using System.Globalization;
9-
using System.Collections.Specialized;
10-
using System.ComponentModel;
11-
12-
using Fasterflect;
9+
using SmartStore.Utilities.Reflection;
1310

1411
namespace SmartStore.Utilities
1512
{
@@ -221,24 +218,12 @@ public static IDictionary<string, object> ObjectToDictionary(object obj)
221218

222219
Type t = obj.GetType();
223220

224-
return t.GetProperties(BindingFlags.Instance | BindingFlags.Public)
221+
return FastProperty.GetProperties(t).Values
225222
.ToDictionary(k => k.Name.Replace("_", "-"),
226-
v => obj.GetPropertyValue(v.Name),
223+
v => v.GetValue(obj),
227224
StringComparer.OrdinalIgnoreCase);
228225
}
229226

230-
public static NameValueCollection ObjectToNameValueCollection(object obj)
231-
{
232-
var result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
233-
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj))
234-
{
235-
object value = descriptor.GetValue(obj);
236-
result.Add(descriptor.Name, value.ToString());
237-
}
238-
239-
return result;
240-
}
241-
242227
/// <summary>
243228
/// Converts the <see cref="Enum" /> type to an <see cref="IList" />
244229
/// compatible object.

0 commit comments

Comments
 (0)