Skip to content
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

Begone realloc #966

Merged
merged 4 commits into from
Mar 26, 2022
Merged

Conversation

psampathkumar
Copy link
Contributor

Changed a few easy reallocs to vectors

Copy link
Contributor

@lmoureaux lmoureaux left a comment

Choose a reason for hiding this comment

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

Haven't run it yet. Some city_report_specs code could be simplified further by declaring variables inside loops and using auto, but the reduction in manual memory management is already a significant improvement.

@@ -33,8 +33,7 @@ void ai_traits_init(struct player *pplayer)
{
enum trait tr;

pplayer->ai_common.traits = static_cast<ai_trait *>(fc_realloc(
pplayer->ai_common.traits, sizeof(struct ai_trait) * TRAIT_COUNT));
pplayer->ai_common.traits = std::vector<ai_trait>(TRAIT_COUNT);
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem to be ever resized. TRAIT_COUNT is 3 and ai_trait is just two ints. Would this make more sense as a std::array<ai_trait, TRAIT_COUNT>?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was considering the same, also we have waay too much ram these days to have a lot more traits still in stack, Ended up doing the same in heap because I thought the difference would be negligible, and array didnt have clear.

@@ -219,24 +219,23 @@ bool city_model::setData(const QModelIndex &index, const QVariant &value,
QVariant city_model::headerData(int section, Qt::Orientation orientation,
int role) const
{
struct city_report_spec *spec = city_report_specs + section;
struct city_report_spec spec = city_report_specs[section];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: const auto &

@psampathkumar
Copy link
Contributor Author

I did run for 200 turns with ASAN and checked the cityreport with the client to see. Seemed to work for me.

We are only reading from spec and not modifying it.
@lmoureaux lmoureaux merged commit 7edd3e3 into longturn:master Mar 26, 2022
@psampathkumar psampathkumar deleted the bugfix/begone-realloc branch April 25, 2022 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants