Skip to content

Commit

Permalink
In JsRT modes improved a performance of .NET methods projection
Browse files Browse the repository at this point in the history
  • Loading branch information
Taritsyn committed Feb 27, 2023
1 parent 4856631 commit 96ab036
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/MsieJavaScriptEngine/JsRt/Edge/EdgeTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,12 @@ private void ProjectMethods(EdgeEmbeddedItem externalItem)
string typeName = type.FullName;
BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);
MethodInfo[] methods = type.GetMethods(defaultBindingFlags);
IEnumerable<IGrouping<string, MethodInfo>> availableMethodGroups = GetAvailableMethodGroups(methods);
Dictionary<string, List<MethodInfo>> availableMethodGroups = GetAvailableMethodGroups(methods);

foreach (IGrouping<string, MethodInfo> methodGroup in availableMethodGroups)
foreach (KeyValuePair<string, List<MethodInfo>> methodGroup in availableMethodGroups)
{
string methodName = methodGroup.Key;
MethodInfo[] methodCandidates = methodGroup.ToArray();
MethodInfo[] methodCandidates = methodGroup.Value.ToArray();

EdgeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
{
Expand Down
6 changes: 3 additions & 3 deletions src/MsieJavaScriptEngine/JsRt/Ie/IeTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,12 @@ private void ProjectMethods(IeEmbeddedItem externalItem)
string typeName = type.FullName;
BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);
MethodInfo[] methods = type.GetMethods(defaultBindingFlags);
IEnumerable<IGrouping<string, MethodInfo>> availableMethodGroups = GetAvailableMethodGroups(methods);
Dictionary<string, List<MethodInfo>> availableMethodGroups = GetAvailableMethodGroups(methods);

foreach (IGrouping<string, MethodInfo> methodGroup in availableMethodGroups)
foreach (KeyValuePair<string, List<MethodInfo>> methodGroup in availableMethodGroups)
{
string methodName = methodGroup.Key;
MethodInfo[] methodCandidates = methodGroup.ToArray();
MethodInfo[] methodCandidates = methodGroup.Value.ToArray();

IeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
{
Expand Down
33 changes: 28 additions & 5 deletions src/MsieJavaScriptEngine/JsRt/TypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,37 @@ protected bool IsAvailableProperty(PropertyInfo property)
return isAvailable;
}

protected IEnumerable<IGrouping<string, MethodInfo>> GetAvailableMethodGroups(MethodInfo[] methods)
protected Dictionary<string, List<MethodInfo>> GetAvailableMethodGroups(MethodInfo[] methods)
{
IEnumerable<MethodInfo> availableMethods = methods.Where(ReflectionHelpers.IsFullyFledgedMethod);
if (!_allowReflection)
int methodCount = methods.Length;
if (methodCount == 0)
{
availableMethods = availableMethods.Where(ReflectionHelpers.IsAllowedMethod);
return new Dictionary<string, List<MethodInfo>>();
}

var availableMethodGroups = new Dictionary<string, List<MethodInfo>>(methodCount);

foreach (MethodInfo method in methods)
{
if (!ReflectionHelpers.IsFullyFledgedMethod(method)
|| (!_allowReflection && !ReflectionHelpers.IsAllowedMethod(method)))
{
continue;
}

string methodName = method.Name;
List<MethodInfo> methodGroup;

if (availableMethodGroups.TryGetValue(methodName, out methodGroup))
{
methodGroup.Add(method);
}
else
{
methodGroup = new List<MethodInfo> { method };
availableMethodGroups.Add(methodName, methodGroup);
}
}
IEnumerable<IGrouping<string, MethodInfo>> availableMethodGroups = availableMethods.GroupBy(m => m.Name);

return availableMethodGroups;
}
Expand Down
2 changes: 1 addition & 1 deletion src/MsieJavaScriptEngine/MsieJavaScriptEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageTags>JavaScript;ECMAScript;MSIE;IE;Edge;Chakra</PackageTags>
<PackageReleaseNotes>In JavaScript engine settings was added one new property - `AllowReflection` (default `false`).</PackageReleaseNotes>
<PackageReleaseNotes>In JsRT modes improved a performance of .NET methods projection.</PackageReleaseNotes>
<NeutralLanguage>en-US</NeutralLanguage>
<PackageOutputPath>../../nuget</PackageOutputPath>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
Expand Down
3 changes: 1 addition & 2 deletions src/MsieJavaScriptEngine/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
=============
RELEASE NOTES
=============
In JavaScript engine settings was added one new property - `AllowReflection`
(default `false`).
In JsRT modes improved a performance of .NET methods projection.

============
PROJECT SITE
Expand Down

0 comments on commit 96ab036

Please sign in to comment.