Optimize squaring operations - #430
Open
andrewdalpino wants to merge 3 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes repeated squaring in distance kernels, statistical calculations, and Gaussian model paths by replacing exponentiation with multiplication and cached deltas.
Changes:
- Optimizes Euclidean, cosine, and sparse distance calculations.
- Updates variance and Gaussian likelihood computations.
- Preserves review findings for arithmetic ordering and Minkowski scope.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Summary |
|---|---|
src/Kernels/Distance/SparseCosine.php |
Optimizes sparse vector norm calculations. |
src/Kernels/Distance/SafeEuclidean.php |
Optimizes NaN-safe squared differences. |
src/Kernels/Distance/Euclidean.php |
Optimizes squared distance calculations. Nit (3 votes): Minkowski is described as optimized but remains unchanged. |
src/Kernels/Distance/Cosine.php |
Optimizes vector norm calculations. |
src/Helpers/Stats.php |
Optimizes variance calculation. |
src/Clusterers/GaussianMixture.php |
Optimizes weighted variance and likelihood calculations. Moderate (3 votes): changed arithmetic ordering may alter underflow, overflow, and rounding behavior. |
src/Classifiers/GaussianNB.php |
Optimizes variance merging and likelihood calculations. |
src/AnomalyDetectors/GaussianMLE.php |
Optimizes variance merging and likelihood calculations. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
apphp
requested changes
Aug 23, 2026
|
|
||
| $oldVariance -= $this->epsilon; | ||
|
|
||
| $delta = $n * $oldMean - $n * $mean; |
There was a problem hiding this comment.
Can we replace this with
$n * ($oldMean - $mean)
for better readability and it also 1 operation less? (x2 places)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
($a - $b) ** 2 in the hottest loops — confirmed at src/Kernels/Distance/Euclidean.php:47, plus SafeEuclidean, Cosine, SparseCosine, Minkowski, and the per-feature ** 2 in GMM (GaussianMixture), GaussianMLE, GaussianNB, Stats::variance. PHP ** goes through the generic pow() path. Replace with ($d = $a -$b); $d * $d. This is the single most-invoked path (KNN, ball/KD trees, seeding, GMM, FCM, KMeans, LOF). ~1.5–4× on distance-heavy workloads.