Skip to content

cache the md5 sum in Style/Supervisor.php and subclasses to make getHashCode cheaper #4426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
40 changes: 31 additions & 9 deletions src/PhpSpreadsheet/Style/Alignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public function applyFromArray(array $styleArray): static
$this->setReadOrder($styleArray['readOrder']);
}
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -259,6 +260,7 @@ public function setHorizontal(string $horizontalAlignment): static
} else {
$this->horizontal = $horizontalAlignment;
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -288,6 +290,7 @@ public function setJustifyLastLine(bool $justifyLastLine): static
} else {
$this->justifyLastLine = $justifyLastLine;
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -321,6 +324,7 @@ public function setVertical(string $verticalAlignment): static
} else {
$this->vertical = $verticalAlignment;
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -360,6 +364,7 @@ public function setTextRotation(int $angleInDegrees): static
} else {
throw new PhpSpreadsheetException('Text rotation should be a value between -90 and 90.');
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -392,6 +397,7 @@ public function setWrapText(bool $wrapped): static
} else {
$this->wrapText = $wrapped;
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -424,6 +430,7 @@ public function setShrinkToFit(bool $shrink): static
} else {
$this->shrinkToFit = $shrink;
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -463,6 +470,7 @@ public function setIndent(int $indent): static
} else {
$this->indent = $indent;
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -495,22 +503,17 @@ public function setReadOrder(int $readOrder): static
} else {
$this->readOrder = $readOrder;
}
$this->updateHashBeforeUse();

return $this;
}

/**
* Get hash code.
*
* @return string Hash code
* Update Hash when something changes.
*/
public function getHashCode(): string
protected function updateHash(): void
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}

return md5(
$this->md5Sum = md5(
$this->horizontal
. (($this->justifyLastLine === null) ? 'null' : ($this->justifyLastLine ? 't' : 'f'))
. $this->vertical
Expand All @@ -521,6 +524,25 @@ public function getHashCode(): string
. $this->readOrder
. __CLASS__
);
$this->updateMd5Sum = false;
}

/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode(): string
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}

if ($this->updateMd5Sum) {
$this->updateHash();
}

return $this->md5Sum;
}

protected function exportArray1(): array
Expand Down
26 changes: 21 additions & 5 deletions src/PhpSpreadsheet/Style/Border.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public function applyFromArray(array $styleArray): static
$this->getColor()->applyFromArray($styleArray['color']);
}
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -160,6 +161,7 @@ public function setBorderStyle(bool|string $style): static
} else {
$this->borderStyle = $style;
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -188,10 +190,24 @@ public function setColor(Color $color): static
} else {
$this->color = $color;
}
$this->updateHashBeforeUse();

return $this;
}

/**
* Update Hash when something changes.
*/
protected function updateHash(): void
{
$this->md5Sum = md5(
$this->borderStyle
. $this->color->getHashCode()
. __CLASS__
);
$this->updateMd5Sum = false;
}

/**
* Get hash code.
*
Expand All @@ -203,11 +219,11 @@ public function getHashCode(): string
return $this->getSharedComponent()->getHashCode();
}

return md5(
$this->borderStyle
. $this->color->getHashCode()
. __CLASS__
);
if ($this->updateMd5Sum) {
$this->updateHash();
}

return $this->md5Sum;
}

protected function exportArray1(): array
Expand Down
32 changes: 23 additions & 9 deletions src/PhpSpreadsheet/Style/Borders.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public function applyFromArray(array $styleArray): static
$this->getBottom()->applyFromArray($styleArray['allBorders']);
}
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -330,22 +331,17 @@ public function setDiagonalDirection(int $direction): static
} else {
$this->diagonalDirection = $direction;
}
$this->updateHashBeforeUse();

return $this;
}

/**
* Get hash code.
*
* @return string Hash code
* Update Hash when something changes.
*/
public function getHashCode(): string
protected function updateHash(): void
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashcode();
}

return md5(
$this->md5Sum = md5(
$this->getLeft()->getHashCode()
. $this->getRight()->getHashCode()
. $this->getTop()->getHashCode()
Expand All @@ -356,6 +352,24 @@ public function getHashCode(): string
);
}

/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode(): string
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashcode();
}

if ($this->updateMd5Sum) {
$this->updateHash();
}

return $this->md5Sum;
}

protected function exportArray1(): array
{
$exportedArray = [];
Expand Down
22 changes: 18 additions & 4 deletions src/PhpSpreadsheet/Style/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ public function setARGB(?string $colorValue = self::COLOR_BLACK, bool $nullStrin
} else {
$this->argb = $colorValue;
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -384,6 +385,18 @@ public static function indexedColor(int $colorIndex, bool $background = false, ?
return ($background) ? new self(self::COLOR_WHITE) : new self(self::COLOR_BLACK);
}

/**
* Update Hash when something changes.
*/
protected function updateHash(): void
{
$this->md5Sum = md5(
$this->argb
. __CLASS__
);
$this->updateMd5Sum = false;
}

/**
* Get hash code.
*
Expand All @@ -395,10 +408,11 @@ public function getHashCode(): string
return $this->getSharedComponent()->getHashCode();
}

return md5(
$this->argb
. __CLASS__
);
if ($this->updateMd5Sum) {
$this->updateHash();
}

return $this->md5Sum;
}

protected function exportArray1(): array
Expand Down
31 changes: 21 additions & 10 deletions src/PhpSpreadsheet/Style/Fill.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public function setFillType(string $fillType): static
} else {
$this->fillType = $fillType;
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -206,6 +207,7 @@ public function setRotation(float $angleInDegrees): static
} else {
$this->rotation = $angleInDegrees;
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -235,6 +237,7 @@ public function setStartColor(Color $color): static
} else {
$this->startColor = $color;
}
$this->updateHashBeforeUse();

return $this;
}
Expand Down Expand Up @@ -264,6 +267,7 @@ public function setEndColor(Color $color): static
} else {
$this->endColor = $color;
}
$this->updateHashBeforeUse();

return $this;
}
Expand All @@ -279,6 +283,18 @@ public function getColorsChanged(): bool
return $changed || $this->startColor->getHasChanged() || $this->endColor->getHasChanged();
}

protected function updateHash(): void
{
$this->md5Sum = md5(
$this->getFillType()
. $this->getRotation()
. ($this->getFillType() !== self::FILL_NONE ? $this->getStartColor()->getHashCode() : '')
. ($this->getFillType() !== self::FILL_NONE ? $this->getEndColor()->getHashCode() : '')
. ((string) $this->getColorsChanged())
. __CLASS__
);
}

/**
* Get hash code.
*
Expand All @@ -290,16 +306,11 @@ public function getHashCode(): string
return $this->getSharedComponent()->getHashCode();
}

// Note that we don't care about colours for fill type NONE, but could have duplicate NONEs with
// different hashes if we don't explicitly prevent this
return md5(
$this->getFillType()
. $this->getRotation()
. ($this->getFillType() !== self::FILL_NONE ? $this->getStartColor()->getHashCode() : '')
. ($this->getFillType() !== self::FILL_NONE ? $this->getEndColor()->getHashCode() : '')
. ((string) $this->getColorsChanged())
. __CLASS__
);
if ($this->updateMd5Sum) {
$this->updateHash();
}

return $this->md5Sum;
}

protected function exportArray1(): array
Expand Down
Loading