Skip to content

Commit b2e1223

Browse files
robertsipkazherczeg
authored andcommitted
Improve performance of search in the list of the magic strings. (#1441)
JerryScript-DCO-1.0-Signed-off-by: Robert Sipka [email protected]
1 parent 97303eb commit b2e1223

File tree

3 files changed

+291
-239
lines changed

3 files changed

+291
-239
lines changed

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

+54-28
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,19 @@ 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 =
40+
static const lit_utf8_byte_t * const lit_magic_strings[] JERRY_CONST_DATA =
4141
{
42+
#define LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE(size, id)
4243
#define LIT_MAGIC_STRING_DEF(id, utf8_string) \
4344
(const lit_utf8_byte_t *) utf8_string,
4445
#include "lit-magic-strings.inc.h"
4546
#undef LIT_MAGIC_STRING_DEF
47+
#undef LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE
4648
};
4749

4850
JERRY_ASSERT (id < LIT_MAGIC_STRING__COUNT);
4951

50-
return magic_strings[id];
52+
return lit_magic_strings[id];
5153
} /* lit_get_magic_string_utf8 */
5254

5355
/**
@@ -60,17 +62,44 @@ lit_get_magic_string_size (lit_magic_string_id_t id) /**< magic string id */
6062
{
6163
static const lit_magic_size_t lit_magic_string_sizes[] JERRY_CONST_DATA =
6264
{
65+
#define LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE(size, id)
6366
#define LIT_MAGIC_STRING_DEF(id, utf8_string) \
6467
sizeof(utf8_string) - 1,
6568
#include "lit-magic-strings.inc.h"
6669
#undef LIT_MAGIC_STRING_DEF
70+
#undef LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE
6771
};
6872

6973
JERRY_ASSERT (id < LIT_MAGIC_STRING__COUNT);
7074

7175
return lit_magic_string_sizes[id];
7276
} /* lit_get_magic_string_size */
7377

78+
/**
79+
* Get the block start element with the given size from
80+
* the list of ECMA and implementation-defined magic string constants
81+
*
82+
* @return magic string id
83+
*/
84+
lit_magic_string_id_t
85+
lit_get_magic_string_size_block_start (lit_utf8_size_t size) /**< magic string size */
86+
{
87+
static const lit_magic_string_id_t const lit_magic_string_size_block_starts[] JERRY_CONST_DATA =
88+
{
89+
#define LIT_MAGIC_STRING_DEF(id, utf8_string)
90+
#define LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE(size, id) \
91+
id,
92+
#include "lit-magic-strings.inc.h"
93+
LIT_MAGIC_STRING__COUNT
94+
#undef LIT_MAGIC_STRING_DEF
95+
#undef LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE
96+
};
97+
98+
JERRY_ASSERT (size <= (sizeof (lit_magic_string_size_block_starts) / sizeof (lit_magic_string_id_t)));
99+
100+
return lit_magic_string_size_block_starts[size];
101+
} /* lit_get_magic_string_size_block_start */
102+
74103
/**
75104
* Get specified magic string as zero-terminated string from external table
76105
*
@@ -145,22 +174,36 @@ lit_is_utf8_string_magic (const lit_utf8_byte_t *string_p, /**< utf-8 string */
145174
lit_utf8_size_t string_size, /**< string size in bytes */
146175
lit_magic_string_id_t *out_id_p) /**< [out] magic string's id */
147176
{
148-
/* TODO: Improve performance of search */
177+
*out_id_p = LIT_MAGIC_STRING__COUNT;
149178

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))
179+
if (string_size > lit_get_magic_string_size (LIT_MAGIC_STRING__COUNT - 1))
153180
{
154-
if (lit_compare_utf8_string_and_magic_string (string_p, string_size, id))
155-
{
156-
*out_id_p = id;
181+
return false;
182+
}
183+
184+
int first = (int) lit_get_magic_string_size_block_start (string_size); /**< First magic string element */
185+
int last = (int) (lit_get_magic_string_size_block_start (string_size + 1) - 1); /**< Last magic string element */
186+
187+
while (first <= last)
188+
{
189+
int middle = ((first + last) / 2); /**< mid point of search */
190+
int compare = memcmp (lit_get_magic_string_utf8 (middle), string_p, string_size);
157191

192+
if (compare == 0)
193+
{
194+
*out_id_p = (lit_magic_string_id_t) middle;
158195
return true;
159196
}
197+
else if (compare > 0)
198+
{
199+
last = middle - 1;
200+
}
201+
else
202+
{
203+
first = middle + 1;
204+
}
160205
}
161206

162-
*out_id_p = LIT_MAGIC_STRING__COUNT;
163-
164207
return false;
165208
} /* lit_is_utf8_string_magic */
166209

@@ -194,23 +237,6 @@ bool lit_is_ex_utf8_string_magic (const lit_utf8_byte_t *string_p, /**< utf-8 st
194237
return false;
195238
} /* lit_is_ex_utf8_string_magic */
196239

197-
/**
198-
* Compare utf-8 string and magic string for equality
199-
*
200-
* @return true if strings are equal
201-
* false otherwise
202-
*/
203-
bool
204-
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 */
207-
{
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));
212-
} /* lit_compare_utf8_string_and_magic_string */
213-
214240
/**
215241
* Compare utf-8 string and external magic string for equality
216242
*

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
*/
2929
typedef enum
3030
{
31+
#define LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE(size, id)
3132
#define LIT_MAGIC_STRING_DEF(id, ascii_zt_string) \
3233
id,
3334
#include "lit-magic-strings.inc.h"
3435
#undef LIT_MAGIC_STRING_DEF
35-
36+
#undef LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE
3637
LIT_MAGIC_STRING__COUNT /**< number of magic strings */
3738
} lit_magic_string_id_t;
3839

@@ -45,17 +46,16 @@ extern uint32_t lit_get_magic_string_ex_count (void);
4546

4647
extern const lit_utf8_byte_t *lit_get_magic_string_utf8 (lit_magic_string_id_t);
4748
extern lit_utf8_size_t lit_get_magic_string_size (lit_magic_string_id_t);
49+
extern lit_magic_string_id_t lit_get_magic_string_size_block_start (lit_utf8_size_t);
4850

4951
extern const lit_utf8_byte_t *lit_get_magic_string_ex_utf8 (lit_magic_string_ex_id_t);
5052
extern lit_utf8_size_t lit_get_magic_string_ex_size (lit_magic_string_ex_id_t);
5153

5254
extern void lit_magic_strings_ex_set (const lit_utf8_byte_t **, uint32_t, const lit_utf8_size_t *);
5355

5456
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 *);
5657

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);
58+
extern bool lit_is_ex_utf8_string_magic (const lit_utf8_byte_t *, lit_utf8_size_t, lit_magic_string_ex_id_t *);
5959

6060
extern bool lit_compare_utf8_string_and_magic_string_ex (const lit_utf8_byte_t *, lit_utf8_size_t,
6161
lit_magic_string_ex_id_t);

0 commit comments

Comments
 (0)