Skip to content

Commit ae50973

Browse files
committed
Optimize ecma_string_get_array_index
JerryScript-DCO-1.0-Signed-off-by: D<C3><A1>niel B<C3><A1>tyai dbatyai@inf.u-szeged.hu
1 parent 35c0869 commit ae50973

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

jerry-core/ecma/base/ecma-helpers-string.c

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -554,27 +554,49 @@ bool
554554
ecma_string_get_array_index (const ecma_string_t *str_p, /**< ecma-string */
555555
uint32_t *out_index_p) /**< [out] index */
556556
{
557-
bool is_array_index = true;
558-
if (ECMA_STRING_GET_CONTAINER (str_p) == ECMA_STRING_CONTAINER_UINT32_IN_DESC)
557+
uint32_t index;
558+
const ecma_string_container_t type = ECMA_STRING_GET_CONTAINER (str_p);
559+
if (type == ECMA_STRING_CONTAINER_UINT32_IN_DESC)
559560
{
560-
*out_index_p = str_p->u.uint32_number;
561+
index = str_p->u.uint32_number;
562+
}
563+
else if (type == ECMA_STRING_CONTAINER_MAGIC_STRING)
564+
{
565+
return false;
561566
}
562567
else
563568
{
564-
ecma_number_t num = ecma_string_to_number (str_p);
565-
*out_index_p = ecma_number_to_uint32 (num);
569+
lit_utf8_size_t size;
570+
const lit_utf8_byte_t *raw_str_p;
566571

567-
ecma_string_t *to_uint32_to_string_p = ecma_new_ecma_string_from_uint32 (*out_index_p);
572+
if (unlikely (type == ECMA_STRING_CONTAINER_MAGIC_STRING_EX))
573+
{
574+
size = lit_get_magic_string_ex_size (str_p->u.magic_string_ex_id);
575+
raw_str_p = lit_get_magic_string_ex_utf8 (str_p->u.magic_string_ex_id);
576+
}
577+
else
578+
{
579+
JERRY_ASSERT(type == ECMA_STRING_CONTAINER_HEAP_UTF8_STRING);
568580

569-
is_array_index = ecma_compare_ecma_strings (str_p,
570-
to_uint32_to_string_p);
581+
size = str_p->u.utf8_string.size;
582+
raw_str_p = (const lit_utf8_byte_t *) (str_p + 1);
583+
}
571584

572-
ecma_deref_ecma_string (to_uint32_to_string_p);
573-
}
585+
index = 0;
586+
for (lit_utf8_size_t i = 0; i < size; i++)
587+
{
588+
if (raw_str_p[i] > LIT_CHAR_9 || raw_str_p[i] < LIT_CHAR_0)
589+
{
590+
return false;
591+
}
574592

575-
is_array_index = is_array_index && (*out_index_p != ECMA_MAX_VALUE_OF_VALID_ARRAY_INDEX);
593+
index *= 10;
594+
index += (uint32_t) (raw_str_p[i] - LIT_CHAR_0);
595+
}
596+
}
576597

577-
return is_array_index;
598+
*out_index_p = index;
599+
return index != ECMA_MAX_VALUE_OF_VALID_ARRAY_INDEX;
578600
} /* ecma_string_get_array_index */
579601

580602
/**

0 commit comments

Comments
 (0)