Skip to content

Commit 4a5192e

Browse files
committed
Replace trie with general string-based-key hashmap
Previously, trie implementation is not consistent, mainly because of using index to point the referencing func_t to FUNCS, additionally, trie's advantage is that enables prefix lookup, but in shecc, it hasn't been used in this way, furthur more, it takes 512 bytes per trie node, while in this implementation, it 24 + W (W stands for key length including NULL character) bytes per hashmap bucket node, which significantly reduces memory usage. This also allows for future refactoring of additional structures using a hashmap implementation.
1 parent 09bb918 commit 4a5192e

File tree

2 files changed

+155
-84
lines changed

2 files changed

+155
-84
lines changed

src/defs.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#define MAX_LOCALS 1500
2121
#define MAX_FIELDS 64
2222
#define MAX_FUNCS 512
23-
#define MAX_FUNC_TRIES 2160
2423
#define MAX_TYPES 64
2524
#define MAX_IR_INSTR 50000
2625
#define MAX_BB_PRED 128
@@ -305,10 +304,18 @@ typedef struct {
305304
int value;
306305
} constant_t;
307306

307+
/* string-based hash map definitions */
308+
309+
typedef struct hashmap_node {
310+
char *key;
311+
void *val;
312+
struct hashmap_node *next;
313+
} hashmap_node_t;
314+
308315
typedef struct {
309-
int index;
310-
int next[128];
311-
} trie_t;
316+
int size;
317+
hashmap_node_t **buckets;
318+
} hashmap_t;
312319

313320
struct phi_operand {
314321
var_t *var;

src/globals.c

+144-80
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,13 @@ block_list_t BLOCKS;
1515
macro_t *MACROS;
1616
int macros_idx = 0;
1717

18-
/* the first element is reserved for global scope */
19-
func_t *FUNCS;
20-
int funcs_idx = 1;
21-
22-
/* FUNC_TRIES is used to improve the performance of the find_func function.
23-
* Instead of searching through all functions and comparing their names, we can
24-
* utilize the trie data structure to search for existing functions efficiently.
25-
* The index starts from 1 because the first trie node represents an empty input
26-
* string, and it is not possible to record a function with an empty name.
18+
/* FUNCS_MAP is used to integerate function storing and boost lookup
19+
* performance, currently it uses FNV-1a hash function to hash function
20+
* name. The bucket size defaults to MAX_FUNCS. Ideally, it should be a small
21+
* number, but due to lack of rehashing implementation, to prevent collision,
22+
* we have to initially create large amount of buckets.
2723
*/
28-
trie_t *FUNC_TRIES;
29-
int func_tries_idx = 1;
24+
hashmap_t *FUNCS_MAP;
3025

3126
type_t *TYPES;
3227
int types_idx = 0;
@@ -75,72 +70,143 @@ char *elf_strtab;
7570
char *elf_section;
7671

7772
/**
78-
* insert_trie() - Inserts a new element into the trie structure.
79-
* @trie: A pointer to the trie where the name will be inserted.
80-
* @name: The name to be inserted into the trie.
81-
* @funcs_index: The index of the pointer to the func_t. The index is recorded
82-
* in a 1-indexed format. Because the first element of 'FUNCS' has been
83-
* reserved, there is no need to shift it.
84-
* Return: The index of the pointer to the func_t.
73+
* hashmap_hash_index() - hashses a string with FNV-1a hash function
74+
* and converts into usable hashmap index. The range of returned
75+
* hashmap index is ranged from "(0 ~ 2,147,483,647) mod size" due to
76+
* lack of unsigned integer implementation.
77+
* @size: The size of map. Must not be negative or 0.
78+
* @key: The key string. May be NULL.
8579
*
86-
* If the function has been inserted, the return value is the index of the
87-
* function in FUNCS. Otherwise, the return value is the value of the parameter
88-
* @funcs_index.
80+
* @returns: The usable hashmap index.
8981
*/
90-
int insert_trie(trie_t *trie, char *name, int funcs_index)
91-
{
92-
char first_char;
93-
int fc;
94-
95-
while (1) {
96-
first_char = *name;
97-
fc = first_char;
98-
if (!fc) {
99-
if (!trie->index)
100-
trie->index = funcs_index;
101-
return trie->index;
102-
}
103-
if (!trie->next[fc]) {
104-
/* FIXME: The func_tries_idx variable may exceed the maximum number,
105-
* which can lead to a segmentation fault. This issue is affected by
106-
* the number of functions and the length of their names. The proper
107-
* way to handle this is to dynamically allocate a new element.
108-
*/
109-
trie->next[fc] = func_tries_idx++;
110-
for (int i = 0; i < 128; i++)
111-
FUNC_TRIES[trie->next[fc]].next[i] = 0;
112-
FUNC_TRIES[trie->next[fc]].index = 0;
113-
}
114-
trie = &FUNC_TRIES[trie->next[fc]];
115-
name++;
82+
int hashmap_hash_index(int size, char *key)
83+
{
84+
int hash = 0x811c9dc5, mask;
85+
86+
for (; *key; key++) {
87+
hash ^= *key;
88+
hash *= 0x01000193;
89+
}
90+
91+
mask = hash >> 31;
92+
return ((hash ^ mask) - mask) % size;
93+
}
94+
95+
/**
96+
* hashmap_create() - creates a hashmap on heap.
97+
* @size: The initial bucket size of hashmap. Must not be 0.
98+
*
99+
* @returns: The pointer of created hashmap.
100+
*/
101+
hashmap_t *hashmap_create(int size)
102+
{
103+
hashmap_t *map = malloc(sizeof(hashmap_t));
104+
map->size = size;
105+
map->buckets = malloc(size * sizeof(hashmap_node_t *));
106+
107+
for (int i = 0; i < map->size; i++)
108+
map->buckets[i] = 0;
109+
110+
return map;
111+
}
112+
113+
/**
114+
* hashmap_node_new() - creates a hashmap node on heap.
115+
* @key: The key of node. Must not be NULL.
116+
* @val: The value of node. Could be NULL.
117+
*
118+
* @returns: The pointer of created node.
119+
*/
120+
hashmap_node_t *hashmap_node_new(char *key, void *val)
121+
{
122+
int len = strlen(key);
123+
hashmap_node_t *node = malloc(sizeof(hashmap_node_t));
124+
node->key = calloc(len + 1, sizeof(char));
125+
strcpy(node->key, key);
126+
node->val = val;
127+
node->next = NULL;
128+
return node;
129+
}
130+
131+
/**
132+
* hashmap_put() - puts a key-value pair into given hashmap.
133+
* If key already contains a value, then replace it with new
134+
* value, the old value will be freed.
135+
* @map: The hashmap to be put into. Must not be NULL.
136+
* @key: The key string. May be NULL.
137+
* @val: The value pointer. May be NULL. This value's lifetime
138+
* is held by hashmap.
139+
*/
140+
void hashmap_put(hashmap_t *map, char *key, void *val)
141+
{
142+
int index = hashmap_hash_index(map->size, key);
143+
hashmap_node_t *cur = map->buckets[index];
144+
145+
if (!cur) {
146+
map->buckets[index] = hashmap_node_new(key, val);
147+
} else {
148+
while (cur->next)
149+
cur = cur->next;
150+
cur->next = hashmap_node_new(key, val);
116151
}
152+
153+
/* TODO: Rehash if size exceeds size * load factor */
117154
}
118155

119156
/**
120-
* find_trie() - search the index of the function name in the trie
121-
* @trie: A pointer to the trie where the name will be searched.
122-
* @name: The name to be searched.
157+
* hashmap_get() - gets value from hashmap from given key.
158+
* @map: The hashmap to be looked up. Must no be NULL.
159+
* @key: The key string. May be NULL.
123160
*
124-
* Return: The index of the pointer to the func_t.
161+
* @returns: The look up result, if the key-value pair entry
162+
* exists, then returns its value's address, NULL otherwise.
163+
*/
164+
void *hashmap_get(hashmap_t *map, char *key)
165+
{
166+
int index = hashmap_hash_index(map->size, key);
167+
168+
for (hashmap_node_t *cur = map->buckets[index]; cur; cur = cur->next)
169+
if (!strcmp(cur->key, key))
170+
return cur->val;
171+
172+
return NULL;
173+
}
174+
175+
/**
176+
* hashmap_contains() - checks if the key-value pair entry exists
177+
* from given key.
178+
* @map: The hashmap to be looked up. Must no be NULL.
179+
* @key: The key string. May be NULL.
125180
*
126-
* 0 - the name not found.
127-
* otherwise - the index of the founded index in the trie array.
181+
* @returns: The look up result, if the key-value pair entry
182+
* exists, then returns true, false otherwise.
183+
*/
184+
bool hashmap_contains(hashmap_t *map, char *key)
185+
{
186+
return hashmap_get(map, key);
187+
}
188+
189+
/**
190+
* hashmap_free() - frees the hashmap, this also frees key-value pair
191+
* entry's value.
192+
* @map: The hashmap to be looked up. Must no be NULL.
128193
*/
129-
int find_trie(trie_t *trie, char *name)
130-
{
131-
char first_char;
132-
int fc;
133-
134-
while (1) {
135-
first_char = *name;
136-
fc = first_char;
137-
if (!fc)
138-
return trie->index;
139-
if (!trie->next[fc])
140-
return 0;
141-
trie = &FUNC_TRIES[trie->next[fc]];
142-
name++;
194+
void hashmap_free(hashmap_t *map)
195+
{
196+
for (int i = 0; i < map->size; i++) {
197+
for (hashmap_node_t *cur = map->buckets[i], *next; cur; cur = next) {
198+
next = cur->next;
199+
free(cur->key);
200+
free(cur->val);
201+
/* FIXME: Remove this if-clause will cause double free error */
202+
if (cur != map->buckets[0])
203+
free(cur);
204+
cur = next;
205+
}
143206
}
207+
208+
free(map->buckets);
209+
free(map);
144210
}
145211

146212
/* options */
@@ -321,12 +387,14 @@ int find_macro_param_src_idx(char *name, block_t *parent)
321387
func_t *add_func(char *name)
322388
{
323389
func_t *fn;
324-
int index = insert_trie(FUNC_TRIES, name, funcs_idx);
325-
if (index == funcs_idx) {
326-
fn = &FUNCS[funcs_idx++];
390+
if (hashmap_contains(FUNCS_MAP, name)) {
391+
fn = hashmap_get(FUNCS_MAP, name);
392+
} else {
393+
fn = malloc(sizeof(func_t));
394+
hashmap_put(FUNCS_MAP, name, fn);
327395
strcpy(fn->return_def.var_name, name);
328396
}
329-
fn = &FUNCS[index];
397+
330398
fn->stack_size = 4; /* starting point of stack */
331399
return fn;
332400
}
@@ -361,10 +429,7 @@ constant_t *find_constant(char alias[])
361429

362430
func_t *find_func(char func_name[])
363431
{
364-
int index = find_trie(FUNC_TRIES, func_name);
365-
if (index)
366-
return &FUNCS[index];
367-
return NULL;
432+
return hashmap_get(FUNCS_MAP, func_name);
368433
}
369434

370435
var_t *find_member(char token[], type_t *type)
@@ -600,8 +665,7 @@ void global_init()
600665
BLOCKS.head = NULL;
601666
BLOCKS.tail = NULL;
602667
MACROS = malloc(MAX_ALIASES * sizeof(macro_t));
603-
FUNCS = malloc(MAX_FUNCS * sizeof(func_t));
604-
FUNC_TRIES = malloc(MAX_FUNC_TRIES * sizeof(trie_t));
668+
FUNCS_MAP = hashmap_create(MAX_FUNCS);
605669
TYPES = malloc(MAX_TYPES * sizeof(type_t));
606670
GLOBAL_IR = malloc(MAX_GLOBAL_IR * sizeof(ph1_ir_t));
607671
PH1_IR = malloc(MAX_IR_INSTR * sizeof(ph1_ir_t));
@@ -619,7 +683,8 @@ void global_init()
619683
elf_section = malloc(MAX_SECTION);
620684

621685
/* set starting point of global stack manually */
622-
FUNCS[0].stack_size = 4;
686+
func_t *global_func = add_func("");
687+
global_func->stack_size = 4;
623688
}
624689

625690
void global_release()
@@ -630,8 +695,7 @@ void global_release()
630695
BLOCKS.head = next;
631696
}
632697
free(MACROS);
633-
free(FUNCS);
634-
free(FUNC_TRIES);
698+
hashmap_free(FUNCS_MAP);
635699
free(TYPES);
636700
free(GLOBAL_IR);
637701
free(PH1_IR);

0 commit comments

Comments
 (0)