From 795e8cb460462571b2246c9420f7efd6ba923d19 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Fri, 23 Jun 2023 18:52:09 +0300 Subject: [PATCH 11/11] create_dummy_reqtree(): Avoid redundant advance_count() calls See osdn #48274 Signed-off-by: Marko Lindqvist --- client/reqtree.c | 29 +++++++++++++++-------------- common/tech.h | 24 ++++++++++++++++-------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/client/reqtree.c b/client/reqtree.c index 2b4b4386cc..fc76dfbcdd 100644 --- a/client/reqtree.c +++ b/client/reqtree.c @@ -381,8 +381,8 @@ static void calculate_diagram_layout(struct reqtree *tree) } /*********************************************************************//** - Create a "dummy" tech tree from current ruleset. This tree is then - fleshed out further (see create_reqtree). This tree doesn't include + Create a "dummy" tech tree from current ruleset. This tree is then + fleshed out further (see create_reqtree() ). This tree doesn't include dummy edges. Layering and ordering isn't done also. If pplayer is given, add only techs reachable by that player to tree. @@ -393,10 +393,11 @@ static struct reqtree *create_dummy_reqtree(struct player *pplayer, const struct research *presearch = research_get(pplayer); struct reqtree *tree = fc_malloc(sizeof(*tree)); int j; - struct tree_node *nodes[advance_count()]; + Tech_type_id ac = advance_count(); + struct tree_node *nodes[ac]; nodes[A_NONE] = NULL; - advance_index_iterate(A_FIRST, tech) { + advance_index_iterate_max(A_FIRST, tech, ac) { if (!valid_advance_by_number(tech)) { nodes[tech] = NULL; continue; @@ -411,9 +412,9 @@ static struct reqtree *create_dummy_reqtree(struct player *pplayer, nodes[tech] = new_tree_node(); nodes[tech]->is_dummy = FALSE; nodes[tech]->tech = tech; - } advance_index_iterate_end; + } advance_index_iterate_max_end; - advance_index_iterate(A_FIRST, tech) { + advance_index_iterate_max(A_FIRST, tech, ac) { struct advance *padvance = valid_advance_by_number(tech); Tech_type_id tech_one, tech_two; @@ -434,9 +435,9 @@ static struct reqtree *create_dummy_reqtree(struct player *pplayer, continue; } - /* Formerly, we used to remove the redundant requirement nodes (the - * technologies already included in the requirements of the other - * requirement). However, it doesn't look like a good idea, because + /* Formerly, we used to remove the redundant requirement nodes + * (the technologies already included in the requirements of the other + * requirement). However, it doesn't look like a good idea, because * a player can steal any technology independently of the technology * tree. */ if (A_NONE != tech_one && A_LAST != tech_two) { @@ -445,18 +446,18 @@ static struct reqtree *create_dummy_reqtree(struct player *pplayer, add_requirement(nodes[tech], nodes[tech_two]); } } - } advance_index_iterate_end; + } advance_index_iterate_max_end; - /* Copy nodes from local array to dynamically allocated one. + /* Copy nodes from local array to dynamically allocated one. * Skip non-existing entries */ - tree->nodes = fc_calloc(advance_count(), sizeof(*tree->nodes)); + tree->nodes = fc_calloc(ac, sizeof(*tree->nodes)); j = 0; - advance_index_iterate(A_FIRST, tech) { + advance_index_iterate_max(A_FIRST, tech, ac) { if (nodes[tech]) { fc_assert_action(valid_advance_by_number(nodes[tech]->tech), continue); tree->nodes[j++] = nodes[tech]; } - } advance_index_iterate_end; + } advance_index_iterate_max_end; tree->num_nodes = j; tree->layers = NULL; diff --git a/common/tech.h b/common/tech.h index 57c5bde60f..31e8ecdf49 100644 --- a/common/tech.h +++ b/common/tech.h @@ -238,16 +238,24 @@ void techs_precalc_data(void); /* Iteration */ -/* This iterates over almost all technologies. It includes non-existent +/* This iterates over almost all technologies. It includes non-existent * technologies, but not A_FUTURE. */ -#define advance_index_iterate(_start, _index) \ -{ \ - Tech_type_id _index = (_start); \ - Tech_type_id _aco_##_index = advance_count(); \ +#define advance_index_iterate(_start, _index) \ +{ \ + advance_index_iterate_max(_start, _index, advance_count()) + +#define advance_index_iterate_end \ + advance_index_iterate_max_end \ +} + +#define advance_index_iterate_max(_start, _index, _max) \ +{ \ + Tech_type_id _index = (_start); \ + Tech_type_id _aco_##_index = (_max); \ for (; _index < _aco_##_index; _index++) { -#define advance_index_iterate_end \ - } \ +#define advance_index_iterate_max_end \ + } \ } const struct advance *advance_array_last(void); @@ -307,4 +315,4 @@ struct iterator *advance_root_req_iter_init(struct advance_root_req_iter *it, } #endif /* __cplusplus */ -#endif /* FC__TECH_H */ +#endif /* FC__TECH_H */ -- 2.40.1