Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize array access and add ability to hint array size #455

Merged
merged 8 commits into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions Jint.Tests.CommonScripts/Jint.Tests.CommonScripts.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
<AssemblyName>Jint.Tests.CommonScripts</AssemblyName>
Expand All @@ -14,21 +13,18 @@
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="Scripts\*.*" Exclude="bin\**;obj\**;**\*.xproj;packages\**;@(EmbeddedResource)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Jint\Jint.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.analyzers" Version="0.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit.analyzers" Version="0.7.0" />
<PackageReference Include="xunit.runner.console" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

</Project>
</Project>
15 changes: 6 additions & 9 deletions Jint.Tests.Ecma/Jint.Tests.Ecma.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>Jint.Tests.Ecma</AssemblyName>
Expand All @@ -15,21 +14,19 @@
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Jint\Jint.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit.analyzers" Version="0.3.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.analyzers" Version="0.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit.runner.console" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

</Project>
</Project>
3 changes: 2 additions & 1 deletion Jint.Tests/Jint.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.analyzers" Version="0.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit.analyzers" Version="0.7.0" />
<PackageReference Include="xunit.runner.console" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
</Project>
24 changes: 23 additions & 1 deletion Jint.Tests/Runtime/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,28 @@ public void ArrayIndexerIsAssigned()
");
}

[Fact]
public void DenseArrayTurnsToSparseArrayWhenSizeGrowsTooMuch()
{
RunTest(@"
var n = 1024*10+2;
var o = Array(n);
for (var i = 0; i < n; i++) o[i] = i;
assert(o[0] == 0);
assert(o[n - 1] == n -1);
");
}

[Fact]
public void DenseArrayTurnsToSparseArrayWhenSparseIndexed()
{
RunTest(@"
var o = Array();
o[100] = 1;
assert(o[100] == 1);
");
}

[Fact]
public void ArrayPopShouldDecrementLength()
{
Expand Down Expand Up @@ -1893,7 +1915,7 @@ public void ShouldStringifyNumWithoutV8DToA()

Assert.True(val.AsString() == "53.6841659");
}

[Theory]
[InlineData("", "escape('')")]
[InlineData("%u0100%u0101%u0102", "escape('\u0100\u0101\u0102')")]
Expand Down
6 changes: 2 additions & 4 deletions Jint/Jint.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<NeutralLanguage>en-US</NeutralLanguage>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyOriginatorKeyFile>Jint.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Esprima" Version="1.0.0-beta-1000" />
<PackageReference Include="Esprima" Version="1.0.0-beta-1011" />
</ItemGroup>
</Project>
</Project>
42 changes: 36 additions & 6 deletions Jint/Native/Array/ArrayConstructor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using Jint.Native.Function;
using Jint.Native.Object;
using Jint.Runtime;
Expand All @@ -19,7 +20,7 @@ public static ArrayConstructor CreateArrayConstructor(Engine engine)
var obj = new ArrayConstructor(engine);
obj.Extensible = true;

// The value of the [[Prototype]] internal property of the Array constructor is the Function prototype object
// The value of the [[Prototype]] internal property of the Array constructor is the Function prototype object
obj.Prototype = engine.Function.PrototypeObject;
obj.PrototypeObject = ArrayPrototype.CreatePrototypeObject(engine, obj);

Expand All @@ -36,7 +37,7 @@ public void Configure()
FastAddProperty("isArray", new ClrFunctionInstance(Engine, IsArray, 1), true, false, true);
}

private JsValue IsArray(JsValue thisObj, JsValue[] arguments)
private static JsValue IsArray(JsValue thisObj, JsValue[] arguments)
{
if (arguments.Length == 0)
{
Expand All @@ -55,7 +56,36 @@ public override JsValue Call(JsValue thisObject, JsValue[] arguments)

public ObjectInstance Construct(JsValue[] arguments)
{
var instance = new ArrayInstance(Engine);
// check if we can figure out good size
var capacity = arguments.Length > 0 ? (uint) arguments.Length : 0;
if (arguments.Length == 1 && arguments[0].Type == Types.Number)
{
var number = arguments[0].AsNumber();
if (number > 0)
{
capacity = (uint) number;
}
}
return Construct(arguments, capacity);
}

public ArrayInstance Construct(int capacity)
{
if (capacity < 0)
{
throw new ArgumentException("invalid array length", nameof(capacity));
}
return Construct(System.Array.Empty<JsValue>(), (uint) capacity);
}

public ArrayInstance Construct(uint capacity)
{
return Construct(System.Array.Empty<JsValue>(), capacity);
}

public ArrayInstance Construct(JsValue[] arguments, uint capacity)
{
var instance = new ArrayInstance(Engine, capacity);
instance.Prototype = PrototypeObject;
instance.Extensible = true;

Expand All @@ -66,7 +96,7 @@ public ObjectInstance Construct(JsValue[] arguments)
{
throw new JavaScriptException(Engine.RangeError, "Invalid array length");
}

instance.FastAddProperty("length", length, true, false, false);
}
else if (arguments.Length == 1 && arguments.At(0).IsObject() && arguments.At(0).As<ObjectWrapper>() != null )
Expand All @@ -75,7 +105,7 @@ public ObjectInstance Construct(JsValue[] arguments)

if (enumerable != null)
{
var jsArray = Engine.Array.Construct(Arguments.Empty);
var jsArray = (ArrayInstance) Engine.Array.Construct(Arguments.Empty);
foreach (var item in enumerable)
{
var jsItem = JsValue.FromObject(Engine, item);
Expand Down
26 changes: 26 additions & 0 deletions Jint/Native/Array/ArrayExecutionContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Threading;

namespace Jint.Native.Array
{
/// <summary>
/// Helper to cache common data structures needed in array access on a per thread basis.
/// </summary>
internal class ArrayExecutionContext
{
// cache key container for array iteration for less allocations
private static readonly ThreadLocal<ArrayExecutionContext> _executionContext = new ThreadLocal<ArrayExecutionContext>(() => new ArrayExecutionContext());

private ArrayExecutionContext()
{
}

public List<uint> KeyCache = new List<uint>();

public JsValue[] CallArray1 = new JsValue[1];
public JsValue[] CallArray3 = new JsValue[3];
public JsValue[] CallArray4 = new JsValue[4];

public static ArrayExecutionContext Current => _executionContext.Value;
}
}
Loading