From 57abd26c33622288bb1ac763880e47b4c00f2653 Mon Sep 17 00:00:00 2001 From: Robert Fancsik Date: Mon, 30 Sep 2019 16:46:18 +0200 Subject: [PATCH] Optimize lcache insert (#3155) JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu --- jerry-core/ecma/base/ecma-lcache.c | 73 +++++++++++++++++------------- jerry-core/ecma/base/ecma-lcache.h | 7 ++- 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/jerry-core/ecma/base/ecma-lcache.c b/jerry-core/ecma/base/ecma-lcache.c index 3a9df1146..338fe1c66 100644 --- a/jerry-core/ecma/base/ecma-lcache.c +++ b/jerry-core/ecma/base/ecma-lcache.c @@ -28,10 +28,19 @@ #if ENABLED (JERRY_LCACHE) +/** + * Bitshift index for calculating hash. + */ +#if ENABLED (JERRY_CPOINTER_32_BIT) +#define ECMA_LCACHE_HASH_BITSHIFT_INDEX (2 * JMEM_ALIGNMENT_LOG) +#else /* !ENABLED (JERRY_CPOINTER_32_BIT) */ +#define ECMA_LCACHE_HASH_BITSHIFT_INDEX 0 +#endif /* ENABLED (JERRY_CPOINTER_32_BIT) */ + /** * Mask for hash bits */ -#define ECMA_LCACHE_HASH_MASK (ECMA_LCACHE_HASH_ROWS_COUNT - 1) +#define ECMA_LCACHE_HASH_MASK ((ECMA_LCACHE_HASH_ROWS_COUNT - 1) << ECMA_LCACHE_HASH_BITSHIFT_INDEX) /** * Bitshift index for creating property identifier @@ -69,15 +78,15 @@ ecma_lcache_row_index (jmem_cpointer_t object_cp, /**< compressed pointer to obj { /* Randomize the property name with the object pointer using a xor operation, * so properties of different objects with the same name can be cached effectively. */ - return (size_t) ((name_cp ^ object_cp) & ECMA_LCACHE_HASH_MASK); + return (size_t) (((name_cp ^ object_cp) & ECMA_LCACHE_HASH_MASK) >> ECMA_LCACHE_HASH_BITSHIFT_INDEX); } /* ecma_lcache_row_index */ /** * Insert an entry into LCache */ void -ecma_lcache_insert (ecma_object_t *object_p, /**< object */ - jmem_cpointer_t name_cp, /**< property name */ +ecma_lcache_insert (const ecma_object_t *object_p, /**< object */ + const jmem_cpointer_t name_cp, /**< property name */ ecma_property_t *prop_p) /**< property */ { JERRY_ASSERT (object_p != NULL); @@ -91,31 +100,32 @@ ecma_lcache_insert (ecma_object_t *object_p, /**< object */ ECMA_SET_NON_NULL_POINTER (object_cp, object_p); size_t row_index = ecma_lcache_row_index (object_cp, name_cp); - ecma_lcache_hash_entry_t *entries_p = JERRY_CONTEXT (lcache) [row_index]; + ecma_lcache_hash_entry_t *entry_p = JERRY_CONTEXT (lcache) [row_index]; + ecma_lcache_hash_entry_t *entry_end_p = entry_p + ECMA_LCACHE_HASH_ROW_LENGTH; - uint32_t entry_index; - for (entry_index = 0; entry_index < ECMA_LCACHE_HASH_ROW_LENGTH; entry_index++) + do { - if (entries_p[entry_index].id == 0) + if (entry_p->id == 0) { - break; + goto insert; } + + entry_p++; + } + while (entry_p < entry_end_p); + + /* Invalidate the last entry. */ + ecma_lcache_invalidate_entry (--entry_p); + + /* Shift other entries towards the end. */ + for (uint32_t i = 0; i < ECMA_LCACHE_HASH_ROW_LENGTH - 1; i++) + { + entry_p->id = entry_p[-1].id; + entry_p->prop_p = entry_p[-1].prop_p; + entry_p--; } - if (entry_index == ECMA_LCACHE_HASH_ROW_LENGTH) - { - /* Invalidate the last entry. */ - ecma_lcache_invalidate_entry (entries_p + ECMA_LCACHE_HASH_ROW_LENGTH - 1); - - /* Shift other entries towards the end. */ - for (uint32_t i = ECMA_LCACHE_HASH_ROW_LENGTH - 1; i > 0; i--) - { - entries_p[i] = entries_p[i - 1]; - } - entry_index = 0; - } - - ecma_lcache_hash_entry_t *entry_p = entries_p + entry_index; +insert: entry_p->prop_p = prop_p; entry_p->id = ECMA_LCACHE_CREATE_ID (object_cp, name_cp); @@ -129,7 +139,7 @@ ecma_lcache_insert (ecma_object_t *object_p, /**< object */ * NULL otherwise */ inline ecma_property_t * JERRY_ATTR_ALWAYS_INLINE -ecma_lcache_lookup (ecma_object_t *object_p, /**< object */ +ecma_lcache_lookup (const ecma_object_t *object_p, /**< object */ const ecma_string_t *prop_name_p) /**< property's name */ { JERRY_ASSERT (object_p != NULL); @@ -138,17 +148,16 @@ ecma_lcache_lookup (ecma_object_t *object_p, /**< object */ jmem_cpointer_t object_cp; ECMA_SET_NON_NULL_POINTER (object_cp, object_p); - ecma_property_t prop_name_type; + ecma_property_t prop_name_type = ECMA_DIRECT_STRING_PTR; jmem_cpointer_t prop_name_cp; - if (ECMA_IS_DIRECT_STRING (prop_name_p)) + if (JERRY_UNLIKELY (ECMA_IS_DIRECT_STRING (prop_name_p))) { prop_name_type = (ecma_property_t) ECMA_GET_DIRECT_STRING_TYPE (prop_name_p); prop_name_cp = (jmem_cpointer_t) ECMA_GET_DIRECT_STRING_VALUE (prop_name_p); } else { - prop_name_type = ECMA_DIRECT_STRING_PTR; ECMA_SET_NON_NULL_POINTER (prop_name_cp, prop_name_p); } @@ -156,18 +165,18 @@ ecma_lcache_lookup (ecma_object_t *object_p, /**< object */ ecma_lcache_hash_entry_t *entry_p = JERRY_CONTEXT (lcache) [row_index]; ecma_lcache_hash_entry_t *entry_end_p = entry_p + ECMA_LCACHE_HASH_ROW_LENGTH; - ecma_lcache_hash_entry_id_t id = ECMA_LCACHE_CREATE_ID (object_cp, prop_name_cp); - while (entry_p < entry_end_p) + do { - if (entry_p->id == id && ECMA_PROPERTY_GET_NAME_TYPE (*entry_p->prop_p) == prop_name_type) + if (entry_p->id == id && JERRY_LIKELY (ECMA_PROPERTY_GET_NAME_TYPE (*entry_p->prop_p) == prop_name_type)) { JERRY_ASSERT (entry_p->prop_p != NULL && ecma_is_property_lcached (entry_p->prop_p)); return entry_p->prop_p; } entry_p++; } + while (entry_p < entry_end_p); return NULL; } /* ecma_lcache_lookup */ @@ -176,8 +185,8 @@ ecma_lcache_lookup (ecma_object_t *object_p, /**< object */ * Invalidate LCache entries associated with given object and property name / property */ void -ecma_lcache_invalidate (ecma_object_t *object_p, /**< object */ - jmem_cpointer_t name_cp, /**< property name */ +ecma_lcache_invalidate (const ecma_object_t *object_p, /**< object */ + const jmem_cpointer_t name_cp, /**< property name */ ecma_property_t *prop_p) /**< property */ { JERRY_ASSERT (object_p != NULL); diff --git a/jerry-core/ecma/base/ecma-lcache.h b/jerry-core/ecma/base/ecma-lcache.h index 428bba097..e4add397b 100644 --- a/jerry-core/ecma/base/ecma-lcache.h +++ b/jerry-core/ecma/base/ecma-lcache.h @@ -24,10 +24,9 @@ */ #if ENABLED (JERRY_LCACHE) - -void ecma_lcache_insert (ecma_object_t *object_p, jmem_cpointer_t name_cp, ecma_property_t *prop_p); -ecma_property_t *ecma_lcache_lookup (ecma_object_t *object_p, const ecma_string_t *prop_name_p); -void ecma_lcache_invalidate (ecma_object_t *object_p, jmem_cpointer_t name_cp, ecma_property_t *prop_p); +void ecma_lcache_insert (const ecma_object_t *object_p, const jmem_cpointer_t name_cp, ecma_property_t *prop_p); +ecma_property_t *ecma_lcache_lookup (const ecma_object_t *object_p, const ecma_string_t *prop_name_p); +void ecma_lcache_invalidate (const ecma_object_t *object_p, const jmem_cpointer_t name_cp, ecma_property_t *prop_p); #endif /* ENABLED (JERRY_LCACHE) */