Optimize applyFromArray by caching existing styles (#1785)

Prevent calling clone and getHashCode when not needed
because these calls are very expensive.

When applying styles to a range of cells can we cache the
styles we encounter along the way so we don't need to look
them up with getHashCode later.
This commit is contained in:
Einar
2021-10-30 17:55:00 +02:00
committed by GitHub
parent 5b4b12f77b
commit 7635b3f91a
3 changed files with 102 additions and 3 deletions
@@ -55,6 +55,29 @@ class StyleTest extends TestCase
self::assertFalse($sheet->getStyle('C3')->getFont()->getItalic());
}
public function testStyleIsReused(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$styleArray = [
'font' => [
'italic' => true,
],
];
$sheet->getStyle('A1')->getFont()->setBold(true);
$sheet->getStyle('A2')->getFont()->setBold(true);
$sheet->getStyle('A3')->getFont()->setBold(true);
$sheet->getStyle('A3')->getFont()->setItalic(true);
$sheet->getStyle('A')->applyFromArray($styleArray);
self::assertCount(4, $spreadsheet->getCellXfCollection());
$spreadsheet->garbageCollect();
self::assertCount(3, $spreadsheet->getCellXfCollection());
}
public function testStyleRow(): void
{
$spreadsheet = new Spreadsheet();