From 0806e029eb7f63791a6424b1d4b38073653bb14a Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sun, 7 May 2023 07:33:49 +0200 Subject: [PATCH 1/4] Backport 792812ddc157fa153b24e62e731786bf82192d1c --- src/MemoryEfficientLongestCommonSubsequenceCalculator.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/MemoryEfficientLongestCommonSubsequenceCalculator.php b/src/MemoryEfficientLongestCommonSubsequenceCalculator.php index 0b626eaf..489113b6 100644 --- a/src/MemoryEfficientLongestCommonSubsequenceCalculator.php +++ b/src/MemoryEfficientLongestCommonSubsequenceCalculator.php @@ -78,7 +78,12 @@ private function length(array $from, array $to): array if ($from[$i] === $to[$j]) { $current[$j + 1] = $prev[$j] + 1; } else { - $current[$j + 1] = max($current[$j], $prev[$j + 1]); + // don't use max() to avoid function call overhead + if ($current[$j] > $prev[$j + 1]) { + $current[$j + 1] = $current[$j]; + } else { + $current[$j + 1] = $prev[$j + 1]; + } } } } From 7058022e030ca6e9083320e82ea55975c8afb931 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sun, 7 May 2023 07:35:10 +0200 Subject: [PATCH 2/4] Backport b0a1aadeaa7e20988bfa01a1a2467b958ae7231e --- ...ientLongestCommonSubsequenceCalculator.php | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/TimeEfficientLongestCommonSubsequenceCalculator.php b/src/TimeEfficientLongestCommonSubsequenceCalculator.php index fd19cac7..4e8d951d 100644 --- a/src/TimeEfficientLongestCommonSubsequenceCalculator.php +++ b/src/TimeEfficientLongestCommonSubsequenceCalculator.php @@ -37,12 +37,24 @@ public function calculate(array $from, array $to): array for ($i = 1; $i <= $fromLength; ++$i) { for ($j = 1; $j <= $toLength; ++$j) { - $o = ($j * $width) + $i; - $matrix[$o] = max( - $matrix[$o - 1], - $matrix[$o - $width], - $from[$i - 1] === $to[$j - 1] ? $matrix[$o - $width - 1] + 1 : 0 - ); + $o = ($j * $width) + $i; + + // don't use max() to avoid function call overhead + $firstOrLast = $from[$i - 1] === $to[$j - 1] ? $matrix[$o - $width - 1] + 1 : 0; + + if ($matrix[$o - 1] > $matrix[$o - $width]) { + if ($firstOrLast > $matrix[$o - 1]) { + $matrix[$o] = $firstOrLast; + } else { + $matrix[$o] = $matrix[$o - 1]; + } + } else { + if ($firstOrLast > $matrix[$o - $width]) { + $matrix[$o] = $firstOrLast; + } else { + $matrix[$o] = $matrix[$o - $width]; + } + } } } From 74be17022044ebaaecfdf0c5cd504fc9cd5a7131 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sun, 7 May 2023 07:35:17 +0200 Subject: [PATCH 3/4] Prepare release --- ChangeLog.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ChangeLog.md b/ChangeLog.md index 9bdcc5b6..a6ccfad7 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -2,6 +2,13 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles. +## [4.0.5] - 2023-05-07 + +### Changed + +* [#118](https://github.com/sebastianbergmann/diff/pull/118): Improve performance of `MemoryEfficientLongestCommonSubsequenceCalculator` +* [#119](https://github.com/sebastianbergmann/diff/pull/119): Improve performance of `TimeEfficientLongestCommonSubsequenceCalculator` + ## [4.0.4] - 2020-10-26 ### Fixed @@ -76,6 +83,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt * This component is no longer supported on PHP 5.6 +[4.0.5]: https://github.com/sebastianbergmann/diff/compare/4.0.4...4.0.5 [4.0.4]: https://github.com/sebastianbergmann/diff/compare/4.0.3...4.0.4 [4.0.3]: https://github.com/sebastianbergmann/diff/compare/4.0.2...4.0.3 [4.0.2]: https://github.com/sebastianbergmann/diff/compare/4.0.1...4.0.2 From ba01945089c3a293b01ba9badc29ad55b106b0bc Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sat, 2 Mar 2024 07:30:58 +0100 Subject: [PATCH 4/4] Do not use implicitly nullable parameters and prepare release --- ChangeLog.md | 7 +++++++ src/Differ.php | 4 ++-- src/Exception/ConfigurationException.php | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index a6ccfad7..e62d8f95 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -2,6 +2,12 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles. +## [4.0.6] - 2024-03-02 + +### Changed + +* Do not use implicitly nullable parameters + ## [4.0.5] - 2023-05-07 ### Changed @@ -83,6 +89,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt * This component is no longer supported on PHP 5.6 +[4.0.6]: https://github.com/sebastianbergmann/diff/compare/4.0.5...4.0.6 [4.0.5]: https://github.com/sebastianbergmann/diff/compare/4.0.4...4.0.5 [4.0.4]: https://github.com/sebastianbergmann/diff/compare/4.0.3...4.0.4 [4.0.3]: https://github.com/sebastianbergmann/diff/compare/4.0.2...4.0.3 diff --git a/src/Differ.php b/src/Differ.php index 5a4d9d10..98c7a9b2 100644 --- a/src/Differ.php +++ b/src/Differ.php @@ -82,7 +82,7 @@ public function __construct($outputBuilder = null) * @param array|string $from * @param array|string $to */ - public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null): string + public function diff($from, $to, ?LongestCommonSubsequenceCalculator $lcs = null): string { $diff = $this->diffToArray( $this->normalizeDiffInput($from), @@ -108,7 +108,7 @@ public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null) * @param array|string $to * @param LongestCommonSubsequenceCalculator $lcs */ - public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs = null): array + public function diffToArray($from, $to, ?LongestCommonSubsequenceCalculator $lcs = null): array { if (is_string($from)) { $from = $this->splitStringByLines($from); diff --git a/src/Exception/ConfigurationException.php b/src/Exception/ConfigurationException.php index b767b219..8847a2e5 100644 --- a/src/Exception/ConfigurationException.php +++ b/src/Exception/ConfigurationException.php @@ -22,7 +22,7 @@ public function __construct( string $expected, $value, int $code = 0, - Exception $previous = null + ?Exception $previous = null ) { parent::__construct( sprintf(