Skip to content

Replace MACROS with hashmap #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define MAX_STRTAB 65536
#define MAX_HEADER 1024
#define MAX_SECTION 1024
#define MAX_ALIASES 1024
#define MAX_ALIASES 128
#define MAX_CONSTANTS 1024
#define MAX_CASES 128
#define MAX_NESTING 128
Expand Down
37 changes: 21 additions & 16 deletions src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ int macro_return_idx;

/* Global objects */

macro_t *MACROS;
int macros_idx = 0;

/* FUNC_MAP is used to integrate function storing and boost lookup
* performance, currently it uses FNV-1a hash function to hash function
* name.
*/

hashmap_t *MACROS_MAP;
hashmap_t *FUNC_MAP;
hashmap_t *ALIASES_MAP;
hashmap_t *CONSTANTS_MAP;
Expand Down Expand Up @@ -557,28 +556,34 @@ bool remove_alias(char *alias)

macro_t *add_macro(char *name)
{
macro_t *ma = &MACROS[macros_idx++];
strcpy(ma->name, name);
macro_t *ma = hashmap_get(MACROS_MAP, name);
if (!ma) {
ma = malloc(sizeof(macro_t));
if (!ma) {
printf("Failed to allocate macro_t\n");
return NULL;
}
strcpy(ma->name, name);
hashmap_put(MACROS_MAP, name, ma);
}
ma->disabled = false;
return ma;
}

macro_t *find_macro(char *name)
{
for (int i = 0; i < macros_idx; i++) {
if (!MACROS[i].disabled && !strcmp(name, MACROS[i].name))
return &MACROS[i];
}
macro_t *ma = hashmap_get(MACROS_MAP, name);
if (ma && !ma->disabled)
return ma;
return NULL;
}

bool remove_macro(char *name)
{
for (int i = 0; i < macros_idx; i++) {
if (!MACROS[i].disabled && !strcmp(name, MACROS[i].name)) {
MACROS[i].disabled = true;
return true;
}
macro_t *ma = hashmap_get(MACROS_MAP, name);
if (ma) {
ma->disabled = true;
return true;
}
return false;
}
Expand Down Expand Up @@ -974,7 +979,7 @@ void global_init(void)
{
elf_code_start = ELF_START + elf_header_len;

MACROS = malloc(MAX_ALIASES * sizeof(macro_t));
MACROS_MAP = hashmap_create(MAX_ALIASES);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide a analysis on MAX_ALIASES's configuration, that is, which power of 2 to represent it would be more efficient in this case? As you stated, this patch provides memory flexibility for macro_t structure allocation, however, it's obvious that 1024 is overkill for this purpose.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used uftrace to analyze the impact of different MAX_ALIASES configurations and found that setting it to 128 resulted in the fewest hashmap_rehash calls during execution while maintaining the smallest stable size.
The detailed analysis and results are documented in the commit message for reference.

TYPES = malloc(MAX_TYPES * sizeof(type_t));
BLOCK_ARENA = arena_init(DEFAULT_ARENA_SIZE);
INSN_ARENA = arena_init(DEFAULT_ARENA_SIZE);
Expand All @@ -996,7 +1001,7 @@ void global_init(void)

void global_release(void)
{
free(MACROS);
hashmap_free(MACROS_MAP);
free(TYPES);
arena_free(BLOCK_ARENA);
arena_free(INSN_ARENA);
Expand Down