Skip to content

Commit 0daa34b

Browse files
committed
Optimize ecma_string_get_array_index
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
1 parent 35c0869 commit 0daa34b

File tree

1 file changed

+61
-12
lines changed

1 file changed

+61
-12
lines changed

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

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -554,27 +554,76 @@ 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+
560+
if (type == ECMA_STRING_CONTAINER_UINT32_IN_DESC)
559561
{
560-
*out_index_p = str_p->u.uint32_number;
562+
index = str_p->u.uint32_number;
563+
}
564+
else if (type == ECMA_STRING_CONTAINER_MAGIC_STRING)
565+
{
566+
return false;
561567
}
562568
else
563569
{
564-
ecma_number_t num = ecma_string_to_number (str_p);
565-
*out_index_p = ecma_number_to_uint32 (num);
570+
lit_utf8_size_t size;
571+
const lit_utf8_byte_t *raw_str_p;
566572

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

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

572-
ecma_deref_ecma_string (to_uint32_to_string_p);
573-
}
586+
if (size > 10)
587+
{
588+
return false;
589+
}
590+
591+
if (size == 1 && *raw_str_p == LIT_CHAR_0)
592+
{
593+
*out_index_p = 0;
594+
return true;
595+
}
574596

575-
is_array_index = is_array_index && (*out_index_p != ECMA_MAX_VALUE_OF_VALID_ARRAY_INDEX);
597+
if (*raw_str_p <= LIT_CHAR_9 && *raw_str_p > LIT_CHAR_0)
598+
{
599+
index = (uint32_t) (*raw_str_p - LIT_CHAR_0);
600+
}
601+
else
602+
{
603+
return false;
604+
}
605+
606+
for (lit_utf8_size_t i = 1; i < size; i++)
607+
{
608+
if (raw_str_p[i] > LIT_CHAR_9 || raw_str_p[i] < LIT_CHAR_0)
609+
{
610+
return false;
611+
}
612+
613+
const uint32_t tmp = (index * 10) + (uint32_t) (raw_str_p[i] - LIT_CHAR_0);
614+
615+
if (unlikely (tmp < index))
616+
{
617+
/* Overflow */
618+
return false;
619+
}
620+
621+
index = tmp;
622+
}
623+
}
576624

577-
return is_array_index;
625+
*out_index_p = index;
626+
return index != ECMA_MAX_VALUE_OF_VALID_ARRAY_INDEX;
578627
} /* ecma_string_get_array_index */
579628

580629
/**

0 commit comments

Comments
 (0)