Skip to content

Commit 9843754

Browse files
committed
Introduce JERRY_ZSTR_ARG macro to avoid call strlen on string literal.
By doing this, jerryx_print_string is avoided and removed JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo [email protected]
1 parent 43f5026 commit 9843754

File tree

9 files changed

+41
-52
lines changed

9 files changed

+41
-52
lines changed

jerry-core/api/jerry-snapshot.c

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,22 +1455,17 @@ jerry_save_literals_sort (ecma_string_t *literals[], /**< array of literals */
14551455
static uint8_t *
14561456
jerry_append_chars_to_buffer (uint8_t *buffer_p, /**< buffer */
14571457
uint8_t *buffer_end_p, /**< the end of the buffer */
1458-
const char *chars, /**< string */
1459-
lit_utf8_size_t string_size) /**< string size */
1458+
const jerry_char_t *chars, /**< string */
1459+
jerry_size_t string_size) /**< string size */
14601460
{
14611461
if (buffer_p > buffer_end_p)
14621462
{
14631463
return buffer_p;
14641464
}
14651465

1466-
if (string_size == 0)
1467-
{
1468-
string_size = (lit_utf8_size_t) strlen (chars);
1469-
}
1470-
14711466
if (buffer_p + string_size <= buffer_end_p)
14721467
{
1473-
memcpy ((char *) buffer_p, chars, string_size);
1468+
memcpy ((char *) buffer_p, (const char *) chars, string_size);
14741469

14751470
return buffer_p + string_size;
14761471
}
@@ -1609,26 +1604,27 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
16091604
if (is_c_format)
16101605
{
16111606
/* Save literal count. */
1612-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "jerry_length_t literal_count = ", 0);
1607+
lit_buf_p =
1608+
jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("jerry_length_t literal_count = "));
16131609

16141610
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count);
16151611

16161612
/* Save the array of literals. */
1617-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ";\n\njerry_char_t *literals[", 0);
1613+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (";\n\njerry_char_t *literals["));
16181614

16191615
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count);
1620-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "] =\n{\n", 0);
1616+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("] =\n{\n"));
16211617

16221618
for (lit_utf8_size_t i = 0; i < literal_count; i++)
16231619
{
1624-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " \"", 0);
1620+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" \""));
16251621
ECMA_STRING_TO_UTF8_STRING (literal_array[i], str_buffer_p, str_buffer_size);
16261622
for (lit_utf8_size_t j = 0; j < str_buffer_size; j++)
16271623
{
16281624
uint8_t byte = str_buffer_p[j];
16291625
if (byte < 32 || byte > 127)
16301626
{
1631-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\\x", 0);
1627+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\\x"));
16321628
ecma_char_t hex_digit = (ecma_char_t) (byte >> 4);
16331629
*lit_buf_p++ = (lit_utf8_byte_t) ((hex_digit > 9) ? (hex_digit + ('A' - 10)) : (hex_digit + '0'));
16341630
hex_digit = (lit_utf8_byte_t) (byte & 0xf);
@@ -1645,20 +1641,21 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
16451641
}
16461642

16471643
ECMA_FINALIZE_UTF8_STRING (str_buffer_p, str_buffer_size);
1648-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\"", 0);
1644+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\""));
16491645

16501646
if (i < literal_count - 1)
16511647
{
1652-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ",", 0);
1648+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (","));
16531649
}
16541650

1655-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\n", 0);
1651+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\n"));
16561652
}
16571653

1658-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "};\n\njerry_length_t literal_sizes[", 0);
1654+
lit_buf_p =
1655+
jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("};\n\njerry_length_t literal_sizes["));
16591656

16601657
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count);
1661-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "] =\n{\n", 0);
1658+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("] =\n{\n"));
16621659
}
16631660

16641661
/* Save the literal sizes respectively. */
@@ -1668,35 +1665,35 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
16681665

16691666
if (is_c_format)
16701667
{
1671-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " ", 0);
1668+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" "));
16721669
}
16731670

16741671
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, str_size);
1675-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " ", 0);
1672+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" "));
16761673

16771674
if (is_c_format)
16781675
{
16791676
/* Show the given string as a comment. */
1680-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "/* ", 0);
1677+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("/* "));
16811678
lit_buf_p = jerry_append_ecma_string_to_buffer (lit_buf_p, buffer_end_p, literal_array[i]);
1682-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " */", 0);
1679+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" */"));
16831680

16841681
if (i < literal_count - 1)
16851682
{
1686-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ",", 0);
1683+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (","));
16871684
}
16881685
}
16891686
else
16901687
{
16911688
lit_buf_p = jerry_append_ecma_string_to_buffer (lit_buf_p, buffer_end_p, literal_array[i]);
16921689
}
16931690

1694-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\n", 0);
1691+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\n"));
16951692
}
16961693

16971694
if (is_c_format)
16981695
{
1699-
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "};\n", 0);
1696+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("};\n"));
17001697
}
17011698

17021699
JMEM_FINALIZE_LOCAL_ARRAY (literal_array);

jerry-core/include/jerryscript-types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,11 @@ typedef void (*jerry_arraybuffer_free_cb_t) (jerry_arraybuffer_type_t buffer_typ
856856
void *arraybuffer_user_p,
857857
void *user_p);
858858

859+
/**
860+
* Helper to expand string literal to (string pointer, string size) arg pair.
861+
*/
862+
#define JERRY_ZSTR_ARG(str) ((const jerry_char_t *) (str)), (sizeof (str) - 1)
863+
859864
/**
860865
* @}
861866
*/

jerry-ext/include/jerryscript-ext/print.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ JERRY_C_API_BEGIN
2424
jerry_value_t jerryx_print_value (const jerry_value_t value);
2525
void jerryx_print_byte (jerry_char_t ch);
2626
void jerryx_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size);
27-
void jerryx_print_string (const char *str_p);
2827
void jerryx_print_backtrace (unsigned depth);
2928
void jerryx_print_unhandled_exception (jerry_value_t exception);
3029
void jerryx_print_unhandled_rejection (jerry_value_t exception);

jerry-ext/include/jerryscript-ext/repl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
JERRY_C_API_BEGIN
2222

23-
void jerryx_repl (const char* prompt_p);
23+
void jerryx_repl (const jerry_char_t *prompt_p, jerry_size_t prompt_size);
2424

2525
JERRY_C_API_END
2626

jerry-ext/util/print.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jerryx_buffered_print (uint32_t value, void *user_p)
6161
jerryx_print_buffer (buffer_p->data, buffer_p->index);
6262
buffer_p->index = 0;
6363

64-
jerryx_print_string ("\\u0000");
64+
jerryx_print_buffer (JERRY_ZSTR_ARG ("\\u0000"));
6565
return;
6666
}
6767

@@ -140,24 +140,6 @@ jerryx_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size)
140140
#endif /* JERRY_DEBUGGER */
141141
} /* jerryx_print_buffer */
142142

143-
/**
144-
* Print a zero-terminated string to standard output, also sending it to the debugger, if connected.
145-
*
146-
* @param buffer_p: inptut string buffer
147-
* @param buffer_size: size of the string
148-
*/
149-
void
150-
jerryx_print_string (const char *str_p)
151-
{
152-
const jerry_char_t *buffer_p = (jerry_char_t *) str_p;
153-
jerry_size_t buffer_size = (jerry_size_t) (strlen (str_p));
154-
155-
jerry_port_print_buffer (buffer_p, buffer_size);
156-
#if JERRY_DEBUGGER
157-
jerry_debugger_send_output (buffer_p, buffer_size);
158-
#endif /* JERRY_DEBUGGER */
159-
} /* jerryx_print_string */
160-
161143
/**
162144
* Print backtrace as log messages up to a specific depth.
163145
*

jerry-ext/util/repl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
#include "jerryscript-ext/print.h"
2525

2626
void
27-
jerryx_repl (const char *prompt_p)
27+
jerryx_repl (const jerry_char_t *prompt_p, jerry_size_t prompt_size)
2828
{
2929
jerry_value_t result;
3030

3131
while (true)
3232
{
33-
jerryx_print_string (prompt_p);
33+
jerryx_print_buffer (prompt_p, prompt_size);
3434
fflush (stdout);
3535

3636
jerry_size_t length;

jerry-main/main-desktop.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,14 @@ main (int argc, char **argv)
222222
}
223223
else if (arguments.source_count == 0)
224224
{
225-
const char *prompt_p = (arguments.option_flags & OPT_FLAG_NO_PROMPT) ? "" : "jerry> ";
226-
jerryx_repl (prompt_p);
225+
if ((arguments.option_flags & OPT_FLAG_NO_PROMPT))
226+
{
227+
jerryx_repl (JERRY_ZSTR_ARG (""));
228+
}
229+
else
230+
{
231+
jerryx_repl (JERRY_ZSTR_ARG ("jerry> "));
232+
}
227233
}
228234

229235
result = jerry_run_jobs ();

targets/os/nuttx/jerry-main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ jerry_main (int argc, char *argv[])
206206

207207
if (files_counter == 0)
208208
{
209-
jerryx_repl ("jerry> ");
209+
jerryx_repl (JERRY_ZSTR_ARG ("jerry> "));
210210
}
211211
else
212212
{

targets/os/zephyr/src/jerry-main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ main (void)
4949
jerry_init (JERRY_INIT_EMPTY);
5050
jerryx_register_global ("print", jerryx_handler_print);
5151

52-
jerryx_repl ("js> ");
52+
jerryx_repl (JERRY_ZSTR_ARG ("js> "));
5353

5454
jerry_cleanup ();
5555
} /* main */

0 commit comments

Comments
 (0)