Skip to content

Commit b9c0a58

Browse files
committed
Improve ecma_string_get_chars related code
Introduce lit_utf8_string_t and use it to reduce heap allocation and improve performance Mark ECMA_STRING_TO_UTF8_STRING/ECMA_STRING_TO_UTF8_STRING_AND_LENGTH do not use heap allocation Now with lit_utf8_string_t, ECMA_STRING_TO_UTF8_STRING and ECMA_STRING_TO_UTF8_STRING_AND_LENGTH are simplified. The parameters passed to ECMA_STRING_TO_UTF8_STRING and ECMA_STRING_TO_UTF8_STRING_AND_LENGTH are reduced and easier to call The function prototype of ecma_string_get_chars are simplified as: lit_utf8_size_t ecma_string_get_chars (const ecma_string_t *string_p, lit_utf8_string_t *string_out_p, lit_utf8_byte_t *uint32_buff_p, uint8_t *flags_p); The macro prototype of ECMA_STRING_TO_UTF8_STRING and ECMA_STRING_TO_UTF8_STRING_AND_LENGTH are simplified as: #define ECMA_STRING_TO_UTF8_STRING(ecma_str_ptr, /**< ecma string pointer */ \ utf8_str) /**< [out] lit_utf8_string_t to get */ \ #define ECMA_STRING_TO_UTF8_STRING_AND_LENGTH(ecma_str_ptr, /**< ecma string pointer */ \ utf8_str) /**< [out] lit_utf8_string_t to get */ \ Now the parameters are reduced. Aslo the calling to ecma_string_get_chars are largely reduced, in most case, ECMA_STRING_TO_UTF8_STRING/ECMA_STRING_TO_UTF8_STRING_AND_LENGTH is enough. Because the heap allocation are reduced, the performance should be improved. And in most case, we only use ECMA_STRING_TO_UTF8_STRING, only when the length is needed, the ECMA_STRING_TO_UTF8_STRING_AND_LENGTH is called, ECMA_STRING_TO_UTF8_STRING_AND_LENGTH needs to calculate the length that's very slow. ECMA_FINALIZE_UTF8_STRING is removed, if we have heap allocation situation, use ecma_string_get_chars instead and free it manually JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo [email protected]
1 parent d2d30df commit b9c0a58

22 files changed

+230
-351
lines changed

jerry-core/api/jerry-snapshot.c

+10-16
Original file line numberDiff line numberDiff line change
@@ -191,22 +191,19 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co
191191

192192
lit_utf8_size_t pattern_size = 0;
193193

194-
ECMA_STRING_TO_UTF8_STRING (pattern_string_p, buffer_p, buffer_size);
194+
ECMA_STRING_TO_UTF8_STRING (pattern_string_p, buffer);
195195

196-
pattern_size = buffer_size;
196+
pattern_size = buffer.size;
197197

198198
if (!snapshot_write_to_buffer_by_offset (snapshot_buffer_p,
199199
snapshot_buffer_size,
200200
&globals_p->snapshot_buffer_write_offset,
201-
buffer_p,
202-
buffer_size))
201+
buffer.ptr,
202+
buffer.size))
203203
{
204204
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, error_buffer_too_small_p);
205-
/* cannot return inside ECMA_FINALIZE_UTF8_STRING */
206205
}
207206

208-
ECMA_FINALIZE_UTF8_STRING (buffer_p, buffer_size);
209-
210207
if (!ecma_is_value_empty (globals_p->snapshot_error))
211208
{
212209
return 0;
@@ -1484,15 +1481,13 @@ jerry_append_ecma_string_to_buffer (uint8_t *buffer_p, /**< buffer */
14841481
uint8_t *buffer_end_p, /**< the end of the buffer */
14851482
ecma_string_t *string_p) /**< ecma-string */
14861483
{
1487-
ECMA_STRING_TO_UTF8_STRING (string_p, str_buffer_p, str_buffer_size);
1484+
ECMA_STRING_TO_UTF8_STRING (string_p, str_buffer);
14881485

14891486
/* Append the string to the buffer. */
14901487
uint8_t *new_buffer_p = jerry_append_chars_to_buffer (buffer_p,
14911488
buffer_end_p,
1492-
(const jerry_char_t *) str_buffer_p,
1493-
(jerry_size_t) str_buffer_size);
1494-
1495-
ECMA_FINALIZE_UTF8_STRING (str_buffer_p, str_buffer_size);
1489+
(const jerry_char_t *) str_buffer.ptr,
1490+
(jerry_size_t) str_buffer.size);
14961491

14971492
return new_buffer_p;
14981493
} /* jerry_append_ecma_string_to_buffer */
@@ -1623,10 +1618,10 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
16231618
for (lit_utf8_size_t i = 0; i < literal_count; i++)
16241619
{
16251620
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" \""));
1626-
ECMA_STRING_TO_UTF8_STRING (literal_array[i], str_buffer_p, str_buffer_size);
1627-
for (lit_utf8_size_t j = 0; j < str_buffer_size; j++)
1621+
ECMA_STRING_TO_UTF8_STRING (literal_array[i], str_buffer);
1622+
for (lit_utf8_size_t j = 0; j < str_buffer.size; j++)
16281623
{
1629-
uint8_t byte = str_buffer_p[j];
1624+
uint8_t byte = str_buffer.ptr[j];
16301625
if (byte < 32 || byte > 127)
16311626
{
16321627
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\\x"));
@@ -1645,7 +1640,6 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
16451640
}
16461641
}
16471642

1648-
ECMA_FINALIZE_UTF8_STRING (str_buffer_p, str_buffer_size);
16491643
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\""));
16501644

16511645
if (i < literal_count - 1)

jerry-core/api/jerryscript.c

+10-16
Original file line numberDiff line numberDiff line change
@@ -384,14 +384,11 @@ jerry_parse_common (void *source_p, /**< script source */
384384
if ((JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED) && options_p != NULL
385385
&& (options_p->options & JERRY_PARSE_HAS_SOURCE_NAME) && ecma_is_value_string (options_p->source_name))
386386
{
387-
ECMA_STRING_TO_UTF8_STRING (ecma_get_string_from_value (options_p->source_name),
388-
source_name_start_p,
389-
source_name_size);
387+
ECMA_STRING_TO_UTF8_STRING (ecma_get_string_from_value (options_p->source_name), source_name_start);
390388
jerry_debugger_send_string (JERRY_DEBUGGER_SOURCE_CODE_NAME,
391389
JERRY_DEBUGGER_NO_SUBTYPE,
392-
source_name_start_p,
393-
source_name_size);
394-
ECMA_FINALIZE_UTF8_STRING (source_name_start_p, source_name_size);
390+
source_name_start.ptr,
391+
source_name_start.size);
395392
}
396393
#endif /* JERRY_DEBUGGER */
397394

@@ -891,12 +888,12 @@ jerry_native_module (jerry_native_module_evaluate_cb_t callback, /**< evaluation
891888

892889
bool valid_identifier = false;
893890

894-
ECMA_STRING_TO_UTF8_STRING (name_str_p, name_start_p, name_size);
891+
ECMA_STRING_TO_UTF8_STRING (name_str_p, name_start);
895892

896-
if (name_size > 0)
893+
if (name_start.size > 0)
897894
{
898-
const lit_utf8_byte_t *name_p = name_start_p;
899-
const lit_utf8_byte_t *name_end_p = name_start_p + name_size;
895+
const lit_utf8_byte_t *name_p = name_start.ptr;
896+
const lit_utf8_byte_t *name_end_p = name_start.ptr + name_start.size;
900897
lit_code_point_t code_point;
901898

902899
lit_utf8_size_t size = lit_read_code_point_from_cesu8 (name_p, name_end_p, &code_point);
@@ -922,8 +919,6 @@ jerry_native_module (jerry_native_module_evaluate_cb_t callback, /**< evaluation
922919
}
923920
}
924921

925-
ECMA_FINALIZE_UTF8_STRING (name_start_p, name_size);
926-
927922
if (!valid_identifier)
928923
{
929924
ecma_deref_object (scope_p);
@@ -3001,10 +2996,10 @@ jerry_string_iterate (const jerry_value_t value,
30012996
}
30022997

30032998
ecma_string_t *str_p = ecma_get_string_from_value (value);
3004-
ECMA_STRING_TO_UTF8_STRING (str_p, buffer_p, buffer_size);
2999+
ECMA_STRING_TO_UTF8_STRING (str_p, buffer);
30053000

3006-
const lit_utf8_byte_t *current_p = buffer_p;
3007-
const lit_utf8_byte_t *end_p = buffer_p + buffer_size;
3001+
const lit_utf8_byte_t *current_p = buffer.ptr;
3002+
const lit_utf8_byte_t *end_p = buffer.ptr + buffer.size;
30083003

30093004
switch (encoding)
30103005
{
@@ -3048,7 +3043,6 @@ jerry_string_iterate (const jerry_value_t value,
30483043
break;
30493044
}
30503045
}
3051-
ECMA_FINALIZE_UTF8_STRING (buffer_p, buffer_size);
30523046
} /* jerry_string_iterate */
30533047

30543048
/**

jerry-core/debugger/debugger.c

+7-13
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ jerry_debugger_copy_variables_to_string_message (uint8_t variable_type, /**< typ
335335
const size_t max_byte_count = JERRY_DEBUGGER_SEND_MAX (uint8_t);
336336
const size_t max_message_size = JERRY_DEBUGGER_SEND_SIZE (max_byte_count, uint8_t);
337337

338-
ECMA_STRING_TO_UTF8_STRING (value_str, str_buff, str_buff_size);
338+
ECMA_STRING_TO_UTF8_STRING (value_str, str_buff);
339339

340340
size_t str_size = 0;
341341
size_t str_limit = 255;
@@ -373,7 +373,7 @@ jerry_debugger_copy_variables_to_string_message (uint8_t variable_type, /**< typ
373373
}
374374
else
375375
{
376-
str_size = (str_buff_size > str_limit) ? str_limit : str_buff_size;
376+
str_size = (str_buff.size > str_limit) ? str_limit : str_buff.size;
377377
}
378378

379379
message_string_p->string[*buffer_pos] = (uint8_t) str_size;
@@ -384,7 +384,7 @@ jerry_debugger_copy_variables_to_string_message (uint8_t variable_type, /**< typ
384384
if (result)
385385
{
386386
size_t free_bytes = max_byte_count - *buffer_pos;
387-
const uint8_t *string_p = str_buff;
387+
const uint8_t *string_p = str_buff.ptr;
388388

389389
while (str_size > free_bytes)
390390
{
@@ -409,8 +409,6 @@ jerry_debugger_copy_variables_to_string_message (uint8_t variable_type, /**< typ
409409
}
410410
}
411411

412-
ECMA_FINALIZE_UTF8_STRING (str_buff, str_buff_size);
413-
414412
return result;
415413
} /* jerry_debugger_copy_variables_to_string_message */
416414

@@ -614,9 +612,8 @@ jerry_debugger_send_eval (const lit_utf8_byte_t *eval_string_p, /**< evaluated s
614612

615613
ecma_string_t *string_p = ecma_get_string_from_value (message);
616614

617-
ECMA_STRING_TO_UTF8_STRING (string_p, buffer_p, buffer_size);
618-
jerry_debugger_send_string (JERRY_DEBUGGER_EVAL_RESULT, type, buffer_p, buffer_size);
619-
ECMA_FINALIZE_UTF8_STRING (buffer_p, buffer_size);
615+
ECMA_STRING_TO_UTF8_STRING (string_p, buffer);
616+
jerry_debugger_send_string (JERRY_DEBUGGER_EVAL_RESULT, type, buffer.ptr, buffer.size);
620617

621618
ecma_free_value (message);
622619

@@ -1525,12 +1522,9 @@ jerry_debugger_send_exception_string (ecma_value_t exception_value)
15251522
string_p = ecma_op_to_string (exception_value);
15261523
}
15271524

1528-
ECMA_STRING_TO_UTF8_STRING (string_p, string_data_p, string_size);
1529-
1530-
bool result =
1531-
jerry_debugger_send_string (JERRY_DEBUGGER_EXCEPTION_STR, JERRY_DEBUGGER_NO_SUBTYPE, string_data_p, string_size);
1525+
ECMA_STRING_TO_UTF8_STRING (string_p, str);
15321526

1533-
ECMA_FINALIZE_UTF8_STRING (string_data_p, string_size);
1527+
bool result = jerry_debugger_send_string (JERRY_DEBUGGER_EXCEPTION_STR, JERRY_DEBUGGER_NO_SUBTYPE, str.ptr, str.size);
15341528

15351529
ecma_deref_ecma_string (string_p);
15361530
return result;

0 commit comments

Comments
 (0)