Skip to content

Commit 8fe59b4

Browse files
author
icgmilk
committed
Replace MACROS with hashmap
Introduced 'MACROS_MAP' to replace the original 'MACROS' structure, and refactored related logic with hashmap utilities. To make this change improves memory flexibility and future extensibility, I set 'MAX_ALIASES' to 128. The original value (1024) is excessive, as uftrace analysis shows that the number of aliases rarely exceeds 64 in practice(measured via 'uftrace record ./out/shecc ./src/main.c'), and the total number of '#defines' in 'shecc' is typically below 70. In shecc's hashmap implementation, a rehash is triggered when the number of entries exceeds 75% of the capacity. With this load factor of 0.75, a capacity of 128 supports up to 96 entries before rehashing, which comfortably covers observed usage while providing sufficient headroom. In contrast, a capacity of 64 only allows 48 entries, increasing the likelihood of rehashing. uftrace confirms this: setting 'MAX_ALIASES' to 64 triggered one more 'hashmap_rehash' compared to 128. Therefore, I chose 128 over 64 to avoid unnecessary rehashing while still keeping memory usage significantly lower than the original default.
1 parent 7163605 commit 8fe59b4

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#define MAX_STRTAB 65536
3333
#define MAX_HEADER 1024
3434
#define MAX_SECTION 1024
35-
#define MAX_ALIASES 1024
35+
#define MAX_ALIASES 128
3636
#define MAX_CONSTANTS 1024
3737
#define MAX_CASES 128
3838
#define MAX_NESTING 128

src/globals.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ int macro_return_idx;
2828

2929
/* Global objects */
3030

31-
macro_t *MACROS;
32-
int macros_idx = 0;
33-
3431
/* FUNC_MAP is used to integrate function storing and boost lookup
3532
* performance, currently it uses FNV-1a hash function to hash function
3633
* name.
3734
*/
35+
36+
hashmap_t *MACROS_MAP;
3837
hashmap_t *FUNC_MAP;
3938
hashmap_t *ALIASES_MAP;
4039
hashmap_t *CONSTANTS_MAP;
@@ -557,28 +556,34 @@ bool remove_alias(char *alias)
557556

558557
macro_t *add_macro(char *name)
559558
{
560-
macro_t *ma = &MACROS[macros_idx++];
561-
strcpy(ma->name, name);
559+
macro_t *ma = hashmap_get(MACROS_MAP, name);
560+
if (!ma) {
561+
ma = malloc(sizeof(macro_t));
562+
if (!ma) {
563+
printf("Failed to allocate macro_t\n");
564+
return NULL;
565+
}
566+
strcpy(ma->name, name);
567+
hashmap_put(MACROS_MAP, name, ma);
568+
}
562569
ma->disabled = false;
563570
return ma;
564571
}
565572

566573
macro_t *find_macro(char *name)
567574
{
568-
for (int i = 0; i < macros_idx; i++) {
569-
if (!MACROS[i].disabled && !strcmp(name, MACROS[i].name))
570-
return &MACROS[i];
571-
}
575+
macro_t *ma = hashmap_get(MACROS_MAP, name);
576+
if (ma && !ma->disabled)
577+
return ma;
572578
return NULL;
573579
}
574580

575581
bool remove_macro(char *name)
576582
{
577-
for (int i = 0; i < macros_idx; i++) {
578-
if (!MACROS[i].disabled && !strcmp(name, MACROS[i].name)) {
579-
MACROS[i].disabled = true;
580-
return true;
581-
}
583+
macro_t *ma = hashmap_get(MACROS_MAP, name);
584+
if (ma) {
585+
ma->disabled = true;
586+
return true;
582587
}
583588
return false;
584589
}
@@ -974,7 +979,7 @@ void global_init(void)
974979
{
975980
elf_code_start = ELF_START + elf_header_len;
976981

977-
MACROS = malloc(MAX_ALIASES * sizeof(macro_t));
982+
MACROS_MAP = hashmap_create(MAX_ALIASES);
978983
TYPES = malloc(MAX_TYPES * sizeof(type_t));
979984
BLOCK_ARENA = arena_init(DEFAULT_ARENA_SIZE);
980985
INSN_ARENA = arena_init(DEFAULT_ARENA_SIZE);
@@ -996,7 +1001,7 @@ void global_init(void)
9961001

9971002
void global_release(void)
9981003
{
999-
free(MACROS);
1004+
hashmap_free(MACROS_MAP);
10001005
free(TYPES);
10011006
arena_free(BLOCK_ARENA);
10021007
arena_free(INSN_ARENA);

0 commit comments

Comments
 (0)