@@ -153,23 +153,65 @@ ecma_finalize_lit_storage (void)
153153} /* ecma_finalize_lit_storage */
154154
155155/**
156- * Find or create a literal string.
156+ * Create a new literal string slot "pool" .
157157 *
158- * @return ecma_string_t compressed pointer
158+ * @return jmem_cpointer_t slot pointer
159159 */
160- ecma_value_t
161- ecma_find_or_create_literal_string (const lit_utf8_byte_t * chars_p , /**< string to be searched */
162- lit_utf8_size_t size , /**< size of the string */
163- bool is_ascii ) /**< encode of the string */
160+
161+ jmem_cpointer_t *
162+ ecma_allocate_new_string_slot (void )
164163{
165- ecma_string_t * string_p =
166- ( is_ascii ? ecma_new_ecma_string_from_ascii ( chars_p , size ) : ecma_new_ecma_string_from_utf8 ( chars_p , size ));
164+ ecma_lit_storage_item_t * new_item_p ;
165+ new_item_p = ( ecma_lit_storage_item_t * ) jmem_pools_alloc ( sizeof ( ecma_lit_storage_item_t ));
167166
168- if ( ECMA_IS_DIRECT_STRING ( string_p ) )
167+ for ( int i = 0 ; i < ECMA_LIT_STORAGE_VALUE_COUNT ; i ++ )
169168 {
170- return ecma_make_string_value (string_p );
169+ new_item_p -> values [i ] = JMEM_CP_NULL ;
170+ }
171+
172+ new_item_p -> next_cp = JERRY_CONTEXT (string_list_first_cp );
173+ JMEM_CP_SET_NON_NULL_POINTER (JERRY_CONTEXT (string_list_first_cp ), new_item_p );
174+
175+ return new_item_p -> values + 0 ;
176+ } /* ecma_allocate_new_string_slot */
177+
178+ /**
179+ * Find an empty a literal string slot.
180+ *
181+ * @return jmem_cpointer_t slot pointer
182+ */
183+
184+ jmem_cpointer_t *
185+ ecma_find_empty_literal_string_slot (void )
186+ {
187+ jmem_cpointer_t string_list_cp = JERRY_CONTEXT (string_list_first_cp );
188+
189+ while (string_list_cp != JMEM_CP_NULL )
190+ {
191+ ecma_lit_storage_item_t * string_list_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_lit_storage_item_t , string_list_cp );
192+
193+ for (int i = 0 ; i < ECMA_LIT_STORAGE_VALUE_COUNT ; i ++ )
194+ {
195+ if (string_list_p -> values [i ] == JMEM_CP_NULL )
196+ {
197+ return string_list_p -> values + i ;
198+ }
199+ }
200+ string_list_cp = string_list_p -> next_cp ;
171201 }
172202
203+ return ecma_allocate_new_string_slot ();
204+ } /* ecma_find_empty_literal_string_slot */
205+
206+ /**
207+ * Find an empty or similar a literal string slot.
208+ *
209+ * @return jmem_cpointer_t slot pointer
210+ */
211+
212+ jmem_cpointer_t *
213+ ecma_find_empty_or_same_literal_string_slot (ecma_string_t * string_p )
214+ {
173215 jmem_cpointer_t string_list_cp = JERRY_CONTEXT (string_list_first_cp );
174216 jmem_cpointer_t * empty_cpointer_p = NULL ;
175217
@@ -189,42 +231,75 @@ ecma_find_or_create_literal_string (const lit_utf8_byte_t *chars_p, /**< string
189231 else
190232 {
191233 ecma_string_t * value_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_string_t , string_list_p -> values [i ]);
192-
193234 if (ecma_compare_ecma_strings (string_p , value_p ))
194235 {
195- /* Return with string if found in the list. */
196- ecma_deref_ecma_string (string_p );
197- return ecma_make_string_value (value_p );
236+ return string_list_p -> values + i ;
198237 }
199238 }
200239 }
201240
202241 string_list_cp = string_list_p -> next_cp ;
203242 }
204243
205- ECMA_SET_STRING_AS_STATIC (string_p );
206- jmem_cpointer_t result ;
207- JMEM_CP_SET_NON_NULL_POINTER (result , string_p );
208-
209244 if (empty_cpointer_p != NULL )
210245 {
211- * empty_cpointer_p = result ;
212- return ecma_make_string_value (string_p );
246+ return empty_cpointer_p ;
213247 }
214248
215- ecma_lit_storage_item_t * new_item_p ;
216- new_item_p = ( ecma_lit_storage_item_t * ) jmem_pools_alloc ( sizeof ( ecma_lit_storage_item_t ));
249+ return ecma_allocate_new_string_slot () ;
250+ } /* ecma_find_empty_or_same_literal_string_slot */
217251
218- new_item_p -> values [0 ] = result ;
219- for (int i = 1 ; i < ECMA_LIT_STORAGE_VALUE_COUNT ; i ++ )
252+ /**
253+ * Find or create a literal string.
254+ *
255+ * @return ecma_string_t compressed pointer
256+ */
257+
258+ ecma_value_t
259+ ecma_find_or_create_literal_string (const lit_utf8_byte_t * chars_p , /**< string to be searched */
260+ lit_utf8_size_t size , /**< size of the string */
261+ bool is_ascii ) /**< encode of the string */
262+ {
263+ ecma_string_t * string_p =
264+ (is_ascii ? ecma_new_ecma_string_from_ascii (chars_p , size ) : ecma_new_ecma_string_from_utf8 (chars_p , size ));
265+
266+ if (ECMA_IS_DIRECT_STRING (string_p ))
220267 {
221- new_item_p -> values [ i ] = JMEM_CP_NULL ;
268+ return ecma_make_string_value ( string_p ) ;
222269 }
223270
224- new_item_p -> next_cp = JERRY_CONTEXT (string_list_first_cp );
225- JMEM_CP_SET_NON_NULL_POINTER (JERRY_CONTEXT (string_list_first_cp ), new_item_p );
271+ #if JERRY_LIT_HASHMAP
272+ const ecma_string_t * hashmap_entry = hashmap_get (& JERRY_CONTEXT (string_hashmap ), string_p );
273+ if (hashmap_entry != NULL )
274+ {
275+ ecma_deref_ecma_string (string_p );
276+ return ecma_make_string_value (hashmap_entry );
277+ }
278+ // Since the string is not found, just find an empty slot
279+ jmem_cpointer_t * slot = ecma_find_empty_literal_string_slot ();
280+ #else /* JERRY_LIT_HASHMAP */
281+ jmem_cpointer_t * slot = ecma_find_empty_or_same_literal_string_slot (string_p );
282+ if (* slot != JMEM_CP_NULL )
283+ {
284+ // The string has been found
285+ ecma_string_t * value_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_string_t , * slot );
286+ ecma_deref_ecma_string (string_p );
287+ return ecma_make_string_value (value_p );
288+ }
289+ #endif /* JERRY_LIT_HASHMAP */
290+
291+ // String has not been found...
292+ ECMA_SET_STRING_AS_STATIC (string_p );
293+ jmem_cpointer_t result ;
294+ JMEM_CP_SET_NON_NULL_POINTER (result , string_p );
295+ * slot = result ;
296+
297+ #if JERRY_LIT_HASHMAP
298+ hashmap_put (& JERRY_CONTEXT (string_hashmap ), string_p );
299+ #endif /* JERRY_LIT_HASHMAP */
226300
227301 return ecma_make_string_value (string_p );
302+
228303} /* ecma_find_or_create_literal_string */
229304
230305/**
0 commit comments