Core/Optimization

* avoid using expensive numeric cast for anything but user inputs
   especially within nested loops
 * CharStats aggregation should be about x5 faster
This commit is contained in:
Sarjuuk
2025-08-08 18:14:53 +02:00
parent 0753bfbcf6
commit eb3b4ca5ec
3 changed files with 15 additions and 14 deletions

View File

@@ -444,10 +444,10 @@ class StatsContainer implements \Countable
{ {
if (isset($json[$key])) // 0 is a valid amount! if (isset($json[$key])) // 0 is a valid amount!
{ {
$float = Stat::getFlags($idx) & Stat::FLAG_FLOAT_VALUE; if (Stat::getFlags($idx) & Stat::FLAG_FLOAT_VALUE)
Util::arraySumByKey($this->store, [$idx => (float)$json[$key]]);
if (Util::checkNumeric($json[$key], $float ? NUM_CAST_FLOAT : NUM_CAST_INT)) else
Util::arraySumByKey($this->store, [$idx => $json[$key]]); Util::arraySumByKey($this->store, [$idx => (int)$json[$key]]);
} }
if ($pruneFromSrc) if ($pruneFromSrc)

View File

@@ -1696,7 +1696,6 @@ class ItemList extends BaseType
$json = array( $json = array(
'id' => $this->id, 'id' => $this->id,
'name' => $this->getField('name', true),
'quality' => ITEM_QUALITY_HEIRLOOM - $this->curTpl['quality'], 'quality' => ITEM_QUALITY_HEIRLOOM - $this->curTpl['quality'],
'icon' => $this->curTpl['iconString'], 'icon' => $this->curTpl['iconString'],
'classs' => $class, 'classs' => $class,
@@ -1709,7 +1708,7 @@ class ItemList extends BaseType
'level' => $this->curTpl['itemLevel'], 'level' => $this->curTpl['itemLevel'],
'reqlevel' => $this->curTpl['requiredLevel'], 'reqlevel' => $this->curTpl['requiredLevel'],
'displayid' => $this->curTpl['displayId'], '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'], 'holres' => $this->curTpl['resHoly'],
'firres' => $this->curTpl['resFire'], 'firres' => $this->curTpl['resFire'],
'natres' => $this->curTpl['resNature'], 'natres' => $this->curTpl['resNature'],
@@ -1729,15 +1728,19 @@ class ItemList extends BaseType
'scaflags' => $this->curTpl['scalingStatValue'] 'scaflags' => $this->curTpl['scalingStatValue']
); );
$json = array_map('intval', $json);
$json['name'] = $this->getField('name', true);
if ($class == ITEM_CLASS_AMMUNITION) if ($class == ITEM_CLASS_AMMUNITION)
$json['dps'] = round(($this->curTpl['tplDmgMin1'] + $this->curTpl['dmgMin2'] + $this->curTpl['tplDmgMax1'] + $this->curTpl['dmgMax2']) / 2, 2); $json['dps'] = round(($this->curTpl['tplDmgMin1'] + $this->curTpl['dmgMin2'] + $this->curTpl['tplDmgMax1'] + $this->curTpl['dmgMax2']) / 2, 2);
else if ($class == ITEM_CLASS_WEAPON) else if ($class == ITEM_CLASS_WEAPON)
{ {
$json['dmgtype1'] = $this->curTpl['dmgType1']; $json['dmgtype1'] = (int)$this->curTpl['dmgType1'];
$json['dmgmin1'] = $this->curTpl['tplDmgMin1'] + $this->curTpl['dmgMin2']; $json['dmgmin1'] = (int)($this->curTpl['tplDmgMin1'] + $this->curTpl['dmgMin2']);
$json['dmgmax1'] = $this->curTpl['tplDmgMax1'] + $this->curTpl['dmgMax2']; $json['dmgmax1'] = (int)($this->curTpl['tplDmgMax1'] + $this->curTpl['dmgMax2']);
$json['speed'] = round($this->curTpl['delay'] / 1000, 2); $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()) if ($this->isRangedWeapon())
{ {
@@ -1768,8 +1771,6 @@ class ItemList extends BaseType
if (!$v && !in_array($k, ['classs', 'subclass', 'quality', 'side', 'gearscore'])) if (!$v && !in_array($k, ['classs', 'subclass', 'quality', 'side', 'gearscore']))
unset($json[$k]); unset($json[$k]);
Util::checkNumeric($json);
$this->json[$json['id']] = $json; $this->json[$json['id']] = $json;
} }
} }

View File

@@ -639,7 +639,7 @@ abstract class Util
{ {
if (is_array($v)) if (is_array($v))
$ref[$k] = []; $ref[$k] = [];
else if (Util::checkNumeric($v)) else if (is_numeric($v))
$ref[$k] = 0; $ref[$k] = 0;
else else
continue; continue;
@@ -647,7 +647,7 @@ abstract class Util
if (is_array($ref[$k]) && is_array($v)) if (is_array($ref[$k]) && is_array($v))
Util::arraySumByKey($ref[$k], $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; $ref[$k] += $v;
} }
} }