Performance improvement for collecting scripts, by reserving size in ScriptSource.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-25 11:33:25 +02:00
parent 3b1c0e84e1
commit 734d056449
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
14 changed files with 26 additions and 0 deletions

View File

@ -85,6 +85,7 @@ bool Battle::CreatureInField(const Creature* creature) const {
void Battle::ForceRecall(uint8_t side, uint8_t index) { _sides[side]->SetCreature(nullptr, index); }
size_t Battle::ScriptCount() const { return 1; }
void Battle::GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) {
scripts.Append(ScriptWrapper::FromSet(&_volatile));
}

View File

@ -81,6 +81,7 @@ namespace CreatureLib::Battling {
void SwitchCreature(uint8_t side, uint8_t index, Creature* c);
bool CanSlotBeFilled(uint8_t side, uint8_t index) const;
size_t ScriptCount() const override;
void GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) override;
void ValidateBattleState();

View File

@ -73,6 +73,8 @@ void BattleSide::GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& sc
scripts.Append(ScriptWrapper::FromSet(&_volatile));
_battle->GetActiveScripts(scripts);
}
size_t BattleSide::ScriptCount() const { return _battle->ScriptCount() + 1; }
uint8_t BattleSide::GetRandomCreatureIndex() {
// TODO: Consider adding parameter to only get index for available creatures.
AssertNotNull(_battle)

View File

@ -48,6 +48,7 @@ namespace CreatureLib::Battling {
Creature* GetCreature(uint8_t index) const;
bool CreatureOnSide(const Creature* creature) const;
size_t ScriptCount() const override;
void GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) override;
const List<Creature*>& GetCreatures() { return _creatures; }

View File

@ -166,6 +166,14 @@ bool Battling::Creature::HasType(uint8_t type) const noexcept {
return std::find(t.begin(), t.end(), type) != t.end();
}
size_t Battling::Creature::ScriptCount() const {
auto c = 3;
if (_side != nullptr) {
c += _side->ScriptCount();
}
return c;
}
void Battling::Creature::GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) {
scripts.Append(ScriptWrapper::FromScript(&_activeTalent));
scripts.Append(ScriptWrapper::FromScript(&_status));

View File

@ -122,6 +122,7 @@ namespace CreatureLib::Battling {
void MarkOpponentAsSeen(Creature* creature) { _seenOpponents.insert(creature); }
const std::unordered_set<Creature*>& GetSeenOpponents() const { return _seenOpponents; }
size_t ScriptCount() const override;
void GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) override;
void ClearVolatileScripts();
void AddVolatileScript(const ConstString& name);

View File

@ -80,6 +80,7 @@ namespace CreatureLib::Battling {
Creature* GetUser() noexcept { return _user; }
LearnedAttack* GetAttack() noexcept { return _attack; }
size_t ScriptCount() const override { return _user->ScriptCount() + 1; }
protected:
void GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) override {

View File

@ -18,6 +18,9 @@ namespace CreatureLib::Battling {
}
}
static constexpr size_t defaultCapacity = 8;
ScriptSet() : _scripts(defaultCapacity), _lookup(defaultCapacity){};
void Add(Script* script) {
size_t v;
if (_lookup.TryGet(script->GetName(), v)) {

View File

@ -16,8 +16,10 @@ namespace CreatureLib::Battling {
void ResetActiveScripts() { _areScriptsInitialized = false; }
public:
virtual size_t ScriptCount() const = 0;
const ScriptAggregator& GetScriptIterator() {
if (!_areScriptsInitialized) {
_scripts.Reserve(ScriptCount());
GetActiveScripts(_scripts);
_scriptsIterator = ScriptAggregator(_scripts);
_areScriptsInitialized = true;

View File

@ -53,6 +53,7 @@ namespace CreatureLib::Battling {
const CreatureIndex& GetTarget() const noexcept { return _target; }
Script* GetAttackScript() const noexcept { return _attackScript; }
size_t ScriptCount() const override { return 1 + GetUser()->ScriptCount(); }
protected:
void GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) override {

View File

@ -10,6 +10,7 @@ namespace CreatureLib::Battling {
FleeTurnChoice(Creature* user) : BaseTurnChoice(user) {}
TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind ::Flee; }
size_t ScriptCount() const override { return GetUser()->ScriptCount(); }
protected:
void GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) override {

View File

@ -9,6 +9,7 @@ namespace CreatureLib::Battling {
PassTurnChoice(Creature* c) : BaseTurnChoice(c) {}
TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind ::Pass; }
size_t ScriptCount() const override { return GetUser()->ScriptCount(); }
protected:
void GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) override {

View File

@ -13,6 +13,7 @@ namespace CreatureLib::Battling {
TurnChoiceKind GetKind() const noexcept final { return TurnChoiceKind::Switch; }
inline Creature* GetNewCreature() const noexcept { return _newCreature; }
size_t ScriptCount() const override { return GetUser()->ScriptCount(); }
protected:
void GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) override {

View File

@ -24,6 +24,7 @@ public:
Script* ScriptPtr = nullptr;
protected:
size_t ScriptCount() const override { return 1; }
void GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) override {
scripts.Append(ScriptWrapper::FromScript(&ScriptPtr));
}
@ -34,6 +35,7 @@ public:
ScriptSet Set;
protected:
size_t ScriptCount() const override { return 1; }
void GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) override {
scripts.Append(ScriptWrapper::FromSet(&Set));
}