Skip to content

Commit

Permalink
perf: change shared_ptrs from copy to reference (opentibiabr#2998)
Browse files Browse the repository at this point in the history
This changes the usage of 'shared_ptr' from being copied to
being passed as a 'const reference'. This adjustment aims to improve
performance by avoiding unnecessary increments in the reference count,
especially for functions that do not need to modify the shared object.

---------

Co-authored-by: Eduardo Dantas <[email protected]>
  • Loading branch information
beats-dh and dudantas authored Oct 30, 2024
1 parent d6c5ec3 commit 6c00cc0
Show file tree
Hide file tree
Showing 339 changed files with 8,989 additions and 8,206 deletions.
1 change: 1 addition & 0 deletions cmake/modules/CanaryLib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ target_include_directories(${PROJECT_NAME}_lib
${GMP_INCLUDE_DIRS}
${LUAJIT_INCLUDE_DIRS}
${PARALLEL_HASHMAP_INCLUDE_DIRS}
${ATOMIC_QUEUE_INCLUDE_DIRS}
)

# *****************************************************************************
Expand Down
273 changes: 137 additions & 136 deletions src/creatures/combat/combat.cpp

Large diffs are not rendered by default.

108 changes: 53 additions & 55 deletions src/creatures/combat/combat.hpp

Large diffs are not rendered by default.

126 changes: 63 additions & 63 deletions src/creatures/combat/condition.cpp

Large diffs are not rendered by default.

62 changes: 31 additions & 31 deletions src/creatures/combat/condition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Condition : public SharedObject {
virtual ~Condition() = default;

virtual bool startCondition(std::shared_ptr<Creature> creature);
virtual bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval);
virtual bool executeCondition(const std::shared_ptr<Creature> &creature, int32_t interval);
virtual void endCondition(std::shared_ptr<Creature> creature) = 0;
virtual void addCondition(std::shared_ptr<Creature> creature, std::shared_ptr<Condition> condition) = 0;
virtual std::unordered_set<PlayerIcon> getIcons() const;
Expand Down Expand Up @@ -75,7 +75,7 @@ class Condition : public SharedObject {
bool isBuff {};
bool m_isPersistent {};

virtual bool updateCondition(std::shared_ptr<Condition> addCondition);
virtual bool updateCondition(const std::shared_ptr<Condition> &addCondition);

private:
SoundEffect_t tickSound = SoundEffect_t::SILENCE;
Expand All @@ -91,7 +91,7 @@ class ConditionGeneric : public Condition {
Condition(initId, initType, initTicks, initBuff, initSubId, isPersistent) { }

bool startCondition(std::shared_ptr<Creature> creature) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;
bool executeCondition(const std::shared_ptr<Creature> &creature, int32_t interval) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, std::shared_ptr<Condition> condition) override;
std::unordered_set<PlayerIcon> getIcons() const override;
Expand All @@ -107,7 +107,7 @@ class ConditionAttributes final : public ConditionGeneric {
ConditionGeneric(initId, initType, initTicks, initBuff, initSubId) { }

bool startCondition(std::shared_ptr<Creature> creature) final;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) final;
bool executeCondition(const std::shared_ptr<Creature> &creature, int32_t interval) final;
void endCondition(std::shared_ptr<Creature> creature) final;
void addCondition(std::shared_ptr<Creature> creature, std::shared_ptr<Condition> condition) final;

Expand Down Expand Up @@ -153,19 +153,19 @@ class ConditionAttributes final : public ConditionGeneric {

bool disableDefense = false;

void updatePercentStats(std::shared_ptr<Player> player);
void updateStats(std::shared_ptr<Player> player);
void updatePercentSkills(std::shared_ptr<Player> player);
void updateSkills(std::shared_ptr<Player> player);
void updateBuffs(std::shared_ptr<Creature> creature);
void updatePercentStats(const std::shared_ptr<Player> &player);
void updateStats(const std::shared_ptr<Player> &player) const;
void updatePercentSkills(const std::shared_ptr<Player> &player);
void updateSkills(const std::shared_ptr<Player> &player) const;
void updateBuffs(const std::shared_ptr<Creature> &creature) const;

// 12.72 mechanics
void updatePercentAbsorbs(std::shared_ptr<Creature> creature);
void updateAbsorbs(std::shared_ptr<Creature> creature) const;
void updatePercentIncreases(std::shared_ptr<Creature> creature);
void updateIncreases(std::shared_ptr<Creature> creature) const;
void updateCharmChanceModifier(std::shared_ptr<Creature> creature) const;
void updatePercentBuffs(std::shared_ptr<Creature> creature);
void updatePercentAbsorbs(const std::shared_ptr<Creature> &creature);
void updateAbsorbs(const std::shared_ptr<Creature> &creature) const;
void updatePercentIncreases(const std::shared_ptr<Creature> &creature);
void updateIncreases(const std::shared_ptr<Creature> &creature) const;
void updateCharmChanceModifier(const std::shared_ptr<Creature> &creature) const;
void updatePercentBuffs(const std::shared_ptr<Creature> &creature);
};

class ConditionRegeneration final : public ConditionGeneric {
Expand All @@ -176,12 +176,12 @@ class ConditionRegeneration final : public ConditionGeneric {
bool startCondition(std::shared_ptr<Creature> creature) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, std::shared_ptr<Condition> addCondition) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;
bool executeCondition(const std::shared_ptr<Creature> &creature, int32_t interval) override;

bool setParam(ConditionParam_t param, int32_t value) override;

uint32_t getHealthTicks(std::shared_ptr<Creature> creature) const;
uint32_t getManaTicks(std::shared_ptr<Creature> creature) const;
uint32_t getHealthTicks(const std::shared_ptr<Creature> &creature) const;
uint32_t getManaTicks(const std::shared_ptr<Creature> &creature) const;

std::shared_ptr<Condition> clone() const override {
return std::make_shared<ConditionRegeneration>(*this);
Expand Down Expand Up @@ -231,7 +231,7 @@ class ConditionSoul final : public ConditionGeneric {
ConditionGeneric(initId, initType, iniTicks, initBuff, initSubId) { }

void addCondition(std::shared_ptr<Creature> creature, std::shared_ptr<Condition> addCondition) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;
bool executeCondition(const std::shared_ptr<Creature> &creature, int32_t interval) override;

bool setParam(ConditionParam_t param, int32_t value) override;

Expand Down Expand Up @@ -271,7 +271,7 @@ class ConditionDamage final : public Condition {
static void generateDamageList(int32_t amount, int32_t start, std::list<int32_t> &list);

bool startCondition(std::shared_ptr<Creature> creature) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;
bool executeCondition(const std::shared_ptr<Creature> &creature, int32_t interval) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, std::shared_ptr<Condition> condition) override;
std::unordered_set<PlayerIcon> getIcons() const override;
Expand Down Expand Up @@ -310,9 +310,9 @@ class ConditionDamage final : public Condition {
std::list<IntervalInfo> damageList;

bool getNextDamage(int32_t &damage);
bool doDamage(std::shared_ptr<Creature> creature, int32_t healthChange);
bool doDamage(const std::shared_ptr<Creature> &creature, int32_t healthChange) const;

bool updateCondition(std::shared_ptr<Condition> addCondition) override;
bool updateCondition(const std::shared_ptr<Condition> &addCondition) override;
};

class ConditionFeared final : public Condition {
Expand All @@ -322,7 +322,7 @@ class ConditionFeared final : public Condition {
Condition(intiId, initType, initTicks, initBuff, initSubId) { }

bool startCondition(std::shared_ptr<Creature> creature) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;
bool executeCondition(const std::shared_ptr<Creature> &creature, int32_t interval) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, std::shared_ptr<Condition> condition) override;
std::unordered_set<PlayerIcon> getIcons() const override;
Expand All @@ -334,11 +334,11 @@ class ConditionFeared final : public Condition {
bool setPositionParam(ConditionParam_t param, const Position &pos) override;

private:
bool canWalkTo(std::shared_ptr<Creature> creature, Position pos, Direction moveDirection) const;
bool getFleeDirection(std::shared_ptr<Creature> creature);
bool getFleePath(std::shared_ptr<Creature> creature, const Position &pos, std::vector<Direction> &dirList);
bool getRandomDirection(std::shared_ptr<Creature> creature, Position pos);
bool isStuck(std::shared_ptr<Creature> creature, Position pos) const;
bool canWalkTo(const std::shared_ptr<Creature> &creature, Position pos, Direction moveDirection) const;
bool getFleeDirection(const std::shared_ptr<Creature> &creature);
bool getFleePath(const std::shared_ptr<Creature> &creature, const Position &pos, std::vector<Direction> &dirList);
bool getRandomDirection(const std::shared_ptr<Creature> &creature, Position pos);
bool isStuck(const std::shared_ptr<Creature> &creature, Position pos) const;

std::vector<Direction> m_directionsVector {
DIRECTION_NORTH,
Expand All @@ -361,7 +361,7 @@ class ConditionSpeed final : public Condition {
Condition(initId, initType, initTicks, initBuff, initSubId), speedDelta(initChangeSpeed) { }

bool startCondition(std::shared_ptr<Creature> creature) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;
bool executeCondition(const std::shared_ptr<Creature> &creature, int32_t interval) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, std::shared_ptr<Condition> condition) override;
std::unordered_set<PlayerIcon> getIcons() const override;
Expand Down Expand Up @@ -396,7 +396,7 @@ class ConditionOutfit final : public Condition {
Condition(initId, initType, initTicks, initBuff, initSubId) { }

bool startCondition(std::shared_ptr<Creature> creature) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;
bool executeCondition(const std::shared_ptr<Creature> &creature, int32_t interval) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, std::shared_ptr<Condition> condition) override;

Expand All @@ -422,7 +422,7 @@ class ConditionLight final : public Condition {
Condition(initId, initType, initTicks, initBuff, initSubId), lightInfo(initLightlevel, initLightcolor) { }

bool startCondition(std::shared_ptr<Creature> creature) override;
bool executeCondition(std::shared_ptr<Creature> creature, int32_t interval) override;
bool executeCondition(const std::shared_ptr<Creature> &creature, int32_t interval) override;
void endCondition(std::shared_ptr<Creature> creature) override;
void addCondition(std::shared_ptr<Creature> creature, std::shared_ptr<Condition> addCondition) override;

Expand Down
Loading

0 comments on commit 6c00cc0

Please sign in to comment.