Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 70 additions & 5 deletions src/PhpSpreadsheet/Collection/Cells.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ class Cells
*/
private array $index = [];

/**
* Flag to avoid sorting the index every time.
*/
private bool $indexSorted = false;

/**
* Index keys cache to avoid recalculating on large arrays.
*
* @var null|string[]
*/
private ?array $indexKeysCache = null;

/**
* Index values cache to avoid recalculating on large arrays.
*
* @var null|int[]
*/
private ?array $indexValuesCache = null;

/**
* Prefix used to uniquely identify cache data for this worksheet.
*/
Expand Down Expand Up @@ -112,6 +131,10 @@ public function delete(string $cellCoordinate): void

unset($this->index[$cellCoordinate]);

// Clear index caches
$this->indexKeysCache = null;
$this->indexValuesCache = null;

// Delete the entry from cache
$this->cache->delete($this->cachePrefix . $cellCoordinate);
}
Expand All @@ -123,7 +146,12 @@ public function delete(string $cellCoordinate): void
*/
public function getCoordinates(): array
{
return array_keys($this->index);
// Build or rebuild index keys cache
if ($this->indexKeysCache === null) {
$this->indexKeysCache = array_keys($this->index);
}

return $this->indexKeysCache;
}

/**
Expand All @@ -133,9 +161,21 @@ public function getCoordinates(): array
*/
public function getSortedCoordinates(): array
{
asort($this->index);
// Sort only when required
if (!$this->indexSorted) {
asort($this->index);
$this->indexSorted = true;
// Clear unsorted cache
$this->indexKeysCache = null;
$this->indexValuesCache = null;
}

return array_keys($this->index);
// Build or rebuild index keys cache
if ($this->indexKeysCache === null) {
$this->indexKeysCache = array_keys($this->index);
}

return $this->indexKeysCache;
}

/**
Expand All @@ -145,9 +185,19 @@ public function getSortedCoordinates(): array
*/
public function getSortedCoordinatesInt(): array
{
asort($this->index);
if (!$this->indexSorted) {
asort($this->index);
$this->indexSorted = true;
// Clear unsorted cache
$this->indexKeysCache = null;
$this->indexValuesCache = null;
}

return array_values($this->index);
if ($this->indexValuesCache === null) {
$this->indexValuesCache = array_values($this->index);
}

return $this->indexValuesCache;
}

/**
Expand Down Expand Up @@ -300,6 +350,11 @@ public function cloneCellCollection(Worksheet $worksheet): static
}
}

// Clear index sorted flag and index caches
$newCollection->indexSorted = false;
$newCollection->indexKeysCache = null;
$newCollection->indexValuesCache = null;

return $newCollection;
}

Expand Down Expand Up @@ -390,6 +445,11 @@ public function add(string $cellCoordinate, Cell $cell): Cell
/** @var int $row */
$this->index[$cellCoordinate] = (--$row * self::MAX_COLUMN_ID) + Coordinate::columnIndexFromString((string) $column);

// Clear index sorted flag and index caches
$this->indexSorted = false;
$this->indexKeysCache = null;
$this->indexValuesCache = null;

$this->currentCoordinate = $cellCoordinate;
$this->currentCell = $cell;
$this->currentCellIsDirty = true;
Expand Down Expand Up @@ -444,6 +504,11 @@ public function unsetWorksheetCells(): void

$this->index = [];

// Clear index sorted flag and index caches
$this->indexSorted = false;
$this->indexKeysCache = null;
$this->indexValuesCache = null;

// detach ourself from the worksheet, so that it can then delete this object successfully
$this->parent = null;
}
Expand Down
21 changes: 21 additions & 0 deletions src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3104,9 +3104,30 @@ public function rangeToArrayYieldRows(

$index = ($row - 1) * AddressRange::MAX_COLUMN_INT + 1;
$indexPlus = $index + AddressRange::MAX_COLUMN_INT - 1;

// Binary search to quickly approach the correct index
$keyIndex = intdiv($keysCount, 2);
$boundLow = 0;
$boundHigh = $keysCount - 1;
while ($boundLow <= $boundHigh) {
$keyIndex = intdiv($boundLow + $boundHigh, 2);
if ($keys[$keyIndex] < $index) {
$boundLow = $keyIndex + 1;
} elseif ($keys[$keyIndex] > $index) {
$boundHigh = $keyIndex - 1;
} else {
break;
}
}

// Realign to the proper index value
while ($keyIndex > 0 && $keys[$keyIndex] > $index) {
--$keyIndex;
}
while ($keyIndex < $keysCount && $keys[$keyIndex] < $index) {
++$keyIndex;
}

while ($keyIndex < $keysCount && $keys[$keyIndex] <= $indexPlus) {
$key = $keys[$keyIndex];
$thisRow = intdiv($key - 1, AddressRange::MAX_COLUMN_INT) + 1;
Expand Down
Loading