Skip to content

Commit

Permalink
Improved 20% performance with field accessing
Browse files Browse the repository at this point in the history
  • Loading branch information
liiir1985 committed Jun 29, 2022
1 parent d803e3e commit 0035b81
Show file tree
Hide file tree
Showing 186 changed files with 1,762 additions and 990 deletions.
10 changes: 8 additions & 2 deletions ILRuntime/CLR/Method/CLRMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
using System;
using System.Collections.Generic;
using System.Reflection;

#if DEBUG && !DISABLE_ILRUNTIME_DEBUG
using AutoList = System.Collections.Generic.List<object>;
#else
using AutoList = ILRuntime.Other.UncheckedList<object>;
#endif
namespace ILRuntime.CLR.Method
{
public class CLRMethod : IMethod
Expand Down Expand Up @@ -270,7 +276,7 @@ void InitParameters()
return (StackObject*)((long)a - sizeof(StackObject) * b);
}

public unsafe object Invoke(Runtime.Intepreter.ILIntepreter intepreter, StackObject* esp, IList<object> mStack, bool isNewObj = false)
public unsafe object Invoke(Runtime.Intepreter.ILIntepreter intepreter, StackObject* esp, AutoList mStack, bool isNewObj = false)
{
if (parameters == null)
{
Expand Down Expand Up @@ -346,7 +352,7 @@ public unsafe object Invoke(Runtime.Intepreter.ILIntepreter intepreter, StackObj
}
}

unsafe void FixReference(int paramCount, StackObject* esp, object[] param, IList<object> mStack, object instance, bool hasThis)
unsafe void FixReference(int paramCount, StackObject* esp, object[] param, AutoList mStack, object instance, bool hasThis)
{
var cnt = hasThis ? paramCount + 1 : paramCount;
for (int i = cnt; i >= 1; i--)
Expand Down
9 changes: 7 additions & 2 deletions ILRuntime/CLR/TypeSystem/CLRType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
using ILRuntime.Runtime.Enviorment;
using ILRuntime.Runtime.Stack;

#if DEBUG && !DISABLE_ILRUNTIME_DEBUG
using AutoList = System.Collections.Generic.List<object>;
#else
using AutoList = ILRuntime.Other.UncheckedList<object>;
#endif
namespace ILRuntime.CLR.TypeSystem
{
public unsafe class CLRType : IType
Expand Down Expand Up @@ -404,7 +409,7 @@ public object GetFieldValue(int hash, object target)
return null;
}

public bool CopyFieldToStack(int hash, object target, Runtime.Intepreter.ILIntepreter intp, ref StackObject* esp, IList<object> mStack)
public bool CopyFieldToStack(int hash, object target, Runtime.Intepreter.ILIntepreter intp, ref StackObject* esp, AutoList mStack)
{
if (fieldMapping == null)
InitializeFields();
Expand All @@ -420,7 +425,7 @@ public bool CopyFieldToStack(int hash, object target, Runtime.Intepreter.ILIntep
return false;
}

public bool AssignFieldFromStack(int hash, ref object target, Runtime.Intepreter.ILIntepreter intp, StackObject* esp, IList<object> mStack)
public bool AssignFieldFromStack(int hash, ref object target, Runtime.Intepreter.ILIntepreter intp, StackObject* esp, AutoList mStack)
{
if (fieldMapping == null)
InitializeFields();
Expand Down
2 changes: 1 addition & 1 deletion ILRuntime/Other/UncheckedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ILRuntime.Other
/// </summary>
/// <typeparam name="T"></typeparam>
[Serializable]
public class UncheckedList<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection
public sealed class UncheckedList<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection
{

private const int _defaultCapacity = 4;
Expand Down
29 changes: 25 additions & 4 deletions ILRuntime/Runtime/CLRBinding/BindingCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public static void GenerateBindingCode(List<Type> types, string outputPath,
using ILRuntime.Runtime.Stack;
using ILRuntime.Reflection;
using ILRuntime.CLR.Utils;
#if DEBUG && !DISABLE_ILRUNTIME_DEBUG
using AutoList = System.Collections.Generic.List<object>;
#else
using AutoList = ILRuntime.Other.UncheckedList<object>;
#endif
namespace ILRuntime.Runtime.Generated
{
Expand Down Expand Up @@ -263,7 +268,11 @@ public static void GenerateBindingCode(ILRuntime.Runtime.Enviorment.AppDomain do
using ILRuntime.Runtime.Stack;
using ILRuntime.Reflection;
using ILRuntime.CLR.Utils;
#if DEBUG && !DISABLE_ILRUNTIME_DEBUG
using AutoList = System.Collections.Generic.List<object>;
#else
using AutoList = ILRuntime.Other.UncheckedList<object>;
#endif
namespace ILRuntime.Runtime.Generated
{
unsafe class ");
Expand Down Expand Up @@ -351,7 +360,11 @@ public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app)
sb.AppendLine(@"using System;
using System.Collections.Generic;
using System.Reflection;
#if DEBUG && !DISABLE_ILRUNTIME_DEBUG
using AutoList = System.Collections.Generic.List<object>;
#else
using AutoList = ILRuntime.Other.UncheckedList<object>;
#endif
namespace ILRuntime.Runtime.Generated
{
class CLRBindings
Expand Down Expand Up @@ -657,7 +670,11 @@ internal static List<string> GenerateDelegateBinding(List<Type> types, string ou
using ILRuntime.Runtime.Stack;
using ILRuntime.Reflection;
using ILRuntime.CLR.Utils;
#if DEBUG && !DISABLE_ILRUNTIME_DEBUG
using AutoList = System.Collections.Generic.List<object>;
#else
using AutoList = ILRuntime.Other.UncheckedList<object>;
#endif
namespace ILRuntime.Runtime.Generated
{
unsafe class ");
Expand Down Expand Up @@ -814,7 +831,11 @@ internal static void GenerateBindingInitializeScript(List<string> clsNames, List
sb.Append(@"using System;
using System.Collections.Generic;
using System.Reflection;
#if DEBUG && !DISABLE_ILRUNTIME_DEBUG
using AutoList = System.Collections.Generic.List<object>;
#else
using AutoList = ILRuntime.Other.UncheckedList<object>;
#endif
namespace ILRuntime.Runtime.Generated
{
class CLRBindings
Expand Down
4 changes: 2 additions & 2 deletions ILRuntime/Runtime/CLRBinding/CommonBindingGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal static string GenerateCommonCode(this Type type, string typeClsName)
StringBuilder sb = new StringBuilder();
if (type.IsPrimitive)
{
sb.AppendLine(string.Format(" static {0} GetInstance(ILRuntime.Runtime.Enviorment.AppDomain __domain, StackObject* ptr_of_this_method, IList<object> __mStack)", typeClsName));
sb.AppendLine(string.Format(" static {0} GetInstance(ILRuntime.Runtime.Enviorment.AppDomain __domain, StackObject* ptr_of_this_method, AutoList __mStack)", typeClsName));
sb.AppendLine(" {");
if (type.IsPrimitive || type.IsValueType)
sb.AppendLine(" ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);");
Expand Down Expand Up @@ -128,7 +128,7 @@ internal static string GenerateCommonCode(this Type type, string typeClsName)
}
if (!type.IsPrimitive && !type.IsAbstract)
{
sb.AppendLine(string.Format(" static void WriteBackInstance(ILRuntime.Runtime.Enviorment.AppDomain __domain, StackObject* ptr_of_this_method, IList<object> __mStack, ref {0} instance_of_this_method)", typeClsName));
sb.AppendLine(string.Format(" static void WriteBackInstance(ILRuntime.Runtime.Enviorment.AppDomain __domain, StackObject* ptr_of_this_method, AutoList __mStack, ref {0} instance_of_this_method)", typeClsName));
sb.AppendLine(" {");
sb.AppendLine(@" ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);
switch(ptr_of_this_method->ObjectType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal static string GenerateConstructorWraperCode(this Type type, Constructor
continue;
var param = i.GetParameters();
int paramCnt = param.Length;
sb.AppendLine(string.Format(" static StackObject* Ctor_{0}(ILIntepreter __intp, StackObject* __esp, IList<object> __mStack, CLRMethod __method, bool isNewObj)", idx));
sb.AppendLine(string.Format(" static StackObject* Ctor_{0}(ILIntepreter __intp, StackObject* __esp, AutoList __mStack, CLRMethod __method, bool isNewObj)", idx));
sb.AppendLine(" {");
sb.AppendLine(" ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;");
if (param.Length != 0)
Expand Down
4 changes: 2 additions & 2 deletions ILRuntime/Runtime/CLRBinding/FieldBindingGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal static string GenerateFieldWraperCode(this Type type, FieldInfo[] field
}
sb.AppendLine(" }");
sb.AppendLine();
sb.AppendLine(string.Format(" static StackObject* CopyToStack_{0}_{1}(ref object o, ILIntepreter __intp, StackObject* __ret, IList<object> __mStack)", i.Name, idx));
sb.AppendLine(string.Format(" static StackObject* CopyToStack_{0}_{1}(ref object o, ILIntepreter __intp, StackObject* __ret, AutoList __mStack)", i.Name, idx));
sb.AppendLine(" {");
if (i.IsStatic)
{
Expand Down Expand Up @@ -118,7 +118,7 @@ internal static string GenerateFieldWraperCode(this Type type, FieldInfo[] field
}
sb.AppendLine(" }");
sb.AppendLine();
sb.AppendLine(string.Format(" static StackObject* AssignFromStack_{0}_{1}(ref object o, ILIntepreter __intp, StackObject* ptr_of_this_method, IList<object> __mStack)", i.Name, idx));
sb.AppendLine(string.Format(" static StackObject* AssignFromStack_{0}_{1}(ref object o, ILIntepreter __intp, StackObject* ptr_of_this_method, AutoList __mStack)", i.Name, idx));
sb.AppendLine(" {");
sb.AppendLine(" ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;");
i.FieldType.AppendArgumentCode(sb, 0, i.Name, valueTypeBinders, false, false, false);
Expand Down
2 changes: 1 addition & 1 deletion ILRuntime/Runtime/CLRBinding/MethodBindingGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ internal static string GenerateMethodWraperCode(this Type type, MethodInfo[] met
int paramCnt = param.Length;
if (!i.IsStatic)
paramCnt++;
sb.AppendLine(string.Format(" static StackObject* {0}_{1}(ILIntepreter __intp, StackObject* __esp, IList<object> __mStack, CLRMethod __method, bool isNewObj)", i.Name, idx));
sb.AppendLine(string.Format(" static StackObject* {0}_{1}(ILIntepreter __intp, StackObject* __esp, AutoList __mStack, CLRMethod __method, bool isNewObj)", i.Name, idx));
sb.AppendLine(" {");
sb.AppendLine(" ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;");
if (param.Length != 0 || !i.IsStatic)
Expand Down
9 changes: 7 additions & 2 deletions ILRuntime/Runtime/Debugger/DebugService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
#endif
using System.Reflection;

#if DEBUG && !DISABLE_ILRUNTIME_DEBUG
using AutoList = System.Collections.Generic.List<object>;
#else
using AutoList = ILRuntime.Other.UncheckedList<object>;
#endif
namespace ILRuntime.Runtime.Debugger
{
public class DebugService
Expand Down Expand Up @@ -1554,7 +1559,7 @@ private static bool CheckParameters(ParameterInfo[] parameters, Type[] checkType
return true;
}

unsafe bool GetValueExpandable(ILIntepreter intp, StackObject* esp, IList<object> mStack)
unsafe bool GetValueExpandable(ILIntepreter intp, StackObject* esp, AutoList mStack)
{
if (esp->ObjectType < ObjectTypes.ValueTypeObjectReference)
return false;
Expand Down Expand Up @@ -1726,7 +1731,7 @@ internal unsafe void DumpStack(StackObject* esp, RuntimeStack stack)
#endif
}

unsafe void GetStackObjectText(StringBuilder sb, StackObject* esp, IList<object> mStack, StackObject* valueTypeEnd)
unsafe void GetStackObjectText(StringBuilder sb, StackObject* esp, AutoList mStack, StackObject* valueTypeEnd)
{
string text = "null";
switch (esp->ObjectType)
Expand Down
9 changes: 7 additions & 2 deletions ILRuntime/Runtime/Enviorment/AppDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
using ILRuntime.Runtime.Intepreter.RegisterVM;
using System.Threading;

#if DEBUG && !DISABLE_ILRUNTIME_DEBUG
using AutoList = System.Collections.Generic.List<object>;
#else
using AutoList = ILRuntime.Other.UncheckedList<object>;
#endif
namespace ILRuntime.Runtime.Enviorment
{
public unsafe delegate StackObject* CLRRedirectionDelegate(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj);
public unsafe delegate StackObject* CLRRedirectionDelegate(ILIntepreter intp, StackObject* esp, AutoList mStack, CLRMethod method, bool isNewObj);
public delegate object CLRFieldGetterDelegate(ref object target);
public unsafe delegate StackObject* CLRFieldBindingDelegate(ref object target, ILIntepreter __intp, StackObject* __esp, IList<object> __mStack);
public unsafe delegate StackObject* CLRFieldBindingDelegate(ref object target, ILIntepreter __intp, StackObject* __esp, AutoList __mStack);
public delegate void CLRFieldSetterDelegate(ref object target, object value);
public delegate object CLRMemberwiseCloneDelegate(ref object target);
public delegate object CLRCreateDefaultInstanceDelegate();
Expand Down
Loading

0 comments on commit 0035b81

Please sign in to comment.