Skip to content

PackedHashtableOptimization ​

info on by default PHP ≥ 7.0

Group: Performance · PhpStorm name: PackedHashtableOptimizationInspection

Since PHP 7 an array whose keys are integers inserted in ascending order can be stored in a compact "packed" layout. An array literal that lists integer keys out of order, or spells integer keys as numeric strings, misses that layout. Sorting the keys or writing them as integers lets the engine use it.

Example ​

php
<?php
$grid = [
    5 => 'e', 3 => 'c', 9 => 'i'
];
$cells = array(
    -1 => 'a', -4 => 'b', 0 => 'c', 2 => 'd'
);
$names = [
    '3' => 'x', 4 => 'y', '8' => 'z'
];
$mixed = array('7' => 1, '2' => 2, 9 => 3);

$small = [2 => 'b', 1 => 'a'];
$sorted = [1 => 'a', 2 => 'b', 2 => 'c'];
$labels = ['3' => 'x', 'label' => 'y', 5 => 'z'];
$padded = ['07' => 'x', 8 => 'y', 9 => 'z'];
$dyn = [$k => 1, 4 => 2, 6 => 3];
$plain = ['a', 'b', 'c'];
$float = [3.5 => 'a', 2 => 'b', 1 => 'c'];

Reported:

  • line 2: Sort the integer keys ascending so the array can be stored packed.
  • line 5: Sort the integer keys ascending so the array can be stored packed.
  • line 8: Write the keys as integers so the array can be stored packed.
  • line 11: Sort the integer keys ascending so the array can be stored packed.

Configure ​

In custos.json:

json
{
  "rules": {
    "PackedHashtableOptimization": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore PackedHashtableOptimization

/**
 * @noinspection PackedHashtableOptimizationInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.