Skip to content

[from dev] Minimise allocations for ModifierSum#755

Open
wvpm wants to merge 2 commits into
masterfrom
dev_minimise_allocations_modifiersum
Open

[from dev] Minimise allocations for ModifierSum#755
wvpm wants to merge 2 commits into
masterfrom
dev_minimise_allocations_modifiersum

Conversation

@wvpm
Copy link
Copy Markdown
Contributor

@wvpm wvpm commented May 23, 2026

Original PR: #739

CountryInstance::contribute_province_modifier_sum is called for each province (expect 10s-100s per country). Every call to ModifierSum::add_modifier_sum called reserve_more, which is defined as:

    constexpr void reserve_more(reservable auto& t, size_t size) {
        t.reserve(t.size() + size);
    }

This resulted in repeated allocations, possibly reallocating the country's modifiersum for each controlled province.
Instead we now first sum the extra size for all provinces and then resize (not reserve!) once.
We use resize instead of reserve as it uses the underlying growth algorithm that typically reserves extra room, avoiding reallocating because of 1 extra element.

Resize creates empty elements which are later replaced. The ModifierSum keeps track of the count of valid elements and uses this for its size().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant