Array元素是String类型,优化toSorted速度

Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/IAWHSX

Signed-off-by: jiangmengyang <jiangmengyang3@huawei.com>
Change-Id: I7e0563846346f84718e0a1d8c6a3fc47a076ef80
This commit is contained in:
jiangmengyang 2024-10-12 11:04:29 +08:00
parent 9bfd506c07
commit b1e8ec8184
8 changed files with 97 additions and 5 deletions

View File

@ -1544,17 +1544,42 @@ GateRef BuiltinsArrayStubBuilder::DoSort(GateRef glue, GateRef receiver, GateRef
Bind(&afterGettingmiddleValue);
{
Label isInt(env);
Label notInt(env);
Label exchangeIndex(env);
GateRef middleVal = *middleValue;
GateRef presentVal = *presentValue;
BRANCH(LogicAndBuilder(env).And(TaggedIsInt(middleVal)).And(TaggedIsInt(presentVal)).Done(),
&isInt, slowPath);
DEFVARIABLE(compareResult, VariableType::INT32(), Int32(0));
GateRef intBool = LogicAndBuilder(env)
.And(TaggedIsInt(middleVal))
.And(TaggedIsInt(presentVal))
.Done();
BRANCH(intBool, &isInt, &notInt);
Bind(&isInt);
{
GateRef compareResult =
compareResult =
CallNGCRuntime(glue, RTSTUB_ID(FastArraySort), {*middleValue, *presentValue});
Jump(&exchangeIndex);
}
Bind(&notInt);
{
Label isString(env);
GateRef strBool = LogicAndBuilder(env)
.And(TaggedIsString(middleVal))
.And(TaggedIsString(presentVal))
.Done();
BRANCH(strBool, &isString, slowPath);
Bind(&isString);
{
compareResult = CallNGCRuntime(glue,
RTSTUB_ID(FastArraySortString), {glue, *middleValue, *presentValue});
Jump(&exchangeIndex);
}
}
Bind(&exchangeIndex);
{
Label less0(env);
Label greater0(env);
BRANCH(Int32LessThanOrEqual(compareResult, Int32(0)), &less0, &greater0);
BRANCH(Int32LessThanOrEqual(*compareResult, Int32(0)), &less0, &greater0);
Bind(&greater0);
{
endIndex = middleIndex;

View File

@ -1744,6 +1744,21 @@ DEF_CALL_SIGNATURE(FastArraySort)
callSign->SetTargetKind(CallSignature::TargetKind::RUNTIME_STUB_NO_GC);
}
DEF_CALL_SIGNATURE(FastArraySortString)
{
// 2 : 2 input parameters
CallSignature fastArraySortString("FastArraySortString", 0, 2,
ArgumentsOrder::DEFAULT_ORDER, VariableType::INT32());
*callSign = fastArraySortString;
std::array<VariableType, 3> params = { // 3 : 3 input parameters
VariableType::NATIVE_POINTER(),
VariableType::JS_ANY(),
VariableType::JS_ANY()
};
callSign->SetParameters(params.data());
callSign->SetGCLeafFunction(true);
callSign->SetTargetKind(CallSignature::TargetKind::RUNTIME_STUB_NO_GC);
}
DEF_CALL_SIGNATURE(StringToNumber)
{
// 4 : 4 input parameters

View File

@ -591,6 +591,7 @@ private:
V(NumberHelperStringToDouble) \
V(GetStringToListCacheArray) \
V(FastArraySort) \
V(FastArraySortString) \
V(StringToNumber) \
V(StringGetStart) \
V(StringGetEnd) \

View File

@ -185,6 +185,7 @@ namespace panda::ecmascript {
V(NumberHelperStringToDouble) \
V(GetStringToListCacheArray) \
V(FastArraySort) \
V(FastArraySortString) \
V(StringToNumber) \
V(StringGetStart) \
V(StringGetEnd) \

View File

@ -3851,6 +3851,15 @@ int RuntimeStubs::FastArraySort(JSTaggedType x, JSTaggedType y)
return JSTaggedValue::IntLexicographicCompare(JSTaggedValue(x), JSTaggedValue(y));
}
int RuntimeStubs::FastArraySortString(uintptr_t argGlue, JSTaggedValue x, JSTaggedValue y)
{
DISALLOW_GARBAGE_COLLECTION;
auto thread = JSThread::GlueToJSThread(argGlue);
JSHandle<EcmaString> valueX(thread, x);
JSHandle<EcmaString> valueY(thread, y);
return static_cast<int>(EcmaStringAccessor::Compare(thread->GetEcmaVM(), valueX, valueY));
}
DEF_RUNTIME_STUBS(LocaleCompareCacheable)
{
RUNTIME_STUBS_HEADER(LocaleCompareCacheable);

View File

@ -143,6 +143,7 @@ public:
static JSTaggedValue NumberHelperStringToDouble(EcmaString *str);
static JSTaggedValue GetStringToListCacheArray(uintptr_t argGlue);
static int FastArraySort(JSTaggedType x, JSTaggedType y);
static int FastArraySortString(uintptr_t argGlue, JSTaggedValue x, JSTaggedValue y);
static JSTaggedValue StringToNumber(JSTaggedType numberString, int32_t radix);
static void ArrayTrim(uintptr_t argGlue, TaggedArray *array, int64_t newLength);
static double TimeClip(double time);

View File

@ -340,4 +340,39 @@ arr.sort();
//for cmp return type is double
let arr34 = [2.63, 1.67];
print(arr34.sort((a, b)=> a - b));
print("sort Test Success!")
print("sort Test Success!");
// String toSorted
{
let array1 = new Array();
for (let i = 0; i < 1; i++) array1[i] = `string.${i}`;
print(array1.toSorted());
array1 = new Array();
for (let i = 0; i < 2; i++) array1[i] = `string.${i}`;
print(array1.toSorted());
array1 = new Array();
for (let i = 0; i < 2; i++) array1[i] = `string.${1 - i}`;
print(array1.toSorted());
array1 = new Array();
array1[0] = `a`;
array1[1] = `b`;
array1[2] = `c`;
array1[3] = `d`;
array1[4] = `e`;
array1[5] = `f`;
array1[6] = `g`;
print(array1.toSorted());
array1 = new Array();
array1[6] = `a`;
array1[5] = `b`;
array1[4] = `c`;
array1[3] = `d`;
array1[2] = `e`;
array1[1] = `f`;
array1[0] = `g`;
print(array1.toSorted());
}

View File

@ -54,3 +54,8 @@ This is not a TypedArray.
[{"name":"Eagle","grade":13},{"name":"Sam","grade":14},{"name":"Alex","grade":15},{"name":"Devlin","grade":15}]
1.67,2.63
sort Test Success!
string.0
string.0,string.1
string.0,string.1
a,b,c,d,e,f,g
a,b,c,d,e,f,g