-
-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve interop performance against BCL list types (#1792)
- Loading branch information
Showing
15 changed files
with
658 additions
and
417 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System.Collections; | ||
using BenchmarkDotNet.Attributes; | ||
using Jint.Native; | ||
using Jint.Runtime.Interop; | ||
|
||
namespace Jint.Benchmark; | ||
|
||
[MemoryDiagnoser] | ||
public class ListInteropBenchmark | ||
{ | ||
private static bool IsArrayLike(Type type) | ||
{ | ||
if (typeof(IDictionary).IsAssignableFrom(type)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (typeof(ICollection).IsAssignableFrom(type)) | ||
{ | ||
return true; | ||
} | ||
|
||
foreach (var typeInterface in type.GetInterfaces()) | ||
{ | ||
if (!typeInterface.IsGenericType) | ||
{ | ||
continue; | ||
} | ||
|
||
if (typeInterface.GetGenericTypeDefinition() == typeof(IReadOnlyCollection<>) | ||
|| typeInterface.GetGenericTypeDefinition() == typeof(ICollection<>)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private const int Count = 10_00; | ||
private Engine _engine; | ||
private JsValue[] _properties; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_engine = new Engine(options => | ||
{ | ||
options | ||
.SetWrapObjectHandler((engine, target, type) => | ||
{ | ||
var instance = new ObjectWrapper(engine, target); | ||
var isArrayLike = IsArrayLike(instance.Target.GetType()); | ||
if (isArrayLike) | ||
{ | ||
instance.Prototype = engine.Intrinsics.Array.PrototypeObject; | ||
} | ||
return instance; | ||
}) | ||
; | ||
}); | ||
|
||
_properties = new JsValue[Count]; | ||
var input = new List<Data>(Count); | ||
for (var i = 0; i < Count; ++i) | ||
{ | ||
input.Add(new Data { Category = new Category { Name = i % 2 == 0 ? "Pugal" : "Beagle" } }); | ||
_properties[i] = JsNumber.Create(i); | ||
} | ||
|
||
_engine.SetValue("input", input); | ||
_engine.SetValue("CONST", new { category = "Pugal" }); | ||
} | ||
|
||
[Benchmark] | ||
public void Filter() | ||
{ | ||
var value = (Data) _engine.Evaluate("input.filter(i => i.category?.name === CONST.category)[0]").ToObject(); | ||
if (value.Category.Name != "Pugal") | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void Indexing() | ||
{ | ||
_engine.Evaluate("for (var i = 0; i < input.length; ++i) { input[i]; }"); | ||
} | ||
|
||
[Benchmark] | ||
public void HasProperty() | ||
{ | ||
var input = (ObjectWrapper) _engine.GetValue("input"); | ||
for (var i = 0; i < _properties.Length; ++i) | ||
{ | ||
if (!input.HasProperty(_properties[i])) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
} | ||
} | ||
|
||
private class Data | ||
{ | ||
public Category Category { get; set; } | ||
} | ||
|
||
private class Category | ||
{ | ||
public string Name { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.