Skip to content

Commit ea1b6ab

Browse files
authored
Merge pull request #231 from sysprog21/arena-alloc
Tweak arena allocator memory efficiency
2 parents cc0741f + 621ed76 commit ea1b6ab

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/defs.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@
4040
#define MAX_ANALYSIS_STACK_SIZE 800
4141

4242
/* Default capacities for common data structures */
43-
/* Default arena size is initialized with 256 KiB */
44-
#define DEFAULT_ARENA_SIZE 262144
43+
/* Arena sizes optimized based on typical usage patterns */
44+
#define DEFAULT_ARENA_SIZE 262144 /* 256 KiB - standard default */
45+
#define SMALL_ARENA_SIZE 65536 /* 64 KiB - for small allocations */
46+
#define LARGE_ARENA_SIZE 524288 /* 512 KiB - for instruction arena */
4547
#define DEFAULT_FUNCS_SIZE 64
4648
#define DEFAULT_INCLUSIONS_SIZE 16
4749

@@ -76,6 +78,7 @@ typedef struct arena_block {
7678

7779
typedef struct {
7880
arena_block_t *head;
81+
int total_bytes; /* Track total allocation for profiling */
7982
} arena_t;
8083

8184
/* string-based hash map definitions */

src/globals.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ arena_t *arena_init(int initial_capacity)
143143
abort();
144144
}
145145
arena->head = arena_block_create(initial_capacity);
146+
arena->total_bytes = initial_capacity;
146147
return arena;
147148
}
148149

@@ -171,6 +172,7 @@ void *arena_alloc(arena_t *arena, int size)
171172
arena_block_t *new_block = arena_block_create(new_capacity);
172173
new_block->next = arena->head;
173174
arena->head = new_block;
175+
arena->total_bytes += new_capacity;
174176
}
175177

176178
void *ptr = arena->head->memory + arena->head->offset;
@@ -198,16 +200,8 @@ void *arena_calloc(arena_t *arena, int n, int size)
198200
int total = n * size;
199201
void *ptr = arena_alloc(arena, total);
200202

201-
int *w_ptr = ptr;
202-
int w_count = total >> 2;
203-
int b_index = w_count << 2;
204-
205-
for (int i = 0; i < w_count; ++i)
206-
w_ptr[i] = 0;
207-
208-
char *b_ptr = ptr;
209-
while (b_index < total)
210-
b_ptr[b_index++] = 0;
203+
/* Use memset for better performance */
204+
memset(ptr, 0, total);
211205

212206
return ptr;
213207
}
@@ -1099,11 +1093,11 @@ void global_init(void)
10991093

11001094
MACROS_MAP = hashmap_create(MAX_ALIASES);
11011095
TYPES = malloc(MAX_TYPES * sizeof(type_t));
1102-
BLOCK_ARENA = arena_init(DEFAULT_ARENA_SIZE);
1103-
INSN_ARENA = arena_init(DEFAULT_ARENA_SIZE);
1104-
BB_ARENA = arena_init(DEFAULT_ARENA_SIZE);
1105-
HASHMAP_ARENA = arena_init(DEFAULT_ARENA_SIZE);
1106-
GENERAL_ARENA = arena_init(DEFAULT_ARENA_SIZE);
1096+
BLOCK_ARENA = arena_init(DEFAULT_ARENA_SIZE); /* Variables/blocks */
1097+
INSN_ARENA = arena_init(LARGE_ARENA_SIZE); /* Instructions - high usage */
1098+
BB_ARENA = arena_init(SMALL_ARENA_SIZE); /* Basic blocks - low usage */
1099+
HASHMAP_ARENA = arena_init(DEFAULT_ARENA_SIZE); /* Hash nodes */
1100+
GENERAL_ARENA = arena_init(SMALL_ARENA_SIZE); /* Misc - low usage */
11071101
PH2_IR_FLATTEN = malloc(MAX_IR_INSTR * sizeof(ph2_ir_t *));
11081102
SOURCE = strbuf_create(MAX_SOURCE);
11091103
FUNC_MAP = hashmap_create(DEFAULT_FUNCS_SIZE);

0 commit comments

Comments
 (0)