Skip to content

Commit

Permalink
10% performance boost by replacing callvirt to call in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
liiir1985 committed Apr 22, 2020
1 parent f0e537f commit 0a4a7b6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ILRuntime/CLR/Method/ILMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ unsafe void InitToken(ref OpCode code, object token, Dictionary<Mono.Cecil.Cil.I
var m = appdomain.GetMethod(token, declaringType, this, out invalidToken);
if (m != null)
{
if(code.Code == OpCodeEnum.Callvirt && m is ILMethod)
{
ILMethod ilm = (ILMethod)m;
if (!ilm.def.IsAbstract && !ilm.def.IsVirtual && !ilm.DeclearingType.IsInterface)
code.Code = OpCodeEnum.Call;
}
if (invalidToken)
code.TokenInteger = m.GetHashCode();
else
Expand Down
21 changes: 20 additions & 1 deletion TestCases/Test01.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,25 @@ public static void UnitTest_Performance()
sw.Reset();
sw.Start();
cnt = 0;
for (int i = 0; i < 100000; i++)
for (int i = 0; i < 1000000; i++)
{
FuncCallResult(ref cnt, i);
}
sw.Stop();

Console.WriteLine(string.Format("Elapsed time:{0:0}ms, result = {1}", sw.ElapsedMilliseconds, cnt));

PerfTest test = new PerfTest();
sw.Reset();
sw.Start();
cnt = 0;
for (int i = 0; i < 1000000; i++)
{
test.FuncCallResult(ref cnt, i);
}
sw.Stop();
Console.WriteLine(string.Format("Elapsed time:{0:0}ms, result = {1}", sw.ElapsedMilliseconds, cnt));

}

public static void UnitTest_Performance3()
Expand Down Expand Up @@ -210,6 +222,13 @@ static void Test1098Sub(Test1098Cls cls)
cls.B = "ok";
}

class PerfTest
{
public void FuncCallResult(ref int cnt, int i)
{
cnt++;
}
}
static void FuncCallResult(ref int cnt, int i)
{
cnt++;
Expand Down

0 comments on commit 0a4a7b6

Please sign in to comment.