From eb3b4ca5ec611be6ce18948bc20b7084fe8b1327 Mon Sep 17 00:00:00 2001 From: Sarjuuk Date: Fri, 8 Aug 2025 18:14:53 +0200 Subject: [PATCH] Core/Optimization * avoid using expensive numeric cast for anything but user inputs especially within nested loops * CharStats aggregation should be about x5 faster --- includes/game/chrstatistics.php | 8 ++++---- includes/types/item.class.php | 17 +++++++++-------- includes/utilities.php | 4 ++-- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/includes/game/chrstatistics.php b/includes/game/chrstatistics.php index fc1cb193..77dbe581 100644 --- a/includes/game/chrstatistics.php +++ b/includes/game/chrstatistics.php @@ -444,10 +444,10 @@ class StatsContainer implements \Countable { if (isset($json[$key])) // 0 is a valid amount! { - $float = Stat::getFlags($idx) & Stat::FLAG_FLOAT_VALUE; - - if (Util::checkNumeric($json[$key], $float ? NUM_CAST_FLOAT : NUM_CAST_INT)) - Util::arraySumByKey($this->store, [$idx => $json[$key]]); + if (Stat::getFlags($idx) & Stat::FLAG_FLOAT_VALUE) + Util::arraySumByKey($this->store, [$idx => (float)$json[$key]]); + else + Util::arraySumByKey($this->store, [$idx => (int)$json[$key]]); } if ($pruneFromSrc) diff --git a/includes/types/item.class.php b/includes/types/item.class.php index 043001ce..506e7951 100644 --- a/includes/types/item.class.php +++ b/includes/types/item.class.php @@ -1696,7 +1696,6 @@ class ItemList extends BaseType $json = array( 'id' => $this->id, - 'name' => $this->getField('name', true), 'quality' => ITEM_QUALITY_HEIRLOOM - $this->curTpl['quality'], 'icon' => $this->curTpl['iconString'], 'classs' => $class, @@ -1709,7 +1708,7 @@ class ItemList extends BaseType 'level' => $this->curTpl['itemLevel'], 'reqlevel' => $this->curTpl['requiredLevel'], 'displayid' => $this->curTpl['displayId'], - // 'commondrop' => 'true' / null // set if the item is a loot-filler-item .. check common ref-templates..? + // 'commondrop' => 'true' / null // set if the item is a loot-filler-item .. check common ref-templates..? 'holres' => $this->curTpl['resHoly'], 'firres' => $this->curTpl['resFire'], 'natres' => $this->curTpl['resNature'], @@ -1729,15 +1728,19 @@ class ItemList extends BaseType 'scaflags' => $this->curTpl['scalingStatValue'] ); + $json = array_map('intval', $json); + + $json['name'] = $this->getField('name', true); + if ($class == ITEM_CLASS_AMMUNITION) $json['dps'] = round(($this->curTpl['tplDmgMin1'] + $this->curTpl['dmgMin2'] + $this->curTpl['tplDmgMax1'] + $this->curTpl['dmgMax2']) / 2, 2); else if ($class == ITEM_CLASS_WEAPON) { - $json['dmgtype1'] = $this->curTpl['dmgType1']; - $json['dmgmin1'] = $this->curTpl['tplDmgMin1'] + $this->curTpl['dmgMin2']; - $json['dmgmax1'] = $this->curTpl['tplDmgMax1'] + $this->curTpl['dmgMax2']; + $json['dmgtype1'] = (int)$this->curTpl['dmgType1']; + $json['dmgmin1'] = (int)($this->curTpl['tplDmgMin1'] + $this->curTpl['dmgMin2']); + $json['dmgmax1'] = (int)($this->curTpl['tplDmgMax1'] + $this->curTpl['dmgMax2']); $json['speed'] = round($this->curTpl['delay'] / 1000, 2); - $json['dps'] = $json['speed'] ? round(($json['dmgmin1'] + $json['dmgmax1']) / (2 * $json['speed']), 1) : 0; + $json['dps'] = $json['speed'] ? round(($json['dmgmin1'] + $json['dmgmax1']) / (2 * $json['speed']), 1) : 0.0; if ($this->isRangedWeapon()) { @@ -1768,8 +1771,6 @@ class ItemList extends BaseType if (!$v && !in_array($k, ['classs', 'subclass', 'quality', 'side', 'gearscore'])) unset($json[$k]); - Util::checkNumeric($json); - $this->json[$json['id']] = $json; } } diff --git a/includes/utilities.php b/includes/utilities.php index 4e682350..57c22756 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -639,7 +639,7 @@ abstract class Util { if (is_array($v)) $ref[$k] = []; - else if (Util::checkNumeric($v)) + else if (is_numeric($v)) $ref[$k] = 0; else continue; @@ -647,7 +647,7 @@ abstract class Util if (is_array($ref[$k]) && is_array($v)) Util::arraySumByKey($ref[$k], $v); - else if (Util::checkNumeric($ref[$k]) && Util::checkNumeric($v)) + else if (is_numeric($ref[$k]) && is_numeric($v)) $ref[$k] += $v; } }