Make ExecutingAttack creature list a raw C array, as this is a hot spot and could use some optimization.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-06-04 21:58:28 +02:00
parent 657d646fda
commit 57e8595bdf
3 changed files with 41 additions and 34 deletions

View File

@@ -36,7 +36,8 @@ namespace CreatureLib::Battling {
};
private:
ArbUt::List<ArbUt::BorrowedPtr<Creature>> _targets;
const ArbUt::BorrowedPtr<Creature>* _targets;
uint8_t _targetCount;
uint8_t _numberHits;
std::unique_ptr<HitData[]> _hits;
ArbUt::BorrowedPtr<Creature> _user;
@@ -47,13 +48,10 @@ namespace CreatureLib::Battling {
ExecutingAttack(const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& targets, uint8_t numberHits,
ArbUt::BorrowedPtr<Creature> user, const ArbUt::BorrowedPtr<LearnedAttack>& attack,
const std::unique_ptr<Script>& script)
: _targets(targets.Count()), _numberHits(numberHits),
: _targets(targets.RawData()), _targetCount(targets.Count()), _numberHits(numberHits),
_hits(std::make_unique<HitData[]>(targets.Count() * numberHits)), _user(user), _attack(attack) {
AssertNotNull(user)
AssertNotNull(attack)
for (auto target : targets) {
_targets.Append(target);
}
// Take ownership of the script of the attack choice, and give attack choice our initial nullptr.
_script.swap(const_cast<std::unique_ptr<Script>&>(script));
}
@@ -63,7 +61,7 @@ namespace CreatureLib::Battling {
virtual ~ExecutingAttack() noexcept = default;
HitData& GetHitData(Creature* creature, uint8_t hit) {
for (size_t i = 0; i < _targets.Count(); i++) {
for (uint8_t i = 0; i < _targetCount; i++) {
if (_targets[i] == creature) {
return _hits[i * _numberHits + hit];
}
@@ -72,7 +70,7 @@ namespace CreatureLib::Battling {
}
HitData* GetTargetIteratorBegin(Creature* creature) {
for (size_t i = 0; i < _targets.Count(); i++) {
for (uint8_t i = 0; i < _targetCount; i++) {
if (_targets[i] == creature) {
return &_hits[i * _numberHits * sizeof(HitData)];
}
@@ -80,13 +78,22 @@ namespace CreatureLib::Battling {
throw CreatureException("Invalid target requested.");
}
bool IsCreatureTarget(Creature* creature) noexcept { return _targets.IndexOf(creature) != (size_t)-1; }
const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& GetTargets() noexcept { return _targets; }
uint8_t GetNumberOfHits() const noexcept { return _numberHits; }
bool IsCreatureTarget(Creature* creature) noexcept {
for (uint8_t i = 0; i < _targetCount; i++) {
if (_targets[i] == creature) {
return true;
}
}
return false;
}
ArbUt::BorrowedPtr<Creature> GetUser() noexcept { return _user; }
inline const uint8_t GetTargetCount() const noexcept { return _targetCount; }
inline const ArbUt::BorrowedPtr<Creature>* GetTargets() const noexcept { return _targets; }
inline uint8_t GetNumberOfHits() const noexcept { return _numberHits; }
const ArbUt::BorrowedPtr<LearnedAttack>& GetAttack() noexcept { return _attack; }
inline const ArbUt::BorrowedPtr<Creature>& GetUser() noexcept { return _user; }
inline const ArbUt::BorrowedPtr<LearnedAttack>& GetAttack() noexcept { return _attack; }
size_t ScriptCount() const override { return _user->ScriptCount() + 1; }
protected: