Skip to content

Commit 8f2645d

Browse files
committed
Improve performance of search in the list of the magic strings.
JerryScript-DCO-1.0-Signed-off-by: Robert Sipka [email protected]
1 parent 6ac0a39 commit 8f2645d

File tree

3 files changed

+257
-223
lines changed

3 files changed

+257
-223
lines changed

jerry-core/lit/lit-magic-strings.c

+50-26
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ lit_get_magic_string_ex_count (void)
3737
const lit_utf8_byte_t *
3838
lit_get_magic_string_utf8 (lit_magic_string_id_t id) /**< magic string id */
3939
{
40-
static const lit_utf8_byte_t * const magic_strings[] JERRY_CONST_DATA =
41-
{
42-
#define LIT_MAGIC_STRING_DEF(id, utf8_string) \
43-
(const lit_utf8_byte_t *) utf8_string,
44-
#include "lit-magic-strings.inc.h"
45-
#undef LIT_MAGIC_STRING_DEF
46-
};
47-
4840
JERRY_ASSERT (id < LIT_MAGIC_STRING__COUNT);
4941

5042
return magic_strings[id];
@@ -145,22 +137,32 @@ lit_is_utf8_string_magic (const lit_utf8_byte_t *string_p, /**< utf-8 string */
145137
lit_utf8_size_t string_size, /**< string size in bytes */
146138
lit_magic_string_id_t *out_id_p) /**< [out] magic string's id */
147139
{
148-
/* TODO: Improve performance of search */
140+
*out_id_p = LIT_MAGIC_STRING__COUNT;
149141

150-
for (lit_magic_string_id_t id = (lit_magic_string_id_t) 0;
151-
id < LIT_MAGIC_STRING__COUNT;
152-
id = (lit_magic_string_id_t) (id + 1))
142+
int first = 0; /**< First magic string element */
143+
int last = (int) (LIT_MAGIC_STRING__COUNT - 1); /**< Last magic string element */
144+
145+
while (first <= last)
153146
{
154-
if (lit_compare_utf8_string_and_magic_string (string_p, string_size, id))
155-
{
156-
*out_id_p = id;
147+
int middle = ((first + last) / 2); /**< mid point of search */
148+
149+
int compare = lit_compare_utf8_string_and_magic_string (string_p, string_size, magic_strings[middle]);
157150

151+
if (compare == 0)
152+
{
153+
*out_id_p = (lit_magic_string_id_t) middle;
158154
return true;
159155
}
156+
else if (compare < 0)
157+
{
158+
last = middle - 1;
159+
}
160+
else
161+
{
162+
first = middle + 1;
163+
}
160164
}
161165

162-
*out_id_p = LIT_MAGIC_STRING__COUNT;
163-
164166
return false;
165167
} /* lit_is_utf8_string_magic */
166168

@@ -197,18 +199,40 @@ bool lit_is_ex_utf8_string_magic (const lit_utf8_byte_t *string_p, /**< utf-8 st
197199
/**
198200
* Compare utf-8 string and magic string for equality
199201
*
200-
* @return true if strings are equal
201-
* false otherwise
202+
* @return 0, if areas are equal;
203+
* <0, if string's content is lexicographically less, than magic string's content;
204+
* >0, otherwise
202205
*/
203-
bool
206+
int
204207
lit_compare_utf8_string_and_magic_string (const lit_utf8_byte_t *string_p, /**< utf-8 string */
205-
lit_utf8_size_t string_size, /**< string size in bytes */
206-
lit_magic_string_id_t magic_string_id) /**< magic string's id */
208+
const lit_utf8_size_t buf_size, /**< string size */
209+
const lit_utf8_byte_t *magic_string_p) /**< magic string */
207210
{
208-
return lit_compare_utf8_strings (string_p,
209-
string_size,
210-
lit_get_magic_string_utf8 (magic_string_id),
211-
lit_get_magic_string_size (magic_string_id));
211+
lit_utf8_size_t position = 0;
212+
213+
while (position < buf_size)
214+
{
215+
if (magic_string_p[position] == '\0')
216+
{
217+
return 1;
218+
}
219+
220+
int diff = ((int) string_p[position]) - ((int) magic_string_p[position]);
221+
222+
if (diff)
223+
{
224+
return diff;
225+
}
226+
227+
position++;
228+
}
229+
230+
if (magic_string_p[position] == '\0')
231+
{
232+
return 0;
233+
}
234+
235+
return -1;
212236
} /* lit_compare_utf8_string_and_magic_string */
213237

214238
/**

jerry-core/lit/lit-magic-strings.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ typedef enum
3636
LIT_MAGIC_STRING__COUNT /**< number of magic strings */
3737
} lit_magic_string_id_t;
3838

39+
/**
40+
*
41+
* List of ECMA and implementation-defined magic string constants
42+
*/
43+
static const lit_utf8_byte_t * const magic_strings[] JERRY_CONST_DATA =
44+
{
45+
#define LIT_MAGIC_STRING_DEF(id, utf8_string) \
46+
(const lit_utf8_byte_t *) utf8_string,
47+
#include "lit-magic-strings.inc.h"
48+
#undef LIT_MAGIC_STRING_DEF
49+
};
50+
3951
/**
4052
* Identifiers of implementation-defined external magic string constants
4153
*/
@@ -52,11 +64,12 @@ extern lit_utf8_size_t lit_get_magic_string_ex_size (lit_magic_string_ex_id_t);
5264
extern void lit_magic_strings_ex_set (const lit_utf8_byte_t **, uint32_t, const lit_utf8_size_t *);
5365

5466
extern bool lit_is_utf8_string_magic (const lit_utf8_byte_t *, lit_utf8_size_t, lit_magic_string_id_t *);
55-
extern bool lit_is_ex_utf8_string_magic (const lit_utf8_byte_t *, lit_utf8_size_t, lit_magic_string_ex_id_t *);
5667

57-
extern bool lit_compare_utf8_string_and_magic_string (const lit_utf8_byte_t *, lit_utf8_size_t,
58-
lit_magic_string_id_t);
68+
extern bool lit_is_ex_utf8_string_magic (const lit_utf8_byte_t *, lit_utf8_size_t, lit_magic_string_ex_id_t *);
5969

70+
extern int lit_compare_utf8_string_and_magic_string (const lit_utf8_byte_t *,
71+
const lit_utf8_size_t,
72+
const lit_utf8_byte_t *magic_string_p);
6073
extern bool lit_compare_utf8_string_and_magic_string_ex (const lit_utf8_byte_t *, lit_utf8_size_t,
6174
lit_magic_string_ex_id_t);
6275

0 commit comments

Comments
 (0)