From 7cd6dffb7e3cd5e9bb93d5398c5902fc36537b7e Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Mon, 25 Apr 2022 19:11:34 +0900 Subject: [PATCH 1/2] Pass pcc_context_t instead of pcc_auxil_t in many places This is preparation for the next change reducing malloc. Signed-off-by: Masatake YAMATO --- src/packcc.c | 188 ++++++++++++++++++++++++++------------------------- 1 file changed, 97 insertions(+), 91 deletions(-) diff --git a/src/packcc.c b/src/packcc.c index 601688c..4ee05a2 100644 --- a/src/packcc.c +++ b/src/packcc.c @@ -3873,26 +3873,27 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); - stream__puts( + stream__printf( &sstream, "MARK_USED_FUNC\n" - "static pcc_thunk_chunk_t *pcc_thunk_chunk__create(pcc_auxil_t auxil) {\n" - " pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_chunk_t));\n" - " pcc_value_table__init(auxil, &chunk->values);\n" - " pcc_capture_table__init(auxil, &chunk->capts);\n" - " pcc_thunk_array__init(auxil, &chunk->thunks);\n" + "static pcc_thunk_chunk_t *pcc_thunk_chunk__create(%s_context_t *ctx) {\n" + " pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_thunk_chunk_t));\n" + " pcc_value_table__init(ctx->auxil, &chunk->values);\n" + " pcc_capture_table__init(ctx->auxil, &chunk->capts);\n" + " pcc_thunk_array__init(ctx->auxil, &chunk->thunks);\n" " chunk->pos = 0;\n" " return chunk;\n" "}\n" "\n" - "static void pcc_thunk_chunk__destroy(pcc_auxil_t auxil, pcc_thunk_chunk_t *chunk) {\n" + "static void pcc_thunk_chunk__destroy(%s_context_t *ctx, pcc_thunk_chunk_t *chunk) {\n" " if (chunk == NULL) return;\n" - " pcc_thunk_array__term(auxil, &chunk->thunks);\n" - " pcc_capture_table__term(auxil, &chunk->capts);\n" - " pcc_value_table__term(auxil, &chunk->values);\n" - " PCC_FREE(auxil, chunk);\n" + " pcc_thunk_array__term(ctx->auxil, &chunk->thunks);\n" + " pcc_capture_table__term(ctx->auxil, &chunk->capts);\n" + " pcc_value_table__term(ctx->auxil, &chunk->values);\n" + " PCC_FREE(ctx->auxil, chunk);\n" "}\n" - "\n" + "\n", + get_prefix(ctx), get_prefix(ctx) ); stream__puts( &sstream, @@ -3950,32 +3951,33 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); - stream__puts( + stream__printf( &sstream, - "static pcc_lr_head_t *pcc_lr_head__create(pcc_auxil_t auxil, pcc_rule_t rule) {\n" - " pcc_lr_head_t *const head = (pcc_lr_head_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_head_t));\n" + "static pcc_lr_head_t *pcc_lr_head__create(%s_context_t *ctx, pcc_rule_t rule) {\n" + " pcc_lr_head_t *const head = (pcc_lr_head_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_lr_head_t));\n" " head->rule = rule;\n" - " pcc_rule_set__init(auxil, &head->invol);\n" - " pcc_rule_set__init(auxil, &head->eval);\n" + " pcc_rule_set__init(ctx->auxil, &head->invol);\n" + " pcc_rule_set__init(ctx->auxil, &head->eval);\n" " head->hold = NULL;\n" " return head;\n" "}\n" "\n" - "static void pcc_lr_head__destroy(pcc_auxil_t auxil, pcc_lr_head_t *head) {\n" + "static void pcc_lr_head__destroy(%s_context_t *ctx, pcc_lr_head_t *head) {\n" " if (head == NULL) return;\n" - " pcc_lr_head__destroy(auxil, head->hold);\n" - " pcc_rule_set__term(auxil, &head->eval);\n" - " pcc_rule_set__term(auxil, &head->invol);\n" - " PCC_FREE(auxil, head);\n" + " pcc_lr_head__destroy(ctx, head->hold);\n" + " pcc_rule_set__term(ctx->auxil, &head->eval);\n" + " pcc_rule_set__term(ctx->auxil, &head->invol);\n" + " PCC_FREE(ctx->auxil, head);\n" "}\n" - "\n" + "\n", + get_prefix(ctx), get_prefix(ctx) ); - stream__puts( + stream__printf( &sstream, "static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr);\n" "\n" - "static pcc_lr_answer_t *pcc_lr_answer__create(pcc_auxil_t auxil, pcc_lr_answer_type_t type, size_t pos) {\n" - " pcc_lr_answer_t *answer = (pcc_lr_answer_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_answer_t));\n" + "static pcc_lr_answer_t *pcc_lr_answer__create(%s_context_t *ctx, pcc_lr_answer_type_t type, size_t pos) {\n" + " pcc_lr_answer_t *answer = (pcc_lr_answer_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_lr_answer_t));\n" " answer->type = type;\n" " answer->pos = pos;\n" " answer->hold = NULL;\n" @@ -3987,14 +3989,14 @@ static bool_t generate(context_t *ctx) { " answer->data.chunk = NULL;\n" " break;\n" " default: /* unknown */\n" - " PCC_FREE(auxil, answer);\n" + " PCC_FREE(ctx->auxil, answer);\n" " answer = NULL;\n" " }\n" " return answer;\n" "}\n" "\n" - "static void pcc_lr_answer__set_chunk(pcc_auxil_t auxil, pcc_lr_answer_t *answer, pcc_thunk_chunk_t *chunk) {\n" - " pcc_lr_answer_t *const a = pcc_lr_answer__create(auxil, answer->type, answer->pos);\n" + "static void pcc_lr_answer__set_chunk(%s_context_t *ctx, pcc_lr_answer_t *answer, pcc_thunk_chunk_t *chunk) {\n" + " pcc_lr_answer_t *const a = pcc_lr_answer__create(ctx, answer->type, answer->pos);\n" " switch (answer->type) {\n" " case PCC_LR_ANSWER_LR:\n" " a->data.lr = answer->data.lr;\n" @@ -4011,26 +4013,27 @@ static bool_t generate(context_t *ctx) { " answer->data.chunk = chunk;\n" "}\n" "\n" - "static void pcc_lr_answer__destroy(pcc_auxil_t auxil, pcc_lr_answer_t *answer) {\n" + "static void pcc_lr_answer__destroy(%s_context_t *ctx, pcc_lr_answer_t *answer) {\n" " while (answer != NULL) {\n" " pcc_lr_answer_t *const a = answer->hold;\n" " switch (answer->type) {\n" " case PCC_LR_ANSWER_LR:\n" - " pcc_lr_entry__destroy(auxil, answer->data.lr);\n" + " pcc_lr_entry__destroy(ctx->auxil, answer->data.lr);\n" " break;\n" " case PCC_LR_ANSWER_CHUNK:\n" - " pcc_thunk_chunk__destroy(auxil, answer->data.chunk);\n" + " pcc_thunk_chunk__destroy(ctx, answer->data.chunk);\n" " break;\n" " default: /* unknown */\n" " break;\n" " }\n" - " PCC_FREE(auxil, answer);\n" + " PCC_FREE(ctx->auxil, answer);\n" " answer = a;\n" " }\n" "}\n" - "\n" + "\n", + get_prefix(ctx), get_prefix(ctx), get_prefix(ctx) ); - stream__puts( + stream__printf( &sstream, "static void pcc_lr_memo_map__init(pcc_auxil_t auxil, pcc_lr_memo_map_t *map) {\n" " map->len = 0;\n" @@ -4046,10 +4049,10 @@ static bool_t generate(context_t *ctx) { " return PCC_VOID_VALUE;\n" "}\n" "\n" - "static void pcc_lr_memo_map__put(pcc_auxil_t auxil, pcc_lr_memo_map_t *map, pcc_rule_t rule, pcc_lr_answer_t *answer) {\n" - " const size_t i = pcc_lr_memo_map__index(auxil, map, rule);\n" + "static void pcc_lr_memo_map__put(%s_context_t *ctx, pcc_lr_memo_map_t *map, pcc_rule_t rule, pcc_lr_answer_t *answer) {\n" + " const size_t i = pcc_lr_memo_map__index(ctx->auxil, map, rule);\n" " if (i != PCC_VOID_VALUE) {\n" - " pcc_lr_answer__destroy(auxil, map->buf[i].answer);\n" + " pcc_lr_answer__destroy(ctx, map->buf[i].answer);\n" " map->buf[i].answer = answer;\n" " }\n" " else {\n" @@ -4059,7 +4062,7 @@ static bool_t generate(context_t *ctx) { " if (m == 0) m = PCC_ARRAYSIZE;\n" " while (m < n && m != 0) m <<= 1;\n" " if (m == 0) m = n;\n" - " map->buf = (pcc_lr_memo_t *)PCC_REALLOC(auxil, map->buf, sizeof(pcc_lr_memo_t) * m);\n" + " map->buf = (pcc_lr_memo_t *)PCC_REALLOC(ctx->auxil, map->buf, sizeof(pcc_lr_memo_t) * m);\n" " map->max = m;\n" " }\n" " map->buf[map->len].rule = rule;\n" @@ -4073,16 +4076,17 @@ static bool_t generate(context_t *ctx) { " return (i != PCC_VOID_VALUE) ? map->buf[i].answer : NULL;\n" "}\n" "\n" - "static void pcc_lr_memo_map__term(pcc_auxil_t auxil, pcc_lr_memo_map_t *map) {\n" + "static void pcc_lr_memo_map__term(%s_context_t *ctx, pcc_lr_memo_map_t *map) {\n" " while (map->len > 0) {\n" " map->len--;\n" - " pcc_lr_answer__destroy(auxil, map->buf[map->len].answer);\n" + " pcc_lr_answer__destroy(ctx, map->buf[map->len].answer);\n" " }\n" - " PCC_FREE(auxil, map->buf);\n" + " PCC_FREE(ctx->auxil, map->buf);\n" "}\n" - "\n" + "\n", + get_prefix(ctx), get_prefix(ctx) ); - stream__puts( + stream__printf( &sstream, "static pcc_lr_table_entry_t *pcc_lr_table_entry__create(pcc_auxil_t auxil) {\n" " pcc_lr_table_entry_t *const entry = (pcc_lr_table_entry_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_table_entry_t));\n" @@ -4093,16 +4097,17 @@ static bool_t generate(context_t *ctx) { " return entry;\n" "}\n" "\n" - "static void pcc_lr_table_entry__destroy(pcc_auxil_t auxil, pcc_lr_table_entry_t *entry) {\n" + "static void pcc_lr_table_entry__destroy(%s_context_t *ctx, pcc_lr_table_entry_t *entry) {\n" " if (entry == NULL) return;\n" - " pcc_lr_head__destroy(auxil, entry->hold_h);\n" - " pcc_lr_answer__destroy(auxil, entry->hold_a);\n" - " pcc_lr_memo_map__term(auxil, &entry->memos);\n" - " PCC_FREE(auxil, entry);\n" + " pcc_lr_head__destroy(ctx, entry->hold_h);\n" + " pcc_lr_answer__destroy(ctx, entry->hold_a);\n" + " pcc_lr_memo_map__term(ctx, &entry->memos);\n" + " PCC_FREE(ctx->auxil, entry);\n" "}\n" - "\n" + "\n", + get_prefix(ctx) ); - stream__puts( + stream__printf( &sstream, "static void pcc_lr_table__init(pcc_auxil_t auxil, pcc_lr_table_t *table) {\n" " table->ofs = 0;\n" @@ -4111,47 +4116,47 @@ static bool_t generate(context_t *ctx) { " table->buf = NULL;\n" "}\n" "\n" - "static void pcc_lr_table__resize(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t len) {\n" + "static void pcc_lr_table__resize(%s_context_t *ctx, pcc_lr_table_t *table, size_t len) {\n" " size_t i;\n" - " for (i = len; i < table->len; i++) pcc_lr_table_entry__destroy(auxil, table->buf[i]);\n" + " for (i = len; i < table->len; i++) pcc_lr_table_entry__destroy(ctx, table->buf[i]);\n" " if (table->max < len) {\n" " size_t m = table->max;\n" " if (m == 0) m = PCC_ARRAYSIZE;\n" " while (m < len && m != 0) m <<= 1;\n" " if (m == 0) m = len;\n" - " table->buf = (pcc_lr_table_entry_t **)PCC_REALLOC(auxil, table->buf, sizeof(pcc_lr_table_entry_t *) * m);\n" + " table->buf = (pcc_lr_table_entry_t **)PCC_REALLOC(ctx->auxil, table->buf, sizeof(pcc_lr_table_entry_t *) * m);\n" " table->max = m;\n" " }\n" " for (i = table->len; i < len; i++) table->buf[i] = NULL;\n" " table->len = len;\n" "}\n" "\n" - "static void pcc_lr_table__set_head(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {\n" + "static void pcc_lr_table__set_head(%s_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {\n" " index += table->ofs;\n" - " if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);\n" - " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);\n" + " if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);\n" + " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx->auxil);\n" " table->buf[index]->head = head;\n" "}\n" "\n" - "static void pcc_lr_table__hold_head(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {\n" + "static void pcc_lr_table__hold_head(%s_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {\n" " index += table->ofs;\n" - " if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);\n" - " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);\n" + " if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);\n" + " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx->auxil);\n" " head->hold = table->buf[index]->hold_h;\n" " table->buf[index]->hold_h = head;\n" "}\n" "\n" - "static void pcc_lr_table__set_answer(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_rule_t rule, pcc_lr_answer_t *answer) {\n" + "static void pcc_lr_table__set_answer(%s_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_rule_t rule, pcc_lr_answer_t *answer) {\n" " index += table->ofs;\n" - " if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);\n" - " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);\n" - " pcc_lr_memo_map__put(auxil, &table->buf[index]->memos, rule, answer);\n" + " if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);\n" + " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx->auxil);\n" + " pcc_lr_memo_map__put(ctx, &table->buf[index]->memos, rule, answer);\n" "}\n" "\n" - "static void pcc_lr_table__hold_answer(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_answer_t *answer) {\n" + "static void pcc_lr_table__hold_answer(%s_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_answer_t *answer) {\n" " index += table->ofs;\n" - " if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);\n" - " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);\n" + " if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);\n" + " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx->auxil);\n" " answer->hold = table->buf[index]->hold_a;\n" " table->buf[index]->hold_a = answer;\n" "}\n" @@ -4168,10 +4173,10 @@ static bool_t generate(context_t *ctx) { " return pcc_lr_memo_map__get(auxil, &table->buf[index]->memos, rule);\n" "}\n" "\n" - "static void pcc_lr_table__shift(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t count) {\n" + "static void pcc_lr_table__shift(%s_context_t *ctx, pcc_lr_table_t *table, size_t count) {\n" " size_t i;\n" " if (count > table->len - table->ofs) count = table->len - table->ofs;\n" - " for (i = 0; i < count; i++) pcc_lr_table_entry__destroy(auxil, table->buf[table->ofs++]);\n" + " for (i = 0; i < count; i++) pcc_lr_table_entry__destroy(ctx, table->buf[table->ofs++]);\n" " if (table->ofs > (table->max >> 1)) {\n" " memmove(table->buf, table->buf + table->ofs, sizeof(pcc_lr_table_entry_t *) * (table->len - table->ofs));\n" " table->len -= table->ofs;\n" @@ -4179,14 +4184,15 @@ static bool_t generate(context_t *ctx) { " }\n" "}\n" "\n" - "static void pcc_lr_table__term(pcc_auxil_t auxil, pcc_lr_table_t *table) {\n" + "static void pcc_lr_table__term(%s_context_t *ctx, pcc_lr_table_t *table) {\n" " while (table->len > table->ofs) {\n" " table->len--;\n" - " pcc_lr_table_entry__destroy(auxil, table->buf[table->len]);\n" + " pcc_lr_table_entry__destroy(ctx, table->buf[table->len]);\n" " }\n" - " PCC_FREE(auxil, table->buf);\n" + " PCC_FREE(ctx->auxil, table->buf);\n" "}\n" - "\n" + "\n", + get_prefix(ctx), get_prefix(ctx), get_prefix(ctx), get_prefix(ctx), get_prefix(ctx), get_prefix(ctx), get_prefix(ctx) ); stream__puts( &sstream, @@ -4263,7 +4269,7 @@ static bool_t generate(context_t *ctx) { " if (ctx == NULL) return;\n" " pcc_thunk_array__term(ctx->auxil, &ctx->thunks);\n" " pcc_lr_stack__term(ctx->auxil, &ctx->lrstack);\n" - " pcc_lr_table__term(ctx->auxil, &ctx->lrtable);\n" + " pcc_lr_table__term(ctx, &ctx->lrtable);\n" " pcc_char_array__term(ctx->auxil, &ctx->buffer);\n" " PCC_FREE(ctx->auxil, ctx);\n" "}\n" @@ -4297,7 +4303,7 @@ static bool_t generate(context_t *ctx) { " memmove(ctx->buffer.buf, ctx->buffer.buf + ctx->cur, ctx->buffer.len - ctx->cur);\n" " ctx->buffer.len -= ctx->cur;\n" " ctx->pos += ctx->cur;\n" - " pcc_lr_table__shift(ctx->auxil, &ctx->lrtable, ctx->cur);\n" + " pcc_lr_table__shift(ctx, &ctx->lrtable, ctx->cur);\n" " ctx->cur = 0;\n" "}\n" "\n" @@ -4400,9 +4406,9 @@ static bool_t generate(context_t *ctx) { " else if (pcc_rule_set__remove(ctx->auxil, &h->eval, rule)) {\n" " b = PCC_FALSE;\n" " c = rule(ctx);\n" - " a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur);\n" + " a = pcc_lr_answer__create(ctx, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur);\n" " a->data.chunk = c;\n" - " pcc_lr_table__hold_answer(ctx->auxil, &ctx->lrtable, p, a);\n" + " pcc_lr_table__hold_answer(ctx, &ctx->lrtable, p, a);\n" " }\n" " }\n" " if (b) {\n" @@ -4411,8 +4417,8 @@ static bool_t generate(context_t *ctx) { " switch (a->type) {\n" " case PCC_LR_ANSWER_LR:\n" " if (a->data.lr->head == NULL) {\n" - " a->data.lr->head = pcc_lr_head__create(ctx->auxil, rule);\n" - " pcc_lr_table__hold_head(ctx->auxil, &ctx->lrtable, p, a->data.lr->head);\n" + " a->data.lr->head = pcc_lr_head__create(ctx, rule);\n" + " pcc_lr_table__hold_head(ctx, &ctx->lrtable, p, a->data.lr->head);\n" " }\n" " {\n" " size_t i = ctx->lrstack.len;\n" @@ -4435,41 +4441,41 @@ static bool_t generate(context_t *ctx) { " else {\n" " pcc_lr_entry_t *const e = pcc_lr_entry__create(ctx->auxil, rule);\n" " pcc_lr_stack__push(ctx->auxil, &ctx->lrstack, e);\n" - " a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_LR, p);\n" + " a = pcc_lr_answer__create(ctx, PCC_LR_ANSWER_LR, p);\n" " a->data.lr = e;\n" - " pcc_lr_table__set_answer(ctx->auxil, &ctx->lrtable, p, rule, a);\n" + " pcc_lr_table__set_answer(ctx, &ctx->lrtable, p, rule, a);\n" " c = rule(ctx);\n" " pcc_lr_stack__pop(ctx->auxil, &ctx->lrstack);\n" " a->pos = ctx->pos + ctx->cur;\n" " if (e->head == NULL) {\n" - " pcc_lr_answer__set_chunk(ctx->auxil, a, c);\n" + " pcc_lr_answer__set_chunk(ctx, a, c);\n" " }\n" " else {\n" " e->seed = c;\n" " h = a->data.lr->head;\n" " if (h->rule != rule) {\n" " c = a->data.lr->seed;\n" - " a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur);\n" + " a = pcc_lr_answer__create(ctx, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur);\n" " a->data.chunk = c;\n" - " pcc_lr_table__hold_answer(ctx->auxil, &ctx->lrtable, p, a);\n" + " pcc_lr_table__hold_answer(ctx, &ctx->lrtable, p, a);\n" " }\n" " else {\n" - " pcc_lr_answer__set_chunk(ctx->auxil, a, a->data.lr->seed);\n" + " pcc_lr_answer__set_chunk(ctx, a, a->data.lr->seed);\n" " if (a->data.chunk == NULL) {\n" " c = NULL;\n" " }\n" " else {\n" - " pcc_lr_table__set_head(ctx->auxil, &ctx->lrtable, p, h);\n" + " pcc_lr_table__set_head(ctx, &ctx->lrtable, p, h);\n" " for (;;) {\n" " ctx->cur = p - ctx->pos;\n" " pcc_rule_set__copy(ctx->auxil, &h->eval, &h->invol);\n" " c = rule(ctx);\n" " if (c == NULL || ctx->pos + ctx->cur <= a->pos) break;\n" - " pcc_lr_answer__set_chunk(ctx->auxil, a, c);\n" + " pcc_lr_answer__set_chunk(ctx, a, c);\n" " a->pos = ctx->pos + ctx->cur;\n" " }\n" - " pcc_thunk_chunk__destroy(ctx->auxil, c);\n" - " pcc_lr_table__set_head(ctx->auxil, &ctx->lrtable, p, NULL);\n" + " pcc_thunk_chunk__destroy(ctx, c);\n" + " pcc_lr_table__set_head(ctx, &ctx->lrtable, p, NULL);\n" " ctx->cur = a->pos - ctx->pos;\n" " c = a->data.chunk;\n" " }\n" @@ -4658,7 +4664,7 @@ static bool_t generate(context_t *ctx) { ); stream__printf( &sstream, - " pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);\n" + " pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx);\n" " chunk->pos = ctx->cur;\n" " PCC_DEBUG(ctx->auxil, PCC_DBG_EVALUATE, \"%s\", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));\n" " ctx->level++;\n", @@ -4694,7 +4700,7 @@ static bool_t generate(context_t *ctx) { "L0000:;\n" " ctx->level--;\n" " PCC_DEBUG(ctx->auxil, PCC_DBG_NOMATCH, \"%s\", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));\n" - " pcc_thunk_chunk__destroy(ctx->auxil, chunk);\n" + " pcc_thunk_chunk__destroy(ctx, chunk);\n" " return NULL;\n", ctx->rules.buf[i]->data.rule.name ); From f3a5c7e772da28b3e0fd88e9cb7bc3eb3487c504 Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Mon, 25 Apr 2022 21:08:08 +0900 Subject: [PATCH 2/2] Preallocate memory objects for pcc_thunk_chunk_t, pcc_lr_head_t, and pcc_lr_answer_t A generated parser allocates memory objects for the types frequently. The objects are not resized so we can preallocate them. bencharmk.sh reports the code with this change is about 10~ faster than the original code in run times. benchmark.sh output for the original code: Generation times: ================= master calc 513 us (100%) json 511 us (100%) kotlin 2320 us (100%) Build times: ============ master calc 69 ms (100%) json 64 ms (100%) kotlin 731 ms (100%) Run times: ========== master calc 126 ms (100%) json 34 ms (100%) kotlin 279 ms (100%) benchmark.sh output for the code with this change: Generation times: ================= recycle-list calc 538 us (100%) json 512 us (100%) kotlin 2225 us (100%) Build times: ============ recycle-list calc 72 ms (100%) json 68 ms (100%) kotlin 732 ms (100%) Run times: ========== recycle-list calc 110 ms (100%) json 28 ms (100%) kotlin 222 ms (100%) Signed-off-by: Masatake YAMATO --- src/packcc.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 6 deletions(-) diff --git a/src/packcc.c b/src/packcc.c index 4ee05a2..439b505 100644 --- a/src/packcc.c +++ b/src/packcc.c @@ -3575,6 +3575,20 @@ static bool_t generate(context_t *ctx) { "} pcc_lr_stack_t;\n" "\n" ); + stream__puts( + &sstream, + "typedef struct pcc_recycle_list_tag {\n" + " struct pcc_recycle_list_tag *next;\n" + "} pcc_recycle_list_t;\n" + "typedef struct pcc_recycle_manager_tag {\n" + " pcc_recycle_list_t *list;\n" + " size_t element_size;\n" + " size_t allocated;\n" + " size_t inuse;\n" + " void *base;\n" + "} pcc_recycle_manager_t;\n" + "\n" + ); stream__printf( &sstream, "struct %s_context_tag {\n" @@ -3586,6 +3600,9 @@ static bool_t generate(context_t *ctx) { " pcc_lr_stack_t lrstack;\n" " pcc_thunk_array_t thunks;\n" " pcc_auxil_t auxil;\n" + " pcc_recycle_manager_t thunk_chunk_recycle_manager;\n" + " pcc_recycle_manager_t lr_head_recycle_manager;\n" + " pcc_recycle_manager_t lr_answer_recycle_manager;\n" "};\n" "\n", get_prefix(ctx) @@ -3873,11 +3890,54 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); + + stream__puts( + &sstream, + "static void *pcc_recycle_alloc(pcc_auxil_t auxil, pcc_recycle_manager_t *recycle_manager) {\n" + " if (recycle_manager->list) {\n" + " pcc_recycle_list_t *tmp = recycle_manager->list;\n" + " recycle_manager->list = tmp->next;\n" + " return tmp;\n" + " }\n" + " if (recycle_manager->inuse < recycle_manager->allocated) {\n" + " char *base = recycle_manager->base;\n" + " char *tmp = base + (recycle_manager->inuse * recycle_manager->element_size);\n" + " recycle_manager->inuse++;\n" + " return tmp;\n" + " }\n" + " return PCC_MALLOC(auxil, recycle_manager->element_size);\n" + "}\n" + "static void pcc_recycle_return(pcc_recycle_manager_t *recycle_manager, void *obj) {\n" + " pcc_recycle_list_t *tmp = obj;\n" + " tmp->next = recycle_manager->list;\n" + " recycle_manager->list = tmp;\n" + "}\n" + "static void pcc_recycle_manager_alloc(pcc_auxil_t auxil, pcc_recycle_manager_t *manager, size_t element_size) {\n" + " manager->list = NULL;\n" + " manager->element_size = element_size;\n" + " manager->allocated = 1024 * 1024;\n" + " manager->inuse = 0;\n" + " manager->base = PCC_MALLOC(auxil, manager->element_size * manager->allocated);\n" + "}\n" + "static void pcc_recycle_manager_destroy(pcc_auxil_t auxil, pcc_recycle_manager_t *recycle_manager) {\n" + " char *start = recycle_manager->base;\n" + " char *end = recycle_manager->base + (recycle_manager->allocated * recycle_manager->element_size);\n" + " while (recycle_manager->list) {\n" + " pcc_recycle_list_t *tmp = recycle_manager->list;\n" + " recycle_manager->list = tmp->next;\n" + " if (start <= (char *)tmp && (char *)tmp < end)\n" + " continue;\n" + " PCC_FREE(auxil, tmp);\n" + " }\n" + " PCC_FREE(auxil, recycle_manager->base);\n" + "}\n" + "\n" + ); stream__printf( &sstream, "MARK_USED_FUNC\n" "static pcc_thunk_chunk_t *pcc_thunk_chunk__create(%s_context_t *ctx) {\n" - " pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_thunk_chunk_t));\n" + " pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)pcc_recycle_alloc(ctx->auxil, &ctx->thunk_chunk_recycle_manager);\n" " pcc_value_table__init(ctx->auxil, &chunk->values);\n" " pcc_capture_table__init(ctx->auxil, &chunk->capts);\n" " pcc_thunk_array__init(ctx->auxil, &chunk->thunks);\n" @@ -3890,7 +3950,7 @@ static bool_t generate(context_t *ctx) { " pcc_thunk_array__term(ctx->auxil, &chunk->thunks);\n" " pcc_capture_table__term(ctx->auxil, &chunk->capts);\n" " pcc_value_table__term(ctx->auxil, &chunk->values);\n" - " PCC_FREE(ctx->auxil, chunk);\n" + " pcc_recycle_return(&ctx->thunk_chunk_recycle_manager, chunk);\n" "}\n" "\n", get_prefix(ctx), get_prefix(ctx) @@ -3954,7 +4014,7 @@ static bool_t generate(context_t *ctx) { stream__printf( &sstream, "static pcc_lr_head_t *pcc_lr_head__create(%s_context_t *ctx, pcc_rule_t rule) {\n" - " pcc_lr_head_t *const head = (pcc_lr_head_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_lr_head_t));\n" + " pcc_lr_head_t *const head = (pcc_lr_head_t *)pcc_recycle_alloc(ctx->auxil, &ctx->lr_head_recycle_manager);\n" " head->rule = rule;\n" " pcc_rule_set__init(ctx->auxil, &head->invol);\n" " pcc_rule_set__init(ctx->auxil, &head->eval);\n" @@ -3967,7 +4027,7 @@ static bool_t generate(context_t *ctx) { " pcc_lr_head__destroy(ctx, head->hold);\n" " pcc_rule_set__term(ctx->auxil, &head->eval);\n" " pcc_rule_set__term(ctx->auxil, &head->invol);\n" - " PCC_FREE(ctx->auxil, head);\n" + " pcc_recycle_return(&ctx->lr_head_recycle_manager, head);\n" "}\n" "\n", get_prefix(ctx), get_prefix(ctx) @@ -3977,7 +4037,7 @@ static bool_t generate(context_t *ctx) { "static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr);\n" "\n" "static pcc_lr_answer_t *pcc_lr_answer__create(%s_context_t *ctx, pcc_lr_answer_type_t type, size_t pos) {\n" - " pcc_lr_answer_t *answer = (pcc_lr_answer_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_lr_answer_t));\n" + " pcc_lr_answer_t *answer = (pcc_lr_answer_t *)pcc_recycle_alloc(ctx->auxil, &ctx->lr_answer_recycle_manager);\n" " answer->type = type;\n" " answer->pos = pos;\n" " answer->hold = NULL;\n" @@ -4026,7 +4086,7 @@ static bool_t generate(context_t *ctx) { " default: /* unknown */\n" " break;\n" " }\n" - " PCC_FREE(ctx->auxil, answer);\n" + " pcc_recycle_return(&ctx->lr_answer_recycle_manager, answer);\n" " answer = a;\n" " }\n" "}\n" @@ -4254,6 +4314,9 @@ static bool_t generate(context_t *ctx) { " pcc_lr_table__init(auxil, &ctx->lrtable);\n" " pcc_lr_stack__init(auxil, &ctx->lrstack);\n" " pcc_thunk_array__init(auxil, &ctx->thunks);\n" + " pcc_recycle_manager_alloc(auxil, &ctx->thunk_chunk_recycle_manager, sizeof(pcc_thunk_chunk_t));\n" + " pcc_recycle_manager_alloc(auxil, &ctx->lr_head_recycle_manager, sizeof(pcc_lr_head_t));\n" + " pcc_recycle_manager_alloc(auxil, &ctx->lr_answer_recycle_manager, sizeof(pcc_lr_answer_t));\n" " ctx->auxil = auxil;\n" " return ctx;\n" "}\n" @@ -4266,11 +4329,15 @@ static bool_t generate(context_t *ctx) { ); stream__puts( &sstream, + " pcc_recycle_list_t *tmp;\n" " if (ctx == NULL) return;\n" " pcc_thunk_array__term(ctx->auxil, &ctx->thunks);\n" " pcc_lr_stack__term(ctx->auxil, &ctx->lrstack);\n" " pcc_lr_table__term(ctx, &ctx->lrtable);\n" " pcc_char_array__term(ctx->auxil, &ctx->buffer);\n" + " pcc_recycle_manager_destroy(ctx->auxil, &ctx->thunk_chunk_recycle_manager);\n" + " pcc_recycle_manager_destroy(ctx->auxil, &ctx->lr_head_recycle_manager);\n" + " pcc_recycle_manager_destroy(ctx->auxil, &ctx->lr_answer_recycle_manager);\n" " PCC_FREE(ctx->auxil, ctx);\n" "}\n" "\n"