Skip to content

Optimize LCache entrys keys #2767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions jerry-core/ecma/base/ecma-globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,14 @@ typedef struct
} ecma_lit_storage_item_t;

#ifndef CONFIG_ECMA_LCACHE_DISABLE
/**
* Container of an LCache entry identifier
*/
#ifdef JERRY_CPOINTER_32_BIT
typedef uint64_t ecma_lcache_hash_entry_id_t;
#else /* !JERRY_CPOINTER_32_BIT */
typedef uint32_t ecma_lcache_hash_entry_id_t;
#endif /* JERRY_CPOINTER_32_BIT */

/**
* Entry of LCache hash table
Expand All @@ -1498,11 +1506,8 @@ typedef struct
/** Pointer to a property of the object */
ecma_property_t *prop_p;

/** Compressed pointer to object (ECMA_NULL_POINTER marks record empty) */
jmem_cpointer_t object_cp;

/** Compressed pointer to property's name */
jmem_cpointer_t prop_name_cp;
/** Entry identifier in LCache */
ecma_lcache_hash_entry_id_t id;
} ecma_lcache_hash_entry_t;

/**
Expand Down
72 changes: 29 additions & 43 deletions jerry-core/ecma/base/ecma-lcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,28 @@
*/
#define ECMA_LCACHE_HASH_MASK (ECMA_LCACHE_HASH_ROWS_COUNT - 1)

/**
* Bitshift index for creating property identifier
*/
#define ECMA_LCACHE_HASH_ENTRY_ID_SHIFT (8 * sizeof (jmem_cpointer_t))

/**
* Create property identifier
*/
#define ECMA_LCACHE_CREATE_ID(object_cp, name_cp) \
(((ecma_lcache_hash_entry_id_t) (object_cp) << ECMA_LCACHE_HASH_ENTRY_ID_SHIFT) | (name_cp))

/**
* Invalidate specified LCache entry
*/
static inline void JERRY_ATTR_ALWAYS_INLINE
ecma_lcache_invalidate_entry (ecma_lcache_hash_entry_t *entry_p) /**< entry to invalidate */
{
JERRY_ASSERT (entry_p != NULL);
JERRY_ASSERT (entry_p->object_cp != ECMA_NULL_POINTER);
JERRY_ASSERT (entry_p->id != 0);
JERRY_ASSERT (entry_p->prop_p != NULL);

entry_p->object_cp = ECMA_NULL_POINTER;
entry_p->id = 0;
ecma_set_property_lcached (entry_p->prop_p, false);
} /* ecma_lcache_invalidate_entry */

Expand All @@ -54,11 +65,11 @@ ecma_lcache_invalidate_entry (ecma_lcache_hash_entry_t *entry_p) /**< entry to i
*/
static inline size_t JERRY_ATTR_ALWAYS_INLINE
ecma_lcache_row_index (jmem_cpointer_t object_cp, /**< compressed pointer to object */
lit_string_hash_t name_hash) /**< property name hash */
jmem_cpointer_t name_cp) /**< compressed pointer to property name */
{
/* Randomize the hash of the property name with the object pointer using a xor operation,
/* 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_hash ^ object_cp) & ECMA_LCACHE_HASH_MASK);
return (size_t) ((name_cp ^ object_cp) & ECMA_LCACHE_HASH_MASK);
} /* ecma_lcache_row_index */

/**
Expand All @@ -79,14 +90,13 @@ ecma_lcache_insert (ecma_object_t *object_p, /**< object */

ECMA_SET_NON_NULL_POINTER (object_cp, object_p);

lit_string_hash_t name_hash = ecma_string_get_property_name_hash (*prop_p, name_cp);
size_t row_index = ecma_lcache_row_index (object_cp, name_hash);
size_t row_index = ecma_lcache_row_index (object_cp, name_cp);
ecma_lcache_hash_entry_t *entries_p = JERRY_CONTEXT (lcache) [row_index];

uint32_t entry_index;
for (entry_index = 0; entry_index < ECMA_LCACHE_HASH_ROW_LENGTH; entry_index++)
{
if (entries_p[entry_index].object_cp == ECMA_NULL_POINTER)
if (entries_p[entry_index].id == 0)
{
break;
}
Expand All @@ -102,14 +112,12 @@ ecma_lcache_insert (ecma_object_t *object_p, /**< object */
{
entries_p[i] = entries_p[i - 1];
}

entry_index = 0;
}

ecma_lcache_hash_entry_t *entry_p = entries_p + entry_index;
ECMA_SET_NON_NULL_POINTER (entry_p->object_cp, object_p);
entry_p->prop_name_cp = name_cp;
entry_p->prop_p = prop_p;
entry_p->id = ECMA_LCACHE_CREATE_ID (object_cp, name_cp);

ecma_set_property_lcached (entry_p->prop_p, true);
} /* ecma_lcache_insert */
Expand All @@ -132,53 +140,32 @@ ecma_lcache_lookup (ecma_object_t *object_p, /**< object */

ecma_property_t prop_name_type;
jmem_cpointer_t prop_name_cp;
lit_string_hash_t name_hash;

if (ECMA_IS_DIRECT_STRING (prop_name_p))
{
prop_name_type = (ecma_property_t) ECMA_GET_DIRECT_STRING_TYPE (prop_name_p);

uintptr_t value = ECMA_GET_DIRECT_STRING_VALUE (prop_name_p);
prop_name_cp = (jmem_cpointer_t) value;
name_hash = (lit_string_hash_t) value;

if (prop_name_type == ECMA_DIRECT_STRING_MAGIC_EX)
{
name_hash = (lit_string_hash_t) (name_hash + LIT_MAGIC_STRING__COUNT);
}
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);
name_hash = prop_name_p->hash;
}

size_t row_index = ecma_lcache_row_index (object_cp, name_hash);
size_t row_index = ecma_lcache_row_index (object_cp, prop_name_cp);

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)
{
if (entry_p->object_cp == object_cp
&& entry_p->prop_name_cp == prop_name_cp)
if (entry_p->id == id && ECMA_PROPERTY_GET_NAME_TYPE (*entry_p->prop_p) == prop_name_type)
{
ecma_property_t *prop_p = entry_p->prop_p;

JERRY_ASSERT (prop_p != NULL && ecma_is_property_lcached (prop_p));

if (ECMA_PROPERTY_GET_NAME_TYPE (*prop_p) == prop_name_type)
{
return prop_p;
}
}
else
{
/* They can be equal, but generic string comparison is too costly. */
JERRY_ASSERT (entry_p->prop_p != NULL && ecma_is_property_lcached (entry_p->prop_p));
return entry_p->prop_p;
}

entry_p++;
}

Expand All @@ -202,18 +189,17 @@ ecma_lcache_invalidate (ecma_object_t *object_p, /**< object */
jmem_cpointer_t object_cp;
ECMA_SET_NON_NULL_POINTER (object_cp, object_p);

lit_string_hash_t name_hash = ecma_string_get_property_name_hash (*prop_p, name_cp);
size_t row_index = ecma_lcache_row_index (object_cp, name_hash);
size_t row_index = ecma_lcache_row_index (object_cp, name_cp);
ecma_lcache_hash_entry_t *entry_p = JERRY_CONTEXT (lcache) [row_index];

while (true)
{
/* The property must be present. */
JERRY_ASSERT (entry_p - JERRY_CONTEXT (lcache) [row_index] < ECMA_LCACHE_HASH_ROW_LENGTH);

if (entry_p->object_cp != ECMA_NULL_POINTER && entry_p->prop_p == prop_p)
if (entry_p->id != 0 && entry_p->prop_p == prop_p)
{
JERRY_ASSERT (entry_p->object_cp == object_cp);
JERRY_ASSERT (entry_p->id == ECMA_LCACHE_CREATE_ID (object_cp, name_cp));

ecma_lcache_invalidate_entry (entry_p);
return;
Expand Down