Skip to content

Commit

Permalink
Reduce memory allocation frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
arithy committed Apr 29, 2022
1 parent b3f7454 commit 142660f
Showing 1 changed file with 48 additions and 33 deletions.
81 changes: 48 additions & 33 deletions src/packcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static size_t strnlen_(const char *str, size_t maxlen) {
#undef TRUE /* to avoid macro definition conflicts with the system header file of IBM AIX */
#undef FALSE

#define VERSION "1.7.2"
#define VERSION "1.8.0"

#ifndef BUFFER_MIN_SIZE
#define BUFFER_MIN_SIZE 256
Expand Down Expand Up @@ -3374,6 +3374,10 @@ static bool_t generate(context_t *ctx) {
"#define PCC_ARRAY_MIN_SIZE 2\n"
"#endif /* !PCC_ARRAY_MIN_SIZE */\n"
"\n"
"#ifndef PCC_POOL_MIN_SIZE\n"
"#define PCC_POOL_MIN_SIZE 65536\n"
"#endif /* !PCC_POOL_MIN_SIZE */\n"
"\n"
"#define PCC_DBG_EVALUATE 0\n"
"#define PCC_DBG_MATCH 1\n"
"#define PCC_DBG_NOMATCH 2\n"
Expand Down Expand Up @@ -3577,16 +3581,23 @@ static bool_t generate(context_t *ctx) {
);
stream__puts(
&sstream,
"typedef struct pcc_recycling_list_tag {\n"
" struct pcc_recycling_list_tag *next;\n"
"} pcc_recycling_list_t;\n"
"typedef struct pcc_memory_entry_tag pcc_memory_entry_t;\n"
"typedef struct pcc_memory_pool_tag pcc_memory_pool_t;\n"
"\n"
"struct pcc_memory_entry_tag {\n"
" pcc_memory_entry_t *next;\n"
"};\n"
"\n"
"struct pcc_memory_pool_tag {\n"
" pcc_memory_pool_t *next;\n"
" size_t allocated;\n"
" size_t unused;\n"
"};\n"
"\n"
"typedef struct pcc_memory_recycler_tag {\n"
" pcc_recycling_list_t *list;\n"
" pcc_memory_pool_t *pool_list;\n"
" pcc_memory_entry_t *entry_list;\n"
" size_t element_size;\n"
" size_t allocated;\n"
" size_t inuse;\n"
" void *base;\n"
"} pcc_memory_recycler_t;\n"
"\n"
);
Expand Down Expand Up @@ -3894,45 +3905,49 @@ static bool_t generate(context_t *ctx) {
stream__puts(
&sstream,
"static void pcc_memory_recycler__init(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, size_t element_size) {\n"
" recycler->list = NULL;\n"
" recycler->pool_list = NULL;\n"
" recycler->entry_list = NULL;\n"
" recycler->element_size = element_size;\n"
" recycler->allocated = 1024 * 1024;\n"
" recycler->inuse = 0;\n"
" recycler->base = PCC_MALLOC(auxil, recycler->element_size * recycler->allocated);\n"
"}\n"
"\n"
"static void *pcc_memory_recycler__supply(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler) {\n"
" if (recycler->list) {\n"
" pcc_recycling_list_t *const tmp = recycler->list;\n"
" recycler->list = tmp->next;\n"
" if (recycler->entry_list) {\n"
" pcc_memory_entry_t *const tmp = recycler->entry_list;\n"
" recycler->entry_list = tmp->next;\n"
" return tmp;\n"
" }\n"
" if (recycler->inuse < recycler->allocated) {\n"
" char *const base = recycler->base;\n"
" char *const tmp = base + (recycler->inuse * recycler->element_size);\n"
" recycler->inuse++;\n"
" return tmp;\n"
" if (!recycler->pool_list || recycler->pool_list->unused == 0) {\n"
" size_t size = PCC_POOL_MIN_SIZE;\n"
" if (recycler->pool_list) {\n"
" size = recycler->pool_list->allocated << 1;\n"
" if (size == 0) size = recycler->pool_list->allocated;\n"
" }\n"
" {\n"
" pcc_memory_pool_t *const pool = (pcc_memory_pool_t *)PCC_MALLOC(\n"
" auxil, sizeof(pcc_memory_pool_t) + recycler->element_size * size\n"
" );\n"
" pool->allocated = size;\n"
" pool->unused = size;\n"
" pool->next = recycler->pool_list;\n"
" recycler->pool_list = pool;\n"
" }\n"
" }\n"
" return PCC_MALLOC(auxil, recycler->element_size);\n"
" recycler->pool_list->unused--;\n"
" return (char *)recycler->pool_list + sizeof(pcc_memory_pool_t) + recycler->element_size * recycler->pool_list->unused;\n"
"}\n"
"\n"
"static void pcc_memory_recycler__recycle(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, void *obj) {\n"
" pcc_recycling_list_t *const tmp = obj;\n"
" tmp->next = recycler->list;\n"
" recycler->list = tmp;\n"
"static void pcc_memory_recycler__recycle(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, void *ptr) {\n"
" pcc_memory_entry_t *const tmp = (pcc_memory_entry_t *)ptr;\n"
" tmp->next = recycler->entry_list;\n"
" recycler->entry_list = tmp;\n"
"}\n"
"\n"
"static void pcc_memory_recycler__term(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler) {\n"
" char *const start = recycler->base;\n"
" char *const end = (char *)recycler->base + (recycler->allocated * recycler->element_size);\n"
" while (recycler->list) {\n"
" pcc_recycling_list_t *const tmp = recycler->list;\n"
" recycler->list = tmp->next;\n"
" if (start <= (char *)tmp && (char *)tmp < end)\n"
" continue;\n"
" while (recycler->pool_list) {\n"
" pcc_memory_pool_t *const tmp = recycler->pool_list;\n"
" recycler->pool_list = tmp->next;\n"
" PCC_FREE(auxil, tmp);\n"
" }\n"
" PCC_FREE(auxil, recycler->base);\n"
"}\n"
"\n"
);
Expand Down

0 comments on commit 142660f

Please sign in to comment.