Skip to content

Commit 7f0d17f

Browse files
committed
Remove string copy
Changed 'ecma_copy_or_ref_ecma_string' to 'ecma_ref_ecma_string'. It does not copy the string if the maximum number of reference counter is reached, but bails out with an error like the 'ecma_ref_object' function does. JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com
1 parent 378d7f7 commit 7f0d17f

18 files changed

+36
-117
lines changed

jerry-core/ecma/base/ecma-gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ ecma_init_gc_info (ecma_object_t *object_p) /**< object */
167167
void
168168
ecma_ref_object (ecma_object_t *object_p) /**< object */
169169
{
170-
if (object_p->type_flags_refs < ECMA_OBJECT_MAX_REF)
170+
if (likely (object_p->type_flags_refs < ECMA_OBJECT_MAX_REF))
171171
{
172172
object_p->type_flags_refs = (uint16_t) (object_p->type_flags_refs + ECMA_OBJECT_REF_ONE);
173173
}

jerry-core/ecma/base/ecma-helpers-string.c

Lines changed: 11 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,11 @@ ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */
384384

385385
if (str1_size == 0)
386386
{
387-
return ecma_copy_or_ref_ecma_string (string2_p);
387+
return ecma_ref_ecma_string (string2_p);
388388
}
389389
else if (str2_size == 0)
390390
{
391-
return ecma_copy_or_ref_ecma_string (string1_p);
391+
return ecma_ref_ecma_string (string1_p);
392392
}
393393

394394
const lit_utf8_size_t new_size = str1_size + str2_size;
@@ -442,111 +442,30 @@ ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */
442442
return string_desc_p;
443443
} /* ecma_concat_ecma_strings */
444444

445-
/**
446-
* Copy ecma-string
447-
*
448-
* @return pointer to copy of ecma-string with reference counter set to 1
449-
*/
450-
static ecma_string_t *
451-
ecma_copy_ecma_string (ecma_string_t *string_desc_p) /**< string descriptor */
452-
{
453-
JERRY_ASSERT (string_desc_p != NULL);
454-
JERRY_ASSERT (string_desc_p->refs_and_container >= ECMA_STRING_REF_ONE);
455-
456-
ecma_string_t *new_str_p;
457-
458-
switch (ECMA_STRING_GET_CONTAINER (string_desc_p))
459-
{
460-
case ECMA_STRING_CONTAINER_LIT_TABLE:
461-
case ECMA_STRING_CONTAINER_UINT32_IN_DESC:
462-
case ECMA_STRING_CONTAINER_MAGIC_STRING:
463-
case ECMA_STRING_CONTAINER_MAGIC_STRING_EX:
464-
{
465-
new_str_p = ecma_alloc_string ();
466-
467-
*new_str_p = *string_desc_p;
468-
469-
new_str_p->refs_and_container = ECMA_STRING_SET_REF_TO_ONE (new_str_p->refs_and_container);
470-
471-
break;
472-
}
473-
474-
case ECMA_STRING_CONTAINER_HEAP_UTF8_STRING:
475-
{
476-
new_str_p = ecma_alloc_string ();
477-
*new_str_p = *string_desc_p;
478-
new_str_p->refs_and_container = ECMA_STRING_SET_REF_TO_ONE (new_str_p->refs_and_container);
479-
480-
const ecma_string_heap_header_t *data_p = ECMA_GET_NON_NULL_POINTER (ecma_string_heap_header_t,
481-
string_desc_p->u.utf8_collection_cp);
482-
JERRY_ASSERT (data_p != NULL);
483-
const size_t data_size = data_p->size + sizeof (ecma_string_heap_header_t);
484-
ecma_string_heap_header_t *new_data_p = (ecma_string_heap_header_t *) jmem_heap_alloc_block (data_size);
485-
memcpy (new_data_p, data_p, data_size);
486-
487-
ECMA_SET_NON_NULL_POINTER (new_str_p->u.utf8_collection_cp, new_data_p);
488-
489-
break;
490-
}
491-
492-
case ECMA_STRING_CONTAINER_HEAP_ASCII_STRING:
493-
{
494-
new_str_p = ecma_alloc_string ();
495-
*new_str_p = *string_desc_p;
496-
new_str_p->refs_and_container = ECMA_STRING_SET_REF_TO_ONE (new_str_p->refs_and_container);
497-
498-
const lit_utf8_byte_t *data_p = ECMA_GET_NON_NULL_POINTER (lit_utf8_byte_t,
499-
string_desc_p->u.ascii_string.ascii_collection_cp);
500-
501-
JERRY_ASSERT (data_p != NULL);
502-
const size_t data_size = string_desc_p->u.ascii_string.size;
503-
lit_utf8_byte_t *new_data_p = (lit_utf8_byte_t *) jmem_heap_alloc_block (data_size);
504-
memcpy (new_data_p, data_p, data_size);
505-
506-
ECMA_SET_NON_NULL_POINTER (new_str_p->u.ascii_string.ascii_collection_cp, new_data_p);
507-
508-
break;
509-
}
510-
511-
default:
512-
{
513-
JERRY_UNREACHABLE ();
514-
}
515-
}
516-
517-
JERRY_ASSERT (ecma_compare_ecma_strings (string_desc_p, new_str_p));
518-
519-
return new_str_p;
520-
} /* ecma_copy_ecma_string */
521-
522445
/**
523446
* Increase reference counter of ecma-string.
524447
*
525448
* @return pointer to same ecma-string descriptor with increased reference counter
526449
* or the ecma-string's copy with reference counter set to 1
527450
*/
528451
ecma_string_t *
529-
ecma_copy_or_ref_ecma_string (ecma_string_t *string_p) /**< string descriptor */
452+
ecma_ref_ecma_string (ecma_string_t *string_p) /**< string descriptor */
530453
{
531454
JERRY_ASSERT (string_p != NULL);
532455
JERRY_ASSERT (string_p->refs_and_container >= ECMA_STRING_REF_ONE);
533456

534-
if (unlikely (string_p->refs_and_container >= ECMA_STRING_MAX_REF))
457+
if (likely (string_p->refs_and_container < ECMA_STRING_MAX_REF))
535458
{
536-
/* First trying to free unreachable objects that maybe refer to the string */
537-
ecma_gc_run ();
538-
539-
if (string_p->refs_and_container >= ECMA_STRING_MAX_REF)
540-
{
541-
/* reference counter was not changed during GC, copying string */
542-
return ecma_copy_ecma_string (string_p);
543-
}
459+
/* Increase reference counter. */
460+
string_p->refs_and_container = (uint16_t) (string_p->refs_and_container + ECMA_STRING_REF_ONE);
461+
}
462+
else
463+
{
464+
jerry_fatal (ERR_REF_COUNT_LIMIT);
544465
}
545466

546-
/* Increase reference counter. */
547-
string_p->refs_and_container = (uint16_t) (string_p->refs_and_container + ECMA_STRING_REF_ONE);
548467
return string_p;
549-
} /* ecma_copy_or_ref_ecma_string */
468+
} /* ecma_ref_ecma_string */
550469

551470
/**
552471
* Decrease reference counter and deallocate ecma-string

jerry-core/ecma/base/ecma-helpers-value.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ ecma_copy_value (ecma_value_t value) /**< value description */
637637
}
638638
case ECMA_TYPE_STRING:
639639
{
640-
return ecma_make_string_value (ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (value)));
640+
return ecma_make_string_value (ecma_ref_ecma_string (ecma_get_string_from_value (value)));
641641
}
642642
case ECMA_TYPE_OBJECT:
643643
{

jerry-core/ecma/base/ecma-helpers.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ ecma_create_named_data_property (ecma_object_t *object_p, /**< object */
584584

585585
uint8_t type_and_flags = ECMA_PROPERTY_TYPE_NAMEDDATA | prop_attributes;
586586

587-
name_p = ecma_copy_or_ref_ecma_string (name_p);
587+
name_p = ecma_ref_ecma_string (name_p);
588588

589589
ecma_property_value_t value;
590590
value.value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
@@ -610,7 +610,7 @@ ecma_create_named_accessor_property (ecma_object_t *object_p, /**< object */
610610

611611
uint8_t type_and_flags = ECMA_PROPERTY_TYPE_NAMEDACCESSOR | prop_attributes;
612612

613-
name_p = ecma_copy_or_ref_ecma_string (name_p);
613+
name_p = ecma_ref_ecma_string (name_p);
614614

615615
ecma_property_value_t value;
616616
ECMA_SET_POINTER (value.getter_setter_pair.getter_p, get_p);

jerry-core/ecma/base/ecma-helpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ extern ecma_string_t *ecma_new_ecma_string_from_lit_cp (lit_cpointer_t);
169169
extern ecma_string_t *ecma_new_ecma_string_from_magic_string_id (lit_magic_string_id_t);
170170
extern ecma_string_t *ecma_new_ecma_string_from_magic_string_ex_id (lit_magic_string_ex_id_t);
171171
extern ecma_string_t *ecma_concat_ecma_strings (ecma_string_t *, ecma_string_t *);
172-
extern ecma_string_t *ecma_copy_or_ref_ecma_string (ecma_string_t *);
172+
extern ecma_string_t *ecma_ref_ecma_string (ecma_string_t *);
173173
extern void ecma_deref_ecma_string (ecma_string_t *);
174174
extern ecma_number_t ecma_string_to_number (const ecma_string_t *);
175175
extern bool ecma_string_get_array_index (const ecma_string_t *, uint32_t *);

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ ecma_builtin_array_prototype_object_to_locale_string (const ecma_value_t this_ar
175175
ecma_builtin_helper_get_to_locale_string_at_index (obj_p, 0),
176176
ret_value);
177177

178-
ecma_string_t *return_string_p = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (first_value));
178+
ecma_string_t *return_string_p = ecma_ref_ecma_string (ecma_get_string_from_value (first_value));
179179

180180
/* 9-10. */
181181
for (uint32_t k = 1; ecma_is_value_empty (ret_value) && (k < length); k++)
@@ -402,7 +402,7 @@ ecma_builtin_array_prototype_join (const ecma_value_t this_arg, /**< this argume
402402
ecma_op_array_get_to_string_at_index (obj_p, 0),
403403
ret_value);
404404

405-
ecma_string_t *return_string_p = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (first_value));
405+
ecma_string_t *return_string_p = ecma_ref_ecma_string (ecma_get_string_from_value (first_value));
406406

407407
/* 9-10. */
408408
for (uint32_t k = 1; ecma_is_value_empty (ret_value) && (k < length); k++)

jerry-core/ecma/builtin-objects/ecma-builtin-error-prototype.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ ecma_builtin_error_prototype_object_to_string (ecma_value_t this_arg) /**< this
125125

126126
if (ecma_string_get_length (name_string_p) == 0)
127127
{
128-
ret_str_p = ecma_copy_or_ref_ecma_string (msg_string_p);
128+
ret_str_p = ecma_ref_ecma_string (msg_string_p);
129129
}
130130
else if (ecma_string_get_length (msg_string_p) == 0)
131131
{
132-
ret_str_p = ecma_copy_or_ref_ecma_string (name_string_p);
132+
ret_str_p = ecma_ref_ecma_string (name_string_p);
133133
}
134134
else
135135
{

jerry-core/ecma/builtin-objects/ecma-builtin-json.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ ecma_builtin_json_stringify (ecma_value_t this_arg, /**< 'this' argument */
992992

993993
if (num_of_chars < 10)
994994
{
995-
context.gap_str_p = ecma_copy_or_ref_ecma_string (space_str_p);
995+
context.gap_str_p = ecma_ref_ecma_string (space_str_p);
996996
}
997997
else
998998
{
@@ -1061,7 +1061,7 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
10611061
{
10621062
/* 1. */
10631063
ecma_string_t *quote_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_DOUBLE_QUOTE_CHAR);
1064-
ecma_string_t *product_str_p = ecma_copy_or_ref_ecma_string (quote_str_p);
1064+
ecma_string_t *product_str_p = ecma_ref_ecma_string (quote_str_p);
10651065
ecma_string_t *tmp_str_p;
10661066

10671067
ECMA_STRING_TO_UTF8_STRING (string_p, string_buff, string_buff_size);

jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ ecma_builtin_regexp_prototype_compile (ecma_value_t this_arg, /**< this argument
168168
}
169169
else
170170
{
171-
pattern_string_p = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (regexp_str_value));
171+
pattern_string_p = ecma_ref_ecma_string (ecma_get_string_from_value (regexp_str_value));
172172
}
173173

174174
ECMA_FINALIZE (regexp_str_value);
@@ -380,7 +380,7 @@ ecma_builtin_regexp_prototype_to_string (ecma_value_t this_arg) /**< this argume
380380

381381
ecma_string_t *src_sep_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_SLASH_CHAR);
382382
ecma_string_t *source_str_p = ecma_get_string_from_value (ecma_get_named_data_property_value (source_prop_p));
383-
ecma_string_t *output_str_p = ecma_concat_ecma_strings (src_sep_str_p, ecma_copy_or_ref_ecma_string (source_str_p));
383+
ecma_string_t *output_str_p = ecma_concat_ecma_strings (src_sep_str_p, ecma_ref_ecma_string (source_str_p));
384384
ecma_deref_ecma_string (source_str_p);
385385

386386
ecma_string_t *concat_p = ecma_concat_ecma_strings (output_str_p, src_sep_str_p);

jerry-core/ecma/builtin-objects/ecma-builtin-regexp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ ecma_builtin_regexp_dispatch_construct (const ecma_value_t *arguments_list_p, /*
109109
}
110110
else
111111
{
112-
pattern_string_p = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (regexp_str_value));
112+
pattern_string_p = ecma_ref_ecma_string (ecma_get_string_from_value (regexp_str_value));
113113
}
114114

115115
ECMA_FINALIZE (regexp_str_value);
@@ -125,7 +125,7 @@ ecma_builtin_regexp_dispatch_construct (const ecma_value_t *arguments_list_p, /*
125125
ecma_op_to_string (flags_value),
126126
ret_value);
127127

128-
flags_string_p = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (flags_str_value));
128+
flags_string_p = ecma_ref_ecma_string (ecma_get_string_from_value (flags_str_value));
129129
ECMA_FINALIZE (flags_str_value);
130130
}
131131

0 commit comments

Comments
 (0)