Skip to content

Minimise allocations for ModifierSum#739

Open
wvpm wants to merge 1 commit into
masterfrom
minimise_allocations_modifiersum
Open

Minimise allocations for ModifierSum#739
wvpm wants to merge 1 commit into
masterfrom
minimise_allocations_modifiersum

Conversation

@wvpm
Copy link
Copy Markdown
Contributor

@wvpm wvpm commented May 17, 2026

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().

@wvpm wvpm added the enhancement New feature or request label May 17, 2026
@wvpm wvpm requested a review from a team as a code owner May 17, 2026 13:12
@wvpm wvpm enabled auto-merge May 17, 2026 13:14
@wvpm wvpm force-pushed the minimise_allocations_modifiersum branch from 2351277 to 99490eb Compare May 17, 2026 13:25
@schombert
Copy link
Copy Markdown

As a general note, calling reserve explicitly is typically only an optimization if you can find a one-time upper bound on the possible growth and thereby call reserve once instead of allowing the vector to grow naturally. However, if you call reserve repeatedly you run the risk of performing much worse than the vector's normal exponential growth. The growth factor of the vector is designed so that adding elements is amortized O(1). Repeated calls to reserve that grow the vector only by some amount that we can place an upper bound on (as presumably happens here, since the total number of possible modifiers is bounded) makes the entire process (cycles of reserve + element additions) amortized O(n). In your particular case, a little back of the napkin math will show that just allocating space for every country to have every possible modifier times the number of land provinces is within the memory budget, so you can use that as the value for a single reservation and be very, very unlikely to ever need to grow the vectors.

Comment thread src/openvic-simulation/modifier/ModifierSum.hpp Outdated
@wvpm wvpm force-pushed the minimise_allocations_modifiersum branch 6 times, most recently from 2eee5e5 to 0540214 Compare May 18, 2026 13:37
Comment thread src/openvic-simulation/utility/BulkInsertWrapper.hpp Outdated
@wvpm wvpm force-pushed the minimise_allocations_modifiersum branch 2 times, most recently from 8215f0e to 6c1f4ad Compare May 18, 2026 13:43
@wvpm wvpm disabled auto-merge May 18, 2026 13:44
@wvpm wvpm force-pushed the minimise_allocations_modifiersum branch from 01af11a to 3435c92 Compare May 18, 2026 14:00
Comment thread src/openvic-simulation/core/BulkInsertWrapper.hpp Outdated
@wvpm wvpm force-pushed the minimise_allocations_modifiersum branch from 3435c92 to c9ba1d1 Compare May 18, 2026 14:37
@wvpm
Copy link
Copy Markdown
Contributor Author

wvpm commented May 18, 2026

I'll add unit tests and then we can merge.

@wvpm wvpm force-pushed the minimise_allocations_modifiersum branch 6 times, most recently from 55ec301 to ed98ce9 Compare May 19, 2026 10:57
@wvpm wvpm force-pushed the minimise_allocations_modifiersum branch 2 times, most recently from 1f173d0 to 44c0e3b Compare May 19, 2026 11:40
@wvpm wvpm force-pushed the minimise_allocations_modifiersum branch from 44c0e3b to 18d03f0 Compare May 19, 2026 13:39
@wvpm wvpm enabled auto-merge May 19, 2026 16:32
Comment thread src/openvic-simulation/core/BulkInsertWrapper.hpp Outdated
Comment thread src/openvic-simulation/core/BulkInsertWrapper.hpp Outdated
@wvpm wvpm force-pushed the minimise_allocations_modifiersum branch 4 times, most recently from 153f1c7 to 71daf85 Compare May 20, 2026 17:01
@wvpm wvpm force-pushed the minimise_allocations_modifiersum branch from 71daf85 to 2b3344b Compare May 20, 2026 17:22
@wvpm wvpm added performance and removed enhancement New feature or request labels May 20, 2026
@wvpm wvpm force-pushed the minimise_allocations_modifiersum branch from 2b3344b to dc042f6 Compare May 21, 2026 08:15
@wvpm wvpm requested a review from Spartan322 May 21, 2026 17:50
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.

3 participants