Skip to content

Commit bd9d1d4

Browse files
Improve allocation of memory for byte-code array and literal identifiers hash table.
- literal identifiers hash table and byte-code array are now allocated in one heap block instead of four blocks; - memory regions with the hash tables and arrays are now linked into linked list using headers at start of the regions instead of using array_list. JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan [email protected]
1 parent 4e56393 commit bd9d1d4

File tree

7 files changed

+154
-80
lines changed

7 files changed

+154
-80
lines changed

jerry-core/parser/js/bytecode-data.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "opcodes.h"
2020
#include "lit-id-hash-table.h"
21+
#include "mem-allocator.h"
2122

2223
/*
2324
* All literals are kept in the 'literals' array.
@@ -35,11 +36,13 @@
3536
#define BLOCK_SIZE 64
3637

3738
/**
38-
* Pointer to lit_id_hash_table precedes every independent bytecode region
39+
* Header of byte-code memory region, containing byte-code array and literal identifiers hash table
3940
*/
4041
typedef struct __attribute__ ((aligned (MEM_ALIGNMENT)))
4142
{
42-
lit_id_hash_table *lit_id_hash;
43+
mem_cpointer_t lit_id_hash_cp; /**< pointer to literal identifiers hash table
44+
* See also: lit_id_hash_table_init */
45+
mem_cpointer_t next_opcodes_cp; /**< pointer to next byte-code memory region */
4346
} opcodes_header_t;
4447

4548
typedef struct
@@ -50,14 +53,15 @@ typedef struct
5053
} bytecode_data_t;
5154

5255
/**
53-
* Macros to get a hash table corresponding to a bytecode region
56+
* Macros to get a pointer to bytecode header by pointer to opcodes start
5457
*/
55-
#define GET_HASH_TABLE_FOR_BYTECODE(opcodes) (((opcodes_header_t *) (((uint8_t *) (opcodes)) - \
56-
sizeof (opcodes_header_t)))->lit_id_hash)
58+
#define GET_BYTECODE_HEADER(opcodes) ((opcodes_header_t *) (((uint8_t *) (opcodes)) - sizeof (opcodes_header_t)))
5759

5860
/**
59-
* Macros to get a pointer to bytecode header by pointer to opcodes start
61+
* Macros to get a hash table corresponding to a bytecode region
6062
*/
61-
#define GET_BYTECODE_HEADER(opcodes) ((opcodes_header_t *) (((uint8_t *) (opcodes)) - sizeof (opcodes_header_t)))
63+
#define GET_HASH_TABLE_FOR_BYTECODE(opcodes) (MEM_CP_GET_POINTER (lit_id_hash_table, \
64+
GET_BYTECODE_HEADER (opcodes)->lit_id_hash_cp))
65+
6266

6367
#endif // BYTECODE_DATA_H

jerry-core/parser/js/collections/lit-id-hash-table.cpp

Lines changed: 96 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,114 @@
1616
#include "lit-id-hash-table.h"
1717
#include "bytecode-data.h"
1818

19+
/** \addtogroup jsparser ECMAScript parser
20+
* @{
21+
*
22+
* \addtogroup collections Collections
23+
* @{
24+
*
25+
* \addtogroup lit_id_hash_table Literal identifiers hash table
26+
* The hash table connects pairs (opcode block, idx_t value) with literal identifiers.
27+
* @{
28+
*/
29+
30+
/**
31+
* Initialize literal identifiers hash table
32+
*
33+
* @return pointer to header of the table
34+
*/
1935
lit_id_hash_table *
20-
lit_id_hash_table_init (size_t buckets_count, size_t blocks_count)
36+
lit_id_hash_table_init (uint8_t *table_buffer_p, /**< buffer to initialize hash table in */
37+
size_t buffer_size, /**< size of the buffer */
38+
size_t buckets_count, /**< number of pairs */
39+
size_t blocks_count) /**< number of opcode blocks */
40+
{
41+
const size_t header_size = JERRY_ALIGNUP (sizeof (lit_id_hash_table), MEM_ALIGNMENT);
42+
const size_t raw_buckets_size = JERRY_ALIGNUP (sizeof (lit_cpointer_t) * buckets_count, MEM_ALIGNMENT);
43+
const size_t buckets_size = JERRY_ALIGNUP (sizeof (lit_cpointer_t*) * blocks_count, MEM_ALIGNMENT);
44+
45+
JERRY_ASSERT (header_size + raw_buckets_size + buckets_size <= buffer_size);
46+
47+
lit_id_hash_table *table_p = (lit_id_hash_table *) table_buffer_p;
48+
49+
table_p->current_bucket_pos = 0;
50+
table_p->raw_buckets = (lit_cpointer_t*) (table_buffer_p + header_size);
51+
table_p->buckets = (lit_cpointer_t **) (table_buffer_p + header_size + raw_buckets_size);
52+
53+
memset (table_p->buckets, 0, buckets_size);
54+
55+
return table_p;
56+
} /* lit_id_hash_table_init */
57+
58+
/**
59+
* Get size of buffer, necessary to hold hash table with specified parameters
60+
*
61+
* @return size of buffer
62+
*/
63+
size_t
64+
lit_id_hash_table_get_size_for_table (size_t buckets_count, /**< number of pairs */
65+
size_t blocks_count) /**< number of opcode blocks */
2166
{
22-
size_t size = mem_heap_recommend_allocation_size (sizeof (lit_id_hash_table));
23-
lit_id_hash_table *table = (lit_id_hash_table *) mem_heap_alloc_block (size, MEM_HEAP_ALLOC_LONG_TERM);
24-
memset (table, 0, size);
25-
size = mem_heap_recommend_allocation_size (sizeof (lit_cpointer_t) * buckets_count);
26-
table->raw_buckets = (lit_cpointer_t *) mem_heap_alloc_block (size, MEM_HEAP_ALLOC_LONG_TERM);
27-
memset (table->raw_buckets, 0, size);
28-
size = mem_heap_recommend_allocation_size (sizeof (lit_cpointer_t *) * blocks_count);
29-
table->buckets = (lit_cpointer_t **) mem_heap_alloc_block (size, MEM_HEAP_ALLOC_LONG_TERM);
30-
memset (table->buckets, 0, size);
31-
table->current_bucket_pos = 0;
32-
return table;
33-
}
67+
const size_t header_size = JERRY_ALIGNUP (sizeof (lit_id_hash_table), MEM_ALIGNMENT);
68+
const size_t raw_buckets_size = JERRY_ALIGNUP (sizeof (lit_cpointer_t) * buckets_count, MEM_ALIGNMENT);
69+
const size_t buckets_size = JERRY_ALIGNUP (sizeof (lit_cpointer_t*) * blocks_count, MEM_ALIGNMENT);
70+
71+
return header_size + raw_buckets_size + buckets_size;
72+
} /* lit_id_hash_table_get_size_for_table */
3473

74+
/**
75+
* Free literal identifiers hash table
76+
*/
3577
void
36-
lit_id_hash_table_free (lit_id_hash_table *table)
78+
lit_id_hash_table_free (lit_id_hash_table *table_p) /**< table's header */
3779
{
38-
JERRY_ASSERT (table);
39-
mem_heap_free_block ((uint8_t *) table->raw_buckets);
40-
mem_heap_free_block ((uint8_t *) table->buckets);
41-
mem_heap_free_block ((uint8_t *) table);
42-
}
80+
JERRY_ASSERT (table_p != NULL);
81+
82+
mem_heap_free_block ((uint8_t *) table_p);
83+
} /* lit_id_hash_table_free */
4384

85+
/**
86+
* Register pair in the hash table
87+
*/
4488
void
45-
lit_id_hash_table_insert (lit_id_hash_table *table, idx_t uid, opcode_counter_t oc, lit_cpointer_t lit_cp)
89+
lit_id_hash_table_insert (lit_id_hash_table *table_p, /**< table's header */
90+
idx_t uid, /**< value of byte-code instruction's argument */
91+
opcode_counter_t oc, /**< opcode counter of the instruction */
92+
lit_cpointer_t lit_cp) /**< literal identifier */
4693
{
47-
JERRY_ASSERT (table);
94+
JERRY_ASSERT (table_p != NULL);
95+
4896
size_t block_id = oc / BLOCK_SIZE;
49-
if (table->buckets[block_id] == NULL)
97+
98+
if (table_p->buckets[block_id] == NULL)
5099
{
51-
table->buckets[block_id] = table->raw_buckets + table->current_bucket_pos;
100+
table_p->buckets[block_id] = table_p->raw_buckets + table_p->current_bucket_pos;
52101
}
53-
table->buckets[block_id][uid] = lit_cp;
54-
table->current_bucket_pos++;
55-
}
56102

103+
table_p->buckets[block_id][uid] = lit_cp;
104+
table_p->current_bucket_pos++;
105+
} /* lit_id_hash_table_insert */
106+
107+
/**
108+
* Lookup literal identifier by pair
109+
*
110+
* @return literal identifier
111+
*/
57112
lit_cpointer_t
58-
lit_id_hash_table_lookup (lit_id_hash_table *table, idx_t uid, opcode_counter_t oc)
113+
lit_id_hash_table_lookup (lit_id_hash_table *table_p, /**< table's header */
114+
idx_t uid, /**< value of byte-code instruction's argument */
115+
opcode_counter_t oc) /**< opcode counter of the instruction */
59116
{
60-
JERRY_ASSERT (table);
117+
JERRY_ASSERT (table_p != NULL);
118+
61119
size_t block_id = oc / BLOCK_SIZE;
62-
JERRY_ASSERT (table->buckets[block_id]);
63-
return table->buckets[block_id][uid];
64-
}
120+
JERRY_ASSERT (table_p->buckets[block_id] != NULL);
121+
122+
return table_p->buckets[block_id][uid];
123+
} /* lit_id_hash_table_lookup */
124+
125+
/**
126+
* @}
127+
* @}
128+
* @}
129+
*/

jerry-core/parser/js/collections/lit-id-hash-table.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ typedef struct
2828
lit_cpointer_t **buckets;
2929
} lit_id_hash_table;
3030

31-
lit_id_hash_table *lit_id_hash_table_init (size_t, size_t);
31+
lit_id_hash_table *lit_id_hash_table_init (uint8_t*, size_t, size_t, size_t);
32+
size_t lit_id_hash_table_get_size_for_table (size_t, size_t);
3233
void lit_id_hash_table_free (lit_id_hash_table *);
3334
void lit_id_hash_table_insert (lit_id_hash_table *, idx_t, opcode_counter_t, lit_cpointer_t);
3435
lit_cpointer_t lit_id_hash_table_lookup (lit_id_hash_table *, idx_t, opcode_counter_t);

jerry-core/parser/js/collections/stack.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,6 @@ static void NAME##_stack_push (TYPE value) { \
117117
NAME.data = array_list_append (NAME.data, &value); \
118118
}
119119

120-
#define DEFINE_CONVERT_TO_RAW_DATA(NAME, TYPE) \
121-
static TYPE *convert_##NAME##_to_raw_data (void) __attr_unused___; \
122-
static TYPE *convert_##NAME##_to_raw_data (void) { \
123-
if (array_list_len (NAME.data) == 0) \
124-
{ \
125-
return NULL; \
126-
} \
127-
size_t size = mem_heap_recommend_allocation_size (array_list_len (NAME.data) * sizeof (NAME##_stack_value_type)); \
128-
TYPE *DATA = (TYPE *) mem_heap_alloc_block (size, MEM_HEAP_ALLOC_LONG_TERM); \
129-
if (DATA == NULL) \
130-
{ \
131-
printf ("Out of memory\n"); \
132-
JERRY_UNREACHABLE (); \
133-
} \
134-
memcpy (DATA, array_list_element (NAME.data, 0), array_list_len (NAME.data) * sizeof (NAME##_stack_value_type)); \
135-
return DATA; \
136-
}
137-
138120
#define STACK_PUSH(NAME, VALUE) \
139121
do { NAME##_stack_push (VALUE); } while (0)
140122

@@ -207,7 +189,6 @@ NAME##_stack NAME; \
207189
DEFINE_STACK_ELEMENT (NAME, TYPE) \
208190
DEFINE_SET_STACK_ELEMENT (NAME, TYPE) \
209191
DEFINE_STACK_HEAD (NAME, TYPE) \
210-
DEFINE_CONVERT_TO_RAW_DATA (NAME, TYPE) \
211192
DEFINE_SET_STACK_HEAD (NAME, TYPE) \
212193
DEFINE_STACK_PUSH (NAME, TYPE)
213194

@@ -217,7 +198,6 @@ static NAME##_stack NAME; \
217198
DEFINE_STACK_ELEMENT (NAME, TYPE) \
218199
DEFINE_SET_STACK_ELEMENT (NAME, TYPE) \
219200
DEFINE_STACK_HEAD (NAME, TYPE) \
220-
DEFINE_CONVERT_TO_RAW_DATA (NAME, TYPE) \
221201
DEFINE_SET_STACK_HEAD (NAME, TYPE) \
222202
DEFINE_STACK_PUSH (NAME, TYPE)
223203

jerry-core/parser/js/scopes-tree.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,10 @@ merge_subscopes (scopes_tree tree, opcode_t *data, lit_id_hash_table *lit_ids)
600600
Reorder function declarations.
601601
Rewrite opcodes' temporary uids with their keys in literal indexes 'hash' table. */
602602
opcode_t *
603-
scopes_tree_raw_data (scopes_tree tree, lit_id_hash_table *lit_ids)
603+
scopes_tree_raw_data (scopes_tree tree, /**< scopes tree to convert to byte-code array */
604+
uint8_t *buffer_p, /**< buffer for byte-code array and literal identifiers hash table */
605+
size_t opcodes_array_size, /**< size of space for byte-code array */
606+
lit_id_hash_table *lit_ids) /**< literal identifiers hash table */
604607
{
605608
JERRY_ASSERT (lit_ids);
606609
assert_tree (tree);
@@ -613,20 +616,24 @@ scopes_tree_raw_data (scopes_tree tree, lit_id_hash_table *lit_ids)
613616
global_oc = 0;
614617

615618
/* Dump bytecode and fill literal indexes 'hash' table. */
616-
// +1 for valgrind
617-
size_t size = sizeof (opcodes_header_t) + (size_t) (scopes_tree_count_opcodes (tree) + 1) * sizeof (opcode_t);
618-
opcodes_header_t *opcodes_data = (opcodes_header_t *) mem_heap_alloc_block (size, MEM_HEAP_ALLOC_LONG_TERM);
619-
memset (opcodes_data, 0, size);
619+
JERRY_ASSERT (opcodes_array_size >=
620+
sizeof (opcodes_header_t) + (size_t) (scopes_tree_count_opcodes (tree)) * sizeof (opcode_t));
621+
622+
opcodes_header_t *opcodes_data = (opcodes_header_t *) buffer_p;
623+
memset (opcodes_data, 0, opcodes_array_size);
624+
620625
opcode_t *opcodes = (opcode_t *)(((uint8_t*) opcodes_data) + sizeof (opcodes_header_t));
621626
merge_subscopes (tree, opcodes, lit_ids);
622627
if (lit_id_to_uid != null_hash)
623628
{
624629
hash_table_free (lit_id_to_uid);
625630
lit_id_to_uid = null_hash;
626631
}
627-
opcodes_data->lit_id_hash = lit_ids;
632+
633+
MEM_CP_SET_POINTER (opcodes_data->lit_id_hash_cp, lit_ids);
634+
628635
return opcodes;
629-
}
636+
} /* scopes_tree_raw_data */
630637

631638
void
632639
scopes_tree_set_strict_mode (scopes_tree tree, bool strict_mode)

jerry-core/parser/js/scopes-tree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void scopes_tree_set_opcodes_num (scopes_tree, opcode_counter_t);
5858
op_meta scopes_tree_op_meta (scopes_tree, opcode_counter_t);
5959
size_t scopes_tree_count_literals_in_blocks (scopes_tree);
6060
opcode_counter_t scopes_tree_count_opcodes (scopes_tree);
61-
opcode_t *scopes_tree_raw_data (scopes_tree, lit_id_hash_table *);
61+
opcode_t *scopes_tree_raw_data (scopes_tree, uint8_t *, size_t, lit_id_hash_table *);
6262
void scopes_tree_set_strict_mode (scopes_tree, bool);
6363
bool scopes_tree_strict_mode (scopes_tree);
6464

jerry-core/parser/js/serializer.cpp

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
static bytecode_data_t bytecode_data;
2222
static scopes_tree current_scope;
23-
static array_list bytecodes_cache; /**< storage of pointers to byetecodes */
2423
static bool print_opcodes;
2524

2625
static void
@@ -83,18 +82,37 @@ const opcode_t *
8382
serializer_merge_scopes_into_bytecode (void)
8483
{
8584
bytecode_data.opcodes_count = scopes_tree_count_opcodes (current_scope);
86-
lit_id_hash_table *lit_id_hash = lit_id_hash_table_init (scopes_tree_count_literals_in_blocks (current_scope),
87-
(size_t) bytecode_data.opcodes_count / BLOCK_SIZE + 1);
88-
bytecode_data.opcodes = scopes_tree_raw_data (current_scope, lit_id_hash);
89-
bytecodes_cache = array_list_append (bytecodes_cache, &bytecode_data.opcodes);
85+
86+
const size_t buckets_count = scopes_tree_count_literals_in_blocks (current_scope);
87+
const size_t blocks_count = (size_t) bytecode_data.opcodes_count / BLOCK_SIZE + 1;
88+
const size_t opcodes_count = scopes_tree_count_opcodes (current_scope);
89+
90+
const size_t opcodes_array_size = JERRY_ALIGNUP (sizeof (opcodes_header_t) + opcodes_count * sizeof (opcode_t),
91+
MEM_ALIGNMENT);
92+
const size_t lit_id_hash_table_size = JERRY_ALIGNUP (lit_id_hash_table_get_size_for_table (buckets_count,
93+
blocks_count),
94+
MEM_ALIGNMENT);
95+
96+
uint8_t *buffer_p = (uint8_t*) mem_heap_alloc_block (opcodes_array_size + lit_id_hash_table_size,
97+
MEM_HEAP_ALLOC_LONG_TERM);
98+
99+
lit_id_hash_table *lit_id_hash = lit_id_hash_table_init (buffer_p + opcodes_array_size,
100+
lit_id_hash_table_size,
101+
buckets_count, blocks_count);
102+
103+
const opcode_t *opcodes_p = scopes_tree_raw_data (current_scope, buffer_p, opcodes_array_size, lit_id_hash);
104+
105+
opcodes_header_t *header_p = (opcodes_header_t*) buffer_p;
106+
MEM_CP_SET_POINTER (header_p->next_opcodes_cp, bytecode_data.opcodes);
107+
bytecode_data.opcodes = opcodes_p;
90108

91109
if (print_opcodes)
92110
{
93111
lit_dump_literals ();
94-
serializer_print_opcodes (bytecode_data.opcodes, bytecode_data.opcodes_count);
112+
serializer_print_opcodes (opcodes_p, bytecode_data.opcodes_count);
95113
}
96114

97-
return bytecode_data.opcodes;
115+
return opcodes_p;
98116
}
99117

100118
void
@@ -176,8 +194,6 @@ serializer_init ()
176194
bytecode_data.opcodes = NULL;
177195

178196
lit_init ();
179-
180-
bytecodes_cache = array_list_init (sizeof (opcode_t *));
181197
}
182198

183199
void serializer_set_show_opcodes (bool show_opcodes)
@@ -195,10 +211,11 @@ serializer_free (void)
195211

196212
lit_finalize ();
197213

198-
for (size_t i = 0; i < array_list_len (bytecodes_cache); ++i)
214+
while (bytecode_data.opcodes != NULL)
199215
{
200-
lit_id_hash_table_free (GET_BYTECODE_HEADER (*(opcode_t **) array_list_element (bytecodes_cache, i))->lit_id_hash);
201-
mem_heap_free_block (GET_BYTECODE_HEADER (*(opcode_t **) array_list_element (bytecodes_cache, i)));
216+
opcodes_header_t *header_p = GET_BYTECODE_HEADER (bytecode_data.opcodes);
217+
bytecode_data.opcodes = MEM_CP_GET_POINTER (opcode_t, header_p->next_opcodes_cp);
218+
219+
mem_heap_free_block (header_p);
202220
}
203-
array_list_free (bytecodes_cache);
204221
}

0 commit comments

Comments
 (0)