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

Fix array performance regression #1148

Merged
merged 1 commit into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions Jint/Native/Array/ArrayOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public virtual JsValue[] GetAll(Types elementTypes)

public abstract void CreateDataPropertyOrThrow(ulong index, JsValue value);

public abstract void Set(ulong index, JsValue value, bool throwOnError);
public abstract void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true);

public abstract void DeletePropertyOrThrow(ulong index);

Expand Down Expand Up @@ -226,7 +226,7 @@ public override bool TryGetValue(ulong index, out JsValue value)
public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
=> _target.CreateDataPropertyOrThrow(JsString.Create(index), value);

public override void Set(ulong index, JsValue value, bool throwOnError)
public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
=> _target.Set(JsString.Create(index), value, throwOnError);

public override void DeletePropertyOrThrow(ulong index)
Expand Down Expand Up @@ -297,8 +297,8 @@ public override void DeletePropertyOrThrow(ulong index)
public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
=> _target.SetIndexValue((uint) index, value, updateLength: false);

public override void Set(ulong index, JsValue value, bool throwOnError)
=> _target.Set((uint) index, value, throwOnError);
public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
=> _target.SetIndexValue((uint) index, value, updateLength);
}

private sealed class TypedArrayInstanceOperations : ArrayOperations
Expand Down Expand Up @@ -357,7 +357,7 @@ public override bool TryGetValue(ulong index, out JsValue value)
public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
=> _target.CreateDataPropertyOrThrow(index, value);

public override void Set(ulong index, JsValue value, bool throwOnError)
public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
=> _target[(int) index] = value;

public override void DeletePropertyOrThrow(ulong index)
Expand All @@ -381,4 +381,4 @@ protected ArrayOperations(T target)

public override ObjectInstance Target => _target;
}
}
}
14 changes: 7 additions & 7 deletions Jint/Native/Array/ArrayPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private JsValue CopyWithin(JsValue thisObj, JsValue[] arguments)
if (fromPresent)
{
var fromValue = operations.Get((ulong) from);
operations.Set((ulong) to, fromValue, throwOnError: true);
operations.Set((ulong) to, fromValue, updateLength: true, throwOnError: true);
}
else
{
Expand Down Expand Up @@ -994,7 +994,8 @@ private JsValue Unshift(JsValue thisObj, JsValue[] arguments)
/// </summary>
private JsValue Sort(JsValue thisObj, JsValue[] arguments)
{
var obj = ArrayOperations.For(TypeConverter.ToObject(_realm, thisObj));
var objectInstance = TypeConverter.ToObject(_realm, thisObj);
var obj = ArrayOperations.For(objectInstance);

var compareArg = arguments.At(0);
ICallable compareFn = null;
Expand Down Expand Up @@ -1034,12 +1035,11 @@ private JsValue Sort(JsValue thisObj, JsValue[] arguments)
uint j;
for (j = 0; j < itemCount; ++j)
{
if (!ReferenceEquals(array[j], null))
{
obj.Set(j, array[j], throwOnError: true);
}
objectInstance.Set(j, array[j], throwOnError: true);
// TODO if we could keep track of data descriptors and whether prototype chain is unchanged
// we could do faster direct write
// obj.Set(j, array[j], updateLength: true, throwOnError: true);
}

for (; j < len; ++j)
{
obj.DeletePropertyOrThrow(j);
Expand Down
1 change: 0 additions & 1 deletion Jint/Native/Object/ObjectInstance.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Runtime.CompilerServices;
using Jint.Collections;
Expand Down