-
Notifications
You must be signed in to change notification settings - Fork 682
Improve performance of search in the list of the magic strings. #1441
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
Improve performance of search in the list of the magic strings. #1441
Conversation
Impressive! |
fa390a4
to
8bf7ecd
Compare
* ascii and non-ascii groups. | ||
* These strings must be ascii strings and needs to be defined in | ||
* lexicographical order. If non-ascii strings will be ever needed, | ||
* a divider will be added to separate ascii and non-ascii groups. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need any dividers. So this could be removed from the comment.
} | ||
else if (id_size > string_size) | ||
{ | ||
last = middle -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space before 1
|
||
return true; | ||
int compare = memcmp (magic_strings[middle], string_p, string_size); | ||
if (compare == 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newline before if
if (compare == 0) | ||
{ | ||
lit_utf8_size_t id_size = lit_zt_utf8_string_size (magic_strings[middle]); | ||
if (id_size == string_size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would simply check that magic_strings[middle][string_size] == '\0' which is faster than getting the string size.
* ascii and non-ascii groups. | ||
* These strings must be ascii strings and needs to be defined in | ||
* lexicographical order. If non-ascii strings will be ever needed, | ||
* a divider will be added to separate ascii and non-ascii groups. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I would also add that NULL character cannot be part of magic strings, because it must be the terminator character of all magic strings.
8bf7ecd
to
9168e3d
Compare
Thanks, I've updated this patch. |
if (lit_compare_utf8_string_and_magic_string (string_p, string_size, id)) | ||
int middle = ((first + last) / 2); /**< mid point of search */ | ||
|
||
int compare = memcmp (magic_strings[middle], string_p, string_size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized that this is unsafe if the input string contains \0 which is allowed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should figure out something clever, not just a brute force size check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't you use 'strcmp' ?
@@ -145,22 +145,45 @@ lit_is_utf8_string_magic (const lit_utf8_byte_t *string_p, /**< utf-8 string */ | |||
lit_utf8_size_t string_size, /**< string size in bytes */ | |||
lit_magic_string_id_t *out_id_p) /**< [out] magic string's id */ | |||
{ | |||
/* TODO: Improve performance of search */ | |||
static const lit_utf8_byte_t * const magic_strings[] JERRY_CONST_DATA = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This array is duplicate of the array in lit_get_magic_string_utf8
. Will the compiler recognize that they are the same? This might increase the binary size if doesn't.
{ | ||
*out_id_p = id; | ||
if (magic_strings[middle][string_size] == '\0') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use 'strcmp', then we don't need this sanity check.
9168e3d
to
24ba199
Compare
Thanks for the comments, I've updated this patch rebased with master. |
*/ | ||
bool | ||
int | ||
lit_compare_utf8_string_and_magic_string (const lit_utf8_byte_t *string_p, /**< utf-8 string */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is a duplicate of 'strcmp', I don't see any difference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is two crucial difference, which requires the existence of this function:
- the input string can contains \0, which is allowed.
- The actual size of the input string is different from the received. We only extracts a part of this string, and received the size of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- All of the magic strings are ASCII and we use this to improve performance in various places of the code. This means magic string cannot contain '\0'.
- Use 'strncmp' then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The strncmp is not good either. Lets take for example the 'call' magic string. We received a string which start with a 'c' character, and the size is 1. The results will be zero, and in our cases it means we find a magic string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
8f2645d
to
9a480ac
Compare
LGTM |
I made some improvements and achieve similar performance and made it safer than the first draft.
|
df3be3c
to
11af13b
Compare
JerryScript-DCO-1.0-Signed-off-by: Robert Sipka [email protected]
11af13b
to
545d260
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still LGTM
Binary sizes (bytes)
7131243:152612
e38bf80:152580