Skip to content

Commit

Permalink
perf: replace manual function name extraction with std::source_locati…
Browse files Browse the repository at this point in the history
…on (#3132)

Replaced the previous string concatenation approach for logging function
names with `std::source_location::current().function_name()`. This
change significantly improves performance by avoiding runtime string
operations. Instead of manually manipulating function signatures using
`methodName` to extract function names, `std::source_location` provides
this information directly in an efficient manner.
  • Loading branch information
dudantas authored Nov 19, 2024
1 parent e0cee27 commit b5f71a8
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 74 deletions.
12 changes: 6 additions & 6 deletions src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ void Combat::setupChain(const std::shared_ptr<Weapon> &weapon) {
}

bool Combat::doCombatChain(const std::shared_ptr<Creature> &caster, const std::shared_ptr<Creature> &target, bool aggressive) const {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (!params.chainCallback) {
return false;
}
Expand Down Expand Up @@ -1469,7 +1469,7 @@ void Combat::setRuneSpellName(const std::string &value) {

std::vector<std::pair<Position, std::vector<uint32_t>>> Combat::pickChainTargets(const std::shared_ptr<Creature> &caster, const CombatParams &params, uint8_t chainDistance, uint8_t maxTargets, bool backtracking, bool aggressive, const std::shared_ptr<Creature> &initialTarget /* = nullptr */) {
Benchmark bm_pickChain;
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (!caster) {
return {};
}
Expand Down Expand Up @@ -1512,7 +1512,7 @@ std::vector<std::pair<Position, std::vector<uint32_t>>> Combat::pickChainTargets
}

if (closestSpectator) {
g_logger().trace("[{}] closestSpectator: {}", __METHOD_NAME__, closestSpectator->getName());
g_logger().trace("[{}] closestSpectator: {}", __FUNCTION__, closestSpectator->getName());

bool found = false;
for (auto &[pos, vec] : resultMap) {
Expand All @@ -1531,15 +1531,15 @@ std::vector<std::pair<Position, std::vector<uint32_t>>> Combat::pickChainTargets
continue;
}
if (backtracking) {
g_logger().debug("[{}] backtracking", __METHOD_NAME__);
g_logger().debug("[{}] backtracking", __FUNCTION__);
targets.pop_back();
backtrackingAttempts--;
continue;
}
break;
}

g_logger().debug("[{}] resultMap: {} in {} ms", __METHOD_NAME__, resultMap.size(), bm_pickChain.duration());
g_logger().debug("[{}] resultMap: {} in {} ms", __FUNCTION__, resultMap.size(), bm_pickChain.duration());
return resultMap;
}

Expand Down Expand Up @@ -2221,7 +2221,7 @@ void MagicField::onStepInField(const std::shared_ptr<Creature> &creature) {
}

void Combat::applyExtensions(const std::shared_ptr<Creature> &caster, const std::shared_ptr<Creature> &target, CombatDamage &damage, const CombatParams &params) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (damage.extension || !caster || damage.primary.type == COMBAT_HEALING) {
return;
}
Expand Down
50 changes: 25 additions & 25 deletions src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Creature::~Creature() {
}

bool Creature::canSee(const Position &myPos, const Position &pos, int32_t viewRangeX, int32_t viewRangeY) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (myPos.z <= MAP_INIT_SURFACE_LAYER) {
// we are on ground level or above (7 -> 0)
// view is from 7 -> 0
Expand Down Expand Up @@ -95,7 +95,7 @@ int32_t Creature::getWalkSize() {
}

void Creature::onThink(uint32_t interval) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);

const auto &followCreature = getFollowCreature();
const auto &master = getMaster();
Expand Down Expand Up @@ -168,7 +168,7 @@ void Creature::onCreatureWalk() {

checkingWalkCreature = true;

metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);

g_dispatcher().addWalkEvent([self = getCreature(), this] {
checkingWalkCreature = false;
Expand Down Expand Up @@ -287,7 +287,7 @@ void Creature::stopEventWalk() {
}

void Creature::onCreatureAppear(const std::shared_ptr<Creature> &creature, bool isLogin) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (creature.get() == this) {
if (isLogin) {
setLastPosition(getPosition());
Expand All @@ -296,7 +296,7 @@ void Creature::onCreatureAppear(const std::shared_ptr<Creature> &creature, bool
}

void Creature::onRemoveCreature(const std::shared_ptr<Creature> &creature, bool) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
onCreatureDisappear(creature, true);

// Update player from monster target list (avoid memory usage after clean)
Expand All @@ -307,7 +307,7 @@ void Creature::onRemoveCreature(const std::shared_ptr<Creature> &creature, bool)
}

void Creature::onCreatureDisappear(const std::shared_ptr<Creature> &creature, bool isLogout) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (getAttackedCreature() == creature) {
setAttackedCreature(nullptr);
onAttackedCreatureDisappear(isLogout);
Expand All @@ -320,15 +320,15 @@ void Creature::onCreatureDisappear(const std::shared_ptr<Creature> &creature, bo
}

void Creature::onChangeZone(ZoneType_t zone) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
const auto &attackedCreature = getAttackedCreature();
if (attackedCreature && zone == ZONE_PROTECTION) {
onCreatureDisappear(attackedCreature, false);
}
}

void Creature::onAttackedCreatureChangeZone(ZoneType_t zone) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (zone == ZONE_PROTECTION) {
const auto &attackedCreature = getAttackedCreature();
if (attackedCreature) {
Expand All @@ -338,7 +338,7 @@ void Creature::onAttackedCreatureChangeZone(ZoneType_t zone) {
}

void Creature::checkSummonMove(const Position &newPos, bool teleportSummon) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (hasSummons()) {
std::vector<std::shared_ptr<Creature>> despawnMonsterList;
for (const auto &summon : getSummons()) {
Expand Down Expand Up @@ -385,7 +385,7 @@ void Creature::checkSummonMove(const Position &newPos, bool teleportSummon) {
}

void Creature::onCreatureMove(const std::shared_ptr<Creature> &creature, const std::shared_ptr<Tile> &newTile, const Position &newPos, const std::shared_ptr<Tile> &oldTile, const Position &oldPos, bool teleport) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (creature.get() == this) {
lastStep = OTSYS_TIME();
lastStepCost = 1;
Expand Down Expand Up @@ -447,7 +447,7 @@ void Creature::onCreatureMove(const std::shared_ptr<Creature> &creature, const s
}

void Creature::onDeath() {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
bool lastHitUnjustified = false;
bool mostDamageUnjustified = false;
const auto &lastHitCreature = g_game().getCreatureByID(lastHitCreatureId);
Expand Down Expand Up @@ -585,7 +585,7 @@ void Creature::onDeath() {
}

bool Creature::dropCorpse(const std::shared_ptr<Creature> &lastHitCreature, const std::shared_ptr<Creature> &mostDamageCreature, bool lastHitUnjustified, bool mostDamageUnjustified) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (!lootDrop && getMonster()) {
if (getMaster()) {
// Scripting event onDeath
Expand Down Expand Up @@ -882,7 +882,7 @@ void Creature::goToFollowCreature_async(std::function<void()> &&onComplete) {
}

void Creature::goToFollowCreature() {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
const auto &followCreature = getFollowCreature();
if (!followCreature) {
return;
Expand Down Expand Up @@ -937,7 +937,7 @@ bool Creature::canFollowMaster() const {
}

bool Creature::setFollowCreature(const std::shared_ptr<Creature> &creature) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (creature) {
if (getFollowCreature() == creature) {
return true;
Expand Down Expand Up @@ -1086,15 +1086,15 @@ void Creature::onAttackedCreatureDrainHealth(const std::shared_ptr<Creature> &ta
}

void Creature::onAttackedCreatureKilled(const std::shared_ptr<Creature> &target) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (target != getCreature()) {
uint64_t gainExp = target->getGainedExperience(static_self_cast<Creature>());
onGainExperience(gainExp, target);
}
}

bool Creature::deprecatedOnKilledCreature(const std::shared_ptr<Creature> &target, bool lastHit) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
const auto &master = getMaster();
if (master) {
master->deprecatedOnKilledCreature(target, lastHit);
Expand All @@ -1109,7 +1109,7 @@ bool Creature::deprecatedOnKilledCreature(const std::shared_ptr<Creature> &targe
}

void Creature::onGainExperience(uint64_t gainExp, const std::shared_ptr<Creature> &target) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
const auto &master = getMaster();
if (gainExp == 0 || !master) {
return;
Expand Down Expand Up @@ -1140,7 +1140,7 @@ void Creature::onGainExperience(uint64_t gainExp, const std::shared_ptr<Creature
}

bool Creature::setMaster(const std::shared_ptr<Creature> &newMaster, bool reloadCreature /* = false*/) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
// Persists if this creature has ever been a summon
this->summoned = true;
const auto &oldMaster = getMaster();
Expand Down Expand Up @@ -1173,7 +1173,7 @@ bool Creature::setMaster(const std::shared_ptr<Creature> &newMaster, bool reload
}

bool Creature::addCondition(const std::shared_ptr<Condition> &condition, bool attackerPlayer /* = false*/) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (condition == nullptr) {
return false;
}
Expand Down Expand Up @@ -1211,7 +1211,7 @@ bool Creature::addCombatCondition(const std::shared_ptr<Condition> &condition, b
}

void Creature::removeCondition(ConditionType_t type) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
auto it = conditions.begin(), end = conditions.end();
while (it != end) {
std::shared_ptr<Condition> condition = *it;
Expand All @@ -1229,7 +1229,7 @@ void Creature::removeCondition(ConditionType_t type) {
}

void Creature::removeCondition(ConditionType_t conditionType, ConditionId_t conditionId, bool force /* = false*/) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
auto it = conditions.begin();
const auto end = conditions.end();
while (it != end) {
Expand Down Expand Up @@ -1292,7 +1292,7 @@ std::shared_ptr<Condition> Creature::getCondition(ConditionType_t type) const {
}

std::shared_ptr<Condition> Creature::getCondition(ConditionType_t type, ConditionId_t conditionId, uint32_t subId /* = 0*/) const {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
for (const auto &condition : conditions) {
if (condition->getType() == type && condition->getId() == conditionId && condition->getSubId() == subId) {
return condition;
Expand All @@ -1312,7 +1312,7 @@ std::vector<std::shared_ptr<Condition>> Creature::getConditionsByType(ConditionT
}

void Creature::executeConditions(uint32_t interval) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
auto it = conditions.begin(), end = conditions.end();
while (it != end) {
std::shared_ptr<Condition> condition = *it;
Expand All @@ -1331,7 +1331,7 @@ void Creature::executeConditions(uint32_t interval) {
}

bool Creature::hasCondition(ConditionType_t type, uint32_t subId /* = 0*/) const {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (isSuppress(type, false)) {
return false;
}
Expand Down Expand Up @@ -1607,7 +1607,7 @@ ZoneType_t Creature::getZoneType() {
}

bool Creature::getPathTo(const Position &targetPos, std::vector<Direction> &dirList, const FindPathParams &fpp) {
metrics::method_latency measure(__METHOD_NAME__);
metrics::method_latency measure(__METRICS_METHOD_NAME__);
if (fpp.maxSearchDist != 0 || fpp.keepDistance) {
return g_game().map.getPathMatchingCond(getCreature(), targetPos, dirList, FrozenPathingConditionCall(targetPos), fpp);
}
Expand Down
Loading

0 comments on commit b5f71a8

Please sign in to comment.