Skip to content

Commit cbf01e0

Browse files
committed
Use CESU-8 lookup table to improve lit_get_unicode_char_size_by_utf8_first_byte performance
JerryScript-DCO-1.0-Signed-off-by: Xin Hu [email protected]
1 parent 50d124b commit cbf01e0

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

jerry-core/lit/lit-strings.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,15 @@ lit_utf8_string_code_unit_at (const lit_utf8_byte_t *utf8_buf_p, /**< utf-8 stri
757757
return code_unit;
758758
} /* lit_utf8_string_code_unit_at */
759759

760+
/* CESU-8 number of bytes occupied lookup table */
761+
const lit_utf8_size_t table[]
762+
{
763+
1, 1, 1, 1, 1, 1, 1, 1,
764+
0, 0, 0, 0,
765+
2, 2,
766+
3, 0
767+
};
768+
760769
/**
761770
* Get CESU-8 encoded size of character
762771
*
@@ -765,19 +774,14 @@ lit_utf8_string_code_unit_at (const lit_utf8_byte_t *utf8_buf_p, /**< utf-8 stri
765774
lit_utf8_size_t
766775
lit_get_unicode_char_size_by_utf8_first_byte (const lit_utf8_byte_t first_byte) /**< buffer with characters */
767776
{
768-
if ((first_byte & LIT_UTF8_1_BYTE_MASK) == LIT_UTF8_1_BYTE_MARKER)
769-
{
770-
return 1;
771-
}
772-
else if ((first_byte & LIT_UTF8_2_BYTE_MASK) == LIT_UTF8_2_BYTE_MARKER)
773-
{
774-
return 2;
775-
}
776-
else
777-
{
778-
JERRY_ASSERT ((first_byte & LIT_UTF8_3_BYTE_MASK) == LIT_UTF8_3_BYTE_MARKER);
779-
return 3;
780-
}
777+
JERRY_ASSERT (((first_byte >> 4) == 0 ||
778+
(first_byte >> 4) == 1 || (first_byte >> 4) == 2 ||
779+
(first_byte >> 4) == 3 || (first_byte >> 4) == 4 ||
780+
(first_byte >> 4) == 5 || (first_byte >> 4) == 6 ||
781+
(first_byte >> 4) == 7 || (first_byte >> 4) == 12 ||
782+
(first_byte >> 4) == 13 || (first_byte >> 4) == 14));
783+
784+
return table[first_byte >> 4];
781785
} /* lit_get_unicode_char_size_by_utf8_first_byte */
782786

783787
/**

0 commit comments

Comments
 (0)