Skip to content

Fix O(N^2) performance degradation in form_from_map - #88527

Open
IcarusOfTroy wants to merge 4 commits into
CleverRaven:masterfrom
IcarusOfTroy:fix-inventory-bulk-performance
Open

Fix O(N^2) performance degradation in form_from_map#88527
IcarusOfTroy wants to merge 4 commits into
CleverRaven:masterfrom
IcarusOfTroy:fix-inventory-bulk-performance

Conversation

@IcarusOfTroy

@IcarusOfTroy IcarusOfTroy commented Sep 4, 2026

Copy link
Copy Markdown

Summary

Performance "Fix O(N^2) performance degradation in form_from_map"

Purpose of change

Fixes #88525.
The game would hang or severely degrade in performance when there were a lot of items around, particularly when opening menus that list items such as crafting or vehicle inspection.

Describe the solution

Hoists the bulk item batching out of the per-tile loop in inventory::form_from_map. Previously, add_items_bulk was called once for every tile containing more than one item, causing severe slowdowns when surrounded by many items, as it rebuilds an index of the entire accumulated inventory each time. Now, it gathers all items across all tiles into a single batch and calls add_items_bulk exactly once at the end of the function.

Describe alternatives you've considered

N/A

Testing

Compiled and checked that form_from_map correctly gathers all items into the single bulk batch, restoring expected performance and avoiding the O(N^2) stack sweeps.

Added a new unit test tests/inventory_test.cpp containing inventory_form_from_map_bulk_batching. This programmatic test proves that form_from_map properly pools items from multiple coordinates into a single bulk batch with correct totals, guarding against future regressions in the inventory loading pipeline.

Additional context

None

@github-actions github-actions Bot added <Bugfix> This is a fix for a bug (or closes open issue) [C++] Changes (can be) made in C++. Previously named `Code` labels Sep 4, 2026
@github-actions github-actions Bot added Code: Performance Performance boosting code (CPU, memory, etc.) Code: Tests Measurement, self-control, statistics, balancing. labels Sep 4, 2026
@IcarusOfTroy
IcarusOfTroy marked this pull request as ready for review September 4, 2026 18:46
@github-actions github-actions Bot added astyled astyled PR, label is assigned by github actions json-styled JSON lint passed, label assigned by github actions labels Sep 4, 2026
Comment thread tests/inventory_test.cpp
Comment thread tests/inventory_test.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

astyled astyled PR, label is assigned by github actions <Bugfix> This is a fix for a bug (or closes open issue) [C++] Changes (can be) made in C++. Previously named `Code` Code: Performance Performance boosting code (CPU, memory, etc.) Code: Tests Measurement, self-control, statistics, balancing. json-styled JSON lint passed, label assigned by github actions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance Degradation across the board when there are a lot of items around

1 participant