Skip to content

Commit

Permalink
Improve ecma_string_get_chars related code
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
lygstate committed Dec 17, 2024
1 parent d2d30df commit b9c0a58
Show file tree
Hide file tree
Showing 22 changed files with 230 additions and 351 deletions.
26 changes: 10 additions & 16 deletions jerry-core/api/jerry-snapshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,19 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co

lit_utf8_size_t pattern_size = 0;

ECMA_STRING_TO_UTF8_STRING (pattern_string_p, buffer_p, buffer_size);
ECMA_STRING_TO_UTF8_STRING (pattern_string_p, buffer);

pattern_size = buffer_size;
pattern_size = buffer.size;

if (!snapshot_write_to_buffer_by_offset (snapshot_buffer_p,
snapshot_buffer_size,
&globals_p->snapshot_buffer_write_offset,
buffer_p,
buffer_size))
buffer.ptr,
buffer.size))
{
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, error_buffer_too_small_p);
/* cannot return inside ECMA_FINALIZE_UTF8_STRING */
}

ECMA_FINALIZE_UTF8_STRING (buffer_p, buffer_size);

if (!ecma_is_value_empty (globals_p->snapshot_error))
{
return 0;
Expand Down Expand Up @@ -1484,15 +1481,13 @@ jerry_append_ecma_string_to_buffer (uint8_t *buffer_p, /**< buffer */
uint8_t *buffer_end_p, /**< the end of the buffer */
ecma_string_t *string_p) /**< ecma-string */
{
ECMA_STRING_TO_UTF8_STRING (string_p, str_buffer_p, str_buffer_size);
ECMA_STRING_TO_UTF8_STRING (string_p, str_buffer);

/* Append the string to the buffer. */
uint8_t *new_buffer_p = jerry_append_chars_to_buffer (buffer_p,
buffer_end_p,
(const jerry_char_t *) str_buffer_p,
(jerry_size_t) str_buffer_size);

ECMA_FINALIZE_UTF8_STRING (str_buffer_p, str_buffer_size);
(const jerry_char_t *) str_buffer.ptr,
(jerry_size_t) str_buffer.size);

return new_buffer_p;
} /* jerry_append_ecma_string_to_buffer */
Expand Down Expand Up @@ -1623,10 +1618,10 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
for (lit_utf8_size_t i = 0; i < literal_count; i++)
{
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" \""));
ECMA_STRING_TO_UTF8_STRING (literal_array[i], str_buffer_p, str_buffer_size);
for (lit_utf8_size_t j = 0; j < str_buffer_size; j++)
ECMA_STRING_TO_UTF8_STRING (literal_array[i], str_buffer);
for (lit_utf8_size_t j = 0; j < str_buffer.size; j++)
{
uint8_t byte = str_buffer_p[j];
uint8_t byte = str_buffer.ptr[j];
if (byte < 32 || byte > 127)
{
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\\x"));
Expand All @@ -1645,7 +1640,6 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
}
}

ECMA_FINALIZE_UTF8_STRING (str_buffer_p, str_buffer_size);
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\""));

if (i < literal_count - 1)
Expand Down
26 changes: 10 additions & 16 deletions jerry-core/api/jerryscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,11 @@ jerry_parse_common (void *source_p, /**< script source */
if ((JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED) && options_p != NULL
&& (options_p->options & JERRY_PARSE_HAS_SOURCE_NAME) && ecma_is_value_string (options_p->source_name))
{
ECMA_STRING_TO_UTF8_STRING (ecma_get_string_from_value (options_p->source_name),
source_name_start_p,
source_name_size);
ECMA_STRING_TO_UTF8_STRING (ecma_get_string_from_value (options_p->source_name), source_name_start);
jerry_debugger_send_string (JERRY_DEBUGGER_SOURCE_CODE_NAME,
JERRY_DEBUGGER_NO_SUBTYPE,
source_name_start_p,
source_name_size);
ECMA_FINALIZE_UTF8_STRING (source_name_start_p, source_name_size);
source_name_start.ptr,
source_name_start.size);
}
#endif /* JERRY_DEBUGGER */

Expand Down Expand Up @@ -891,12 +888,12 @@ jerry_native_module (jerry_native_module_evaluate_cb_t callback, /**< evaluation

bool valid_identifier = false;

ECMA_STRING_TO_UTF8_STRING (name_str_p, name_start_p, name_size);
ECMA_STRING_TO_UTF8_STRING (name_str_p, name_start);

if (name_size > 0)
if (name_start.size > 0)
{
const lit_utf8_byte_t *name_p = name_start_p;
const lit_utf8_byte_t *name_end_p = name_start_p + name_size;
const lit_utf8_byte_t *name_p = name_start.ptr;
const lit_utf8_byte_t *name_end_p = name_start.ptr + name_start.size;
lit_code_point_t code_point;

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

ECMA_FINALIZE_UTF8_STRING (name_start_p, name_size);

if (!valid_identifier)
{
ecma_deref_object (scope_p);
Expand Down Expand Up @@ -3001,10 +2996,10 @@ jerry_string_iterate (const jerry_value_t value,
}

ecma_string_t *str_p = ecma_get_string_from_value (value);
ECMA_STRING_TO_UTF8_STRING (str_p, buffer_p, buffer_size);
ECMA_STRING_TO_UTF8_STRING (str_p, buffer);

const lit_utf8_byte_t *current_p = buffer_p;
const lit_utf8_byte_t *end_p = buffer_p + buffer_size;
const lit_utf8_byte_t *current_p = buffer.ptr;
const lit_utf8_byte_t *end_p = buffer.ptr + buffer.size;

switch (encoding)
{
Expand Down Expand Up @@ -3048,7 +3043,6 @@ jerry_string_iterate (const jerry_value_t value,
break;
}
}
ECMA_FINALIZE_UTF8_STRING (buffer_p, buffer_size);
} /* jerry_string_iterate */

/**
Expand Down
20 changes: 7 additions & 13 deletions jerry-core/debugger/debugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ jerry_debugger_copy_variables_to_string_message (uint8_t variable_type, /**< typ
const size_t max_byte_count = JERRY_DEBUGGER_SEND_MAX (uint8_t);
const size_t max_message_size = JERRY_DEBUGGER_SEND_SIZE (max_byte_count, uint8_t);

ECMA_STRING_TO_UTF8_STRING (value_str, str_buff, str_buff_size);
ECMA_STRING_TO_UTF8_STRING (value_str, str_buff);

size_t str_size = 0;
size_t str_limit = 255;
Expand Down Expand Up @@ -373,7 +373,7 @@ jerry_debugger_copy_variables_to_string_message (uint8_t variable_type, /**< typ
}
else
{
str_size = (str_buff_size > str_limit) ? str_limit : str_buff_size;
str_size = (str_buff.size > str_limit) ? str_limit : str_buff.size;
}

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

while (str_size > free_bytes)
{
Expand All @@ -409,8 +409,6 @@ jerry_debugger_copy_variables_to_string_message (uint8_t variable_type, /**< typ
}
}

ECMA_FINALIZE_UTF8_STRING (str_buff, str_buff_size);

return result;
} /* jerry_debugger_copy_variables_to_string_message */

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

ecma_string_t *string_p = ecma_get_string_from_value (message);

ECMA_STRING_TO_UTF8_STRING (string_p, buffer_p, buffer_size);
jerry_debugger_send_string (JERRY_DEBUGGER_EVAL_RESULT, type, buffer_p, buffer_size);
ECMA_FINALIZE_UTF8_STRING (buffer_p, buffer_size);
ECMA_STRING_TO_UTF8_STRING (string_p, buffer);
jerry_debugger_send_string (JERRY_DEBUGGER_EVAL_RESULT, type, buffer.ptr, buffer.size);

ecma_free_value (message);

Expand Down Expand Up @@ -1525,12 +1522,9 @@ jerry_debugger_send_exception_string (ecma_value_t exception_value)
string_p = ecma_op_to_string (exception_value);
}

ECMA_STRING_TO_UTF8_STRING (string_p, string_data_p, string_size);

bool result =
jerry_debugger_send_string (JERRY_DEBUGGER_EXCEPTION_STR, JERRY_DEBUGGER_NO_SUBTYPE, string_data_p, string_size);
ECMA_STRING_TO_UTF8_STRING (string_p, str);

ECMA_FINALIZE_UTF8_STRING (string_data_p, string_size);
bool result = jerry_debugger_send_string (JERRY_DEBUGGER_EXCEPTION_STR, JERRY_DEBUGGER_NO_SUBTYPE, str.ptr, str.size);

ecma_deref_ecma_string (string_p);
return result;
Expand Down
Loading

0 comments on commit b9c0a58

Please sign in to comment.