Skip to content

Commit 57abd26

Browse files
authored
Optimize lcache insert (#3155)
JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent a8fb4ee commit 57abd26

File tree

2 files changed

+41
-33
lines changed

2 files changed

+41
-33
lines changed

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

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,19 @@
2828

2929
#if ENABLED (JERRY_LCACHE)
3030

31+
/**
32+
* Bitshift index for calculating hash.
33+
*/
34+
#if ENABLED (JERRY_CPOINTER_32_BIT)
35+
#define ECMA_LCACHE_HASH_BITSHIFT_INDEX (2 * JMEM_ALIGNMENT_LOG)
36+
#else /* !ENABLED (JERRY_CPOINTER_32_BIT) */
37+
#define ECMA_LCACHE_HASH_BITSHIFT_INDEX 0
38+
#endif /* ENABLED (JERRY_CPOINTER_32_BIT) */
39+
3140
/**
3241
* Mask for hash bits
3342
*/
34-
#define ECMA_LCACHE_HASH_MASK (ECMA_LCACHE_HASH_ROWS_COUNT - 1)
43+
#define ECMA_LCACHE_HASH_MASK ((ECMA_LCACHE_HASH_ROWS_COUNT - 1) << ECMA_LCACHE_HASH_BITSHIFT_INDEX)
3544

3645
/**
3746
* Bitshift index for creating property identifier
@@ -69,15 +78,15 @@ ecma_lcache_row_index (jmem_cpointer_t object_cp, /**< compressed pointer to obj
6978
{
7079
/* Randomize the property name with the object pointer using a xor operation,
7180
* so properties of different objects with the same name can be cached effectively. */
72-
return (size_t) ((name_cp ^ object_cp) & ECMA_LCACHE_HASH_MASK);
81+
return (size_t) (((name_cp ^ object_cp) & ECMA_LCACHE_HASH_MASK) >> ECMA_LCACHE_HASH_BITSHIFT_INDEX);
7382
} /* ecma_lcache_row_index */
7483

7584
/**
7685
* Insert an entry into LCache
7786
*/
7887
void
79-
ecma_lcache_insert (ecma_object_t *object_p, /**< object */
80-
jmem_cpointer_t name_cp, /**< property name */
88+
ecma_lcache_insert (const ecma_object_t *object_p, /**< object */
89+
const jmem_cpointer_t name_cp, /**< property name */
8190
ecma_property_t *prop_p) /**< property */
8291
{
8392
JERRY_ASSERT (object_p != NULL);
@@ -91,31 +100,32 @@ ecma_lcache_insert (ecma_object_t *object_p, /**< object */
91100
ECMA_SET_NON_NULL_POINTER (object_cp, object_p);
92101

93102
size_t row_index = ecma_lcache_row_index (object_cp, name_cp);
94-
ecma_lcache_hash_entry_t *entries_p = JERRY_CONTEXT (lcache) [row_index];
103+
ecma_lcache_hash_entry_t *entry_p = JERRY_CONTEXT (lcache) [row_index];
104+
ecma_lcache_hash_entry_t *entry_end_p = entry_p + ECMA_LCACHE_HASH_ROW_LENGTH;
95105

96-
uint32_t entry_index;
97-
for (entry_index = 0; entry_index < ECMA_LCACHE_HASH_ROW_LENGTH; entry_index++)
106+
do
98107
{
99-
if (entries_p[entry_index].id == 0)
108+
if (entry_p->id == 0)
100109
{
101-
break;
110+
goto insert;
102111
}
112+
113+
entry_p++;
103114
}
115+
while (entry_p < entry_end_p);
104116

105-
if (entry_index == ECMA_LCACHE_HASH_ROW_LENGTH)
106-
{
107-
/* Invalidate the last entry. */
108-
ecma_lcache_invalidate_entry (entries_p + ECMA_LCACHE_HASH_ROW_LENGTH - 1);
117+
/* Invalidate the last entry. */
118+
ecma_lcache_invalidate_entry (--entry_p);
109119

110-
/* Shift other entries towards the end. */
111-
for (uint32_t i = ECMA_LCACHE_HASH_ROW_LENGTH - 1; i > 0; i--)
112-
{
113-
entries_p[i] = entries_p[i - 1];
114-
}
115-
entry_index = 0;
120+
/* Shift other entries towards the end. */
121+
for (uint32_t i = 0; i < ECMA_LCACHE_HASH_ROW_LENGTH - 1; i++)
122+
{
123+
entry_p->id = entry_p[-1].id;
124+
entry_p->prop_p = entry_p[-1].prop_p;
125+
entry_p--;
116126
}
117127

118-
ecma_lcache_hash_entry_t *entry_p = entries_p + entry_index;
128+
insert:
119129
entry_p->prop_p = prop_p;
120130
entry_p->id = ECMA_LCACHE_CREATE_ID (object_cp, name_cp);
121131

@@ -129,7 +139,7 @@ ecma_lcache_insert (ecma_object_t *object_p, /**< object */
129139
* NULL otherwise
130140
*/
131141
inline ecma_property_t * JERRY_ATTR_ALWAYS_INLINE
132-
ecma_lcache_lookup (ecma_object_t *object_p, /**< object */
142+
ecma_lcache_lookup (const ecma_object_t *object_p, /**< object */
133143
const ecma_string_t *prop_name_p) /**< property's name */
134144
{
135145
JERRY_ASSERT (object_p != NULL);
@@ -138,36 +148,35 @@ ecma_lcache_lookup (ecma_object_t *object_p, /**< object */
138148
jmem_cpointer_t object_cp;
139149
ECMA_SET_NON_NULL_POINTER (object_cp, object_p);
140150

141-
ecma_property_t prop_name_type;
151+
ecma_property_t prop_name_type = ECMA_DIRECT_STRING_PTR;
142152
jmem_cpointer_t prop_name_cp;
143153

144-
if (ECMA_IS_DIRECT_STRING (prop_name_p))
154+
if (JERRY_UNLIKELY (ECMA_IS_DIRECT_STRING (prop_name_p)))
145155
{
146156
prop_name_type = (ecma_property_t) ECMA_GET_DIRECT_STRING_TYPE (prop_name_p);
147157
prop_name_cp = (jmem_cpointer_t) ECMA_GET_DIRECT_STRING_VALUE (prop_name_p);
148158
}
149159
else
150160
{
151-
prop_name_type = ECMA_DIRECT_STRING_PTR;
152161
ECMA_SET_NON_NULL_POINTER (prop_name_cp, prop_name_p);
153162
}
154163

155164
size_t row_index = ecma_lcache_row_index (object_cp, prop_name_cp);
156165

157166
ecma_lcache_hash_entry_t *entry_p = JERRY_CONTEXT (lcache) [row_index];
158167
ecma_lcache_hash_entry_t *entry_end_p = entry_p + ECMA_LCACHE_HASH_ROW_LENGTH;
159-
160168
ecma_lcache_hash_entry_id_t id = ECMA_LCACHE_CREATE_ID (object_cp, prop_name_cp);
161169

162-
while (entry_p < entry_end_p)
170+
do
163171
{
164-
if (entry_p->id == id && ECMA_PROPERTY_GET_NAME_TYPE (*entry_p->prop_p) == prop_name_type)
172+
if (entry_p->id == id && JERRY_LIKELY (ECMA_PROPERTY_GET_NAME_TYPE (*entry_p->prop_p) == prop_name_type))
165173
{
166174
JERRY_ASSERT (entry_p->prop_p != NULL && ecma_is_property_lcached (entry_p->prop_p));
167175
return entry_p->prop_p;
168176
}
169177
entry_p++;
170178
}
179+
while (entry_p < entry_end_p);
171180

172181
return NULL;
173182
} /* ecma_lcache_lookup */
@@ -176,8 +185,8 @@ ecma_lcache_lookup (ecma_object_t *object_p, /**< object */
176185
* Invalidate LCache entries associated with given object and property name / property
177186
*/
178187
void
179-
ecma_lcache_invalidate (ecma_object_t *object_p, /**< object */
180-
jmem_cpointer_t name_cp, /**< property name */
188+
ecma_lcache_invalidate (const ecma_object_t *object_p, /**< object */
189+
const jmem_cpointer_t name_cp, /**< property name */
181190
ecma_property_t *prop_p) /**< property */
182191
{
183192
JERRY_ASSERT (object_p != NULL);

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
*/
2525

2626
#if ENABLED (JERRY_LCACHE)
27-
28-
void ecma_lcache_insert (ecma_object_t *object_p, jmem_cpointer_t name_cp, ecma_property_t *prop_p);
29-
ecma_property_t *ecma_lcache_lookup (ecma_object_t *object_p, const ecma_string_t *prop_name_p);
30-
void ecma_lcache_invalidate (ecma_object_t *object_p, jmem_cpointer_t name_cp, ecma_property_t *prop_p);
27+
void ecma_lcache_insert (const ecma_object_t *object_p, const jmem_cpointer_t name_cp, ecma_property_t *prop_p);
28+
ecma_property_t *ecma_lcache_lookup (const ecma_object_t *object_p, const ecma_string_t *prop_name_p);
29+
void ecma_lcache_invalidate (const ecma_object_t *object_p, const jmem_cpointer_t name_cp, ecma_property_t *prop_p);
3130

3231
#endif /* ENABLED (JERRY_LCACHE) */
3332

0 commit comments

Comments
 (0)