Use sorted forward_list instead of multimap for ID option change handlers

This commit is contained in:
Tim Wojtulewicz 2025-07-23 21:58:26 -07:00
parent 020dd1a848
commit 035b4a4a8e
2 changed files with 10 additions and 7 deletions

View file

@ -576,14 +576,16 @@ void ID::UpdateValID() {
}
#endif
void ID::AddOptionHandler(FuncPtr callback, int priority) { option_handlers.emplace(priority, std::move(callback)); }
void ID::AddOptionHandler(FuncPtr callback, int priority) {
option_handlers.emplace_front(priority, callback);
option_handlers.sort(
[](const OptionHandler& left, const OptionHandler& right) { return left.first < right.first; });
}
std::vector<Func*> ID::GetOptionHandlers() const {
// multimap is sorted
// It might be worth caching this if we expect it to be called
// a lot...
// It might be worth caching this if we expect it to be called a lot...
std::vector<Func*> v;
for ( auto& element : option_handlers )
for ( const auto& element : option_handlers )
v.push_back(element.second.get());
return v;
}

View file

@ -2,7 +2,7 @@
#pragma once
#include <map>
#include <forward_list>
#include <string>
#include <string_view>
#include <unordered_set>
@ -172,7 +172,8 @@ protected:
AttributesPtr attrs;
// contains list of functions that are called when an option changes
std::multimap<int, FuncPtr> option_handlers;
using OptionHandler = std::pair<int, FuncPtr>;
std::forward_list<OptionHandler> option_handlers;
// Information managed by script optimization. We package this
// up into a separate object for purposes of modularity, and,