TiborB has proposed merging lp:~widelands-dev/widelands/ai_variables_rework
into lp:widelands.
Requested reviews:
Widelands Developers (widelands-dev)
For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/ai_variables_rework/+merge/333041
This change is only about memory allocation for AI functions. The goal is to
reduce intensity how AI allocated and returns memory for its data. I dont have
a proof but on PCs with low memory the game can have problems with allocation,
resulting to crashes. After this changes I BELIEVE the number of crashes is
reduced. Also I believe it has tiny, but measurable positive effect on game
speed.
So what it contains:
1. changing std::lists to std::deque
2. making some vectors static + using reserve()
3. making some of integers static
I believe no harm should be done with this change
--
Your team Widelands Developers is requested to review the proposed merge of
lp:~widelands-dev/widelands/ai_variables_rework into lp:widelands.
=== modified file 'src/ai/ai_help_structs.h'
--- src/ai/ai_help_structs.h 2017-09-29 16:10:25 +
+++ src/ai/ai_help_structs.h 2017-10-31 17:41:52 +
@@ -388,7 +388,7 @@
explicit EconomyObserver(Widelands::Economy& e);
Widelands::Economy& economy;
- std::list flags;
+ std::deque flags;
int32_t dismantle_grace_time;
};
=== modified file 'src/ai/defaultai.cc'
--- src/ai/defaultai.cc 2017-09-29 16:10:25 +
+++ src/ai/defaultai.cc 2017-10-31 17:41:52 +
@@ -162,43 +162,43 @@
});
// Subscribe to ShipNotes.
- shipnotes_subscriber_ =
- Notifications::subscribe([this](const NoteShipMessage& note) {
-
- // in a short time between start and late_initialization the player
- // can get notes that can not be processed.
- // It seems that this causes no problem, at least no substantial
- if (player_ == nullptr) {
- return;
- }
- if (note.ship->get_owner()->player_number() != player_->player_number()) {
- return;
- }
-
- switch (note.message) {
-
- case NoteShipMessage::Message::kGained:
- gain_ship(*note.ship, NewShip::kBuilt);
- break;
-
- case NoteShipMessage::Message::kLost:
- for (std::list::iterator i = allships.begin(); i != allships.end(); ++i) {
- if (i->ship == note.ship) {
- allships.erase(i);
- break;
- }
- }
- break;
-
- case NoteShipMessage::Message::kWaitingForCommand:
- for (std::list::iterator i = allships.begin(); i != allships.end(); ++i) {
- if (i->ship == note.ship) {
- i->waiting_for_command_ = true;
- break;
- }
- }
- }
- });
+ shipnotes_subscriber_ = Notifications::subscribe([this](
+ const NoteShipMessage& note) {
+
+ // in a short time between start and late_initialization the player
+ // can get notes that can not be processed.
+ // It seems that this causes no problem, at least no substantial
+ if (player_ == nullptr) {
+ return;
+ }
+ if (note.ship->get_owner()->player_number() != player_->player_number()) {
+ return;
+ }
+
+ switch (note.message) {
+
+ case NoteShipMessage::Message::kGained:
+ gain_ship(*note.ship, NewShip::kBuilt);
+ break;
+
+ case NoteShipMessage::Message::kLost:
+ for (std::deque::iterator i = allships.begin(); i != allships.end(); ++i) {
+if (i->ship == note.ship) {
+ allships.erase(i);
+ break;
+}
+ }
+ break;
+
+ case NoteShipMessage::Message::kWaitingForCommand:
+ for (std::deque::iterator i = allships.begin(); i != allships.end(); ++i) {
+if (i->ship == note.ship) {
+ i->waiting_for_command_ = true;
+ break;
+}
+ }
+ }
+ });
}
DefaultAI::~DefaultAI() {
@@ -1254,7 +1254,8 @@
}
// are we going to count resources now?
- bool resource_count_now = false;
+ static bool resource_count_now = false;
+ resource_count_now = false;
// Testing in first 10 seconds or if last testing was more then 60 sec ago
if (field.last_resources_check_time < 1 ||
field.last_resources_check_time - gametime > 60 * 1000) {
@@ -1445,12 +1446,15 @@
field.unconnected_nearby = false;
// collect information about productionsites nearby
- std::vector immovables;
+ static std::vector immovables;
+ immovables.reserve(50);
+ immovables.clear();
// Search in a radius of range
map.find_immovables(Area(field.coords, kProductionArea + 2), );
// function seems to return duplicates, so we will use serial numbers to filter them out
- std::set unique_serials;
+ static std::set unique_serials;
+ unique_serials.clear();
for (uint32_t i = 0; i < immovables.size(); ++i) {
const BaseImmovable& base_immovable = *immovables.at(i).object;
@@ -1483,8 +1487,10 @@
map.find_immovables(Area(field.coords, actual_enemy_check_area), );
// We are interested in unconnected immovables, but we must be also close to connected ones
- bool any_connected_imm = false;
- bool any_unconnected_imm = false;
+ static bool any_connected_imm = false;
+ any_connected_imm