From d61f2fb292c36a67bd2789e107e2a5d61706434a Mon Sep 17 00:00:00 2001 From: yanpeng Date: Tue, 24 Oct 2023 09:40:43 +0800 Subject: [PATCH] jerry jerry_literal_cache.patch add Signed-off-by: yanpeng --- jerry-core/api/jerry_literal_cache.h | 0 jerry-core/ecma/base/ecma-helpers-string.c | 17 ++++++----- jerry-core/ecma/base/ecma-literal-storage.c | 34 ++++++++++++++++++++- jerry-core/ecma/base/ecma-literal-storage.h | 4 +++ 4 files changed, 46 insertions(+), 9 deletions(-) create mode 100755 jerry-core/api/jerry_literal_cache.h mode change 100644 => 100755 jerry-core/ecma/base/ecma-helpers-string.c mode change 100644 => 100755 jerry-core/ecma/base/ecma-literal-storage.c diff --git a/jerry-core/api/jerry_literal_cache.h b/jerry-core/api/jerry_literal_cache.h new file mode 100755 index 00000000..e69de29b diff --git a/jerry-core/ecma/base/ecma-helpers-string.c b/jerry-core/ecma/base/ecma-helpers-string.c old mode 100644 new mode 100755 index 4c94038d..87d9b8ce --- a/jerry-core/ecma/base/ecma-helpers-string.c +++ b/jerry-core/ecma/base/ecma-helpers-string.c @@ -1662,19 +1662,20 @@ ecma_compare_ecma_strings_longpath (const ecma_string_t *string1_p, /**< ecma-st const lit_utf8_byte_t *utf8_string1_p, *utf8_string2_p; lit_utf8_size_t utf8_string1_size, utf8_string2_size; - if (JERRY_LIKELY (ECMA_STRING_GET_CONTAINER (string1_p) == ECMA_STRING_CONTAINER_HEAP_ASCII_STRING)) - { - utf8_string1_p = ECMA_ASCII_STRING_GET_BUFFER (string1_p); - utf8_string1_size = ((ecma_ascii_string_t *) string1_p)->size; - utf8_string2_p = ECMA_ASCII_STRING_GET_BUFFER (string2_p); - utf8_string2_size = ((ecma_ascii_string_t *) string2_p)->size; - } - else if (ECMA_STRING_GET_CONTAINER (string1_p) == ECMA_STRING_CONTAINER_HEAP_UTF8_STRING) + if (JERRY_LIKELY (ECMA_STRING_GET_CONTAINER (string1_p) == ECMA_STRING_CONTAINER_HEAP_UTF8_STRING)) { utf8_string1_p = ECMA_UTF8_STRING_GET_BUFFER (string1_p); utf8_string1_size = ((ecma_utf8_string_t *) string1_p)->size; utf8_string2_p = ECMA_UTF8_STRING_GET_BUFFER (string2_p); utf8_string2_size = ((ecma_utf8_string_t *) string2_p)->size; + + } + else if (ECMA_STRING_GET_CONTAINER(string1_p) == ECMA_STRING_CONTAINER_HEAP_ASCII_STRING) + { + utf8_string1_p = ECMA_ASCII_STRING_GET_BUFFER (string1_p); + utf8_string1_size = ((ecma_ascii_string_t *) string1_p)->size; + utf8_string2_p = ECMA_ASCII_STRING_GET_BUFFER (string2_p); + utf8_string2_size = ((ecma_ascii_string_t *) string2_p)->size; } else { diff --git a/jerry-core/ecma/base/ecma-literal-storage.c b/jerry-core/ecma/base/ecma-literal-storage.c old mode 100644 new mode 100755 index e4dc58ce..1e2b8f46 --- a/jerry-core/ecma/base/ecma-literal-storage.c +++ b/jerry-core/ecma/base/ecma-literal-storage.c @@ -17,6 +17,20 @@ #include "ecma-literal-storage.h" #include "ecma-helpers.h" #include "jcontext.h" +#include "jerry_literal_cache.h" + +__weak void InitJerryLiteralCache(void) {} + +__weak void ClearJerryLiteralCache(void) {} + +__weak void AddJerryLiteralCache(int32_t hash, void *p) {} + +__weak void DelJerryLiteralCache(int32_t hash) {} + +__weak void *GetJerryLiteralCache(int32_t hash) +{ + return (void*)UINT32_MAX; +} /** \addtogroup ecma ECMA * @{ @@ -73,6 +87,7 @@ ecma_free_string_list (jmem_cpointer_t string_list_cp) /**< string list */ string_list_p->values[i]); JERRY_ASSERT (ECMA_STRING_IS_REF_EQUALS_TO_ONE (string_p)); + DelJerryLiteralCache(string_p->u.hash); ecma_destroy_ecma_string (string_p); } } @@ -138,6 +153,14 @@ ecma_find_or_create_literal_string (const lit_utf8_byte_t *chars_p, /**< string return ecma_make_string_value (string_p); } + ecma_string_t *cached_literal = (ecma_string_t *)GetJerryLiteralCache(string_p->u.hash); + if ((cached_literal != NULL) && (cached_literal != (void*)UINT32_MAX)) { + if (ecma_compare_ecma_strings (string_p, cached_literal)) { + ecma_deref_ecma_string (string_p); + return ecma_make_string_value (cached_literal); + } + } + jmem_cpointer_t string_list_cp = JERRY_CONTEXT (string_list_first_cp); jmem_cpointer_t *empty_cpointer_p = NULL; @@ -153,8 +176,12 @@ ecma_find_or_create_literal_string (const lit_utf8_byte_t *chars_p, /**< string { empty_cpointer_p = string_list_p->values + i; } + + if ((cached_literal == NULL) && empty_cpointer_p != NULL) { + break; + } } - else + else if (cached_literal != NULL) { ecma_string_t *value_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_string_t, string_list_p->values[i]); @@ -168,6 +195,9 @@ ecma_find_or_create_literal_string (const lit_utf8_byte_t *chars_p, /**< string } } + if ((cached_literal == NULL) && (empty_cpointer_p != NULL)) { + break; + } string_list_cp = string_list_p->next_cp; } @@ -178,6 +208,7 @@ ecma_find_or_create_literal_string (const lit_utf8_byte_t *chars_p, /**< string if (empty_cpointer_p != NULL) { *empty_cpointer_p = result; + AddJerryLiteralCache(string_p->u.hash, string_p); return ecma_make_string_value (string_p); } @@ -193,6 +224,7 @@ ecma_find_or_create_literal_string (const lit_utf8_byte_t *chars_p, /**< string new_item_p->next_cp = JERRY_CONTEXT (string_list_first_cp); JMEM_CP_SET_NON_NULL_POINTER (JERRY_CONTEXT (string_list_first_cp), new_item_p); + AddJerryLiteralCache(string_p->u.hash, string_p); return ecma_make_string_value (string_p); } /* ecma_find_or_create_literal_string */ diff --git a/jerry-core/ecma/base/ecma-literal-storage.h b/jerry-core/ecma/base/ecma-literal-storage.h index debbcc50..809486ca 100644 --- a/jerry-core/ecma/base/ecma-literal-storage.h +++ b/jerry-core/ecma/base/ecma-literal-storage.h @@ -20,6 +20,10 @@ #include "jmem.h" #include "lit-globals.h" +#ifndef __weak +#define __weak __attribute__((weak)) +#endif + /** \addtogroup ecma ECMA * @{ * -- Gitee