Skip to content

Optimize squaring operations - #430

Open
andrewdalpino wants to merge 3 commits into
masterfrom
optimize-square
Open

Optimize squaring operations#430
andrewdalpino wants to merge 3 commits into
masterfrom
optimize-square

Conversation

@andrewdalpino

Copy link
Copy Markdown
Member

($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.

Benchmark Before After Δ
Euclidean 5.981ms ±1.8% 3.395ms ±5.2%¹ −43% (1.76×)
SafeEuclidean 8.979ms ±3.0% 7.046ms ±9.9%¹ −22% (1.27×)
Cosine dense 11.134ms ±0.7% 5.572ms ±1.1% −50% (2.00×)
Cosine sparse / very sparse 5.299 / 4.837ms 3.093 / 3.009ms −42% / −38%
SparseCosine dense / sparse / very sparse 12.737 / 5.150 / 3.026ms 7.437 / 3.956 / 2.675ms −42% / −23% / −12%
GaussianMLE train+predict 0.019s ±0.6% 0.015s ±1.9% −21%
GaussianNB train+predict 0.026s ±1.0% 0.021s ±0.7% −19%
GaussianMixture train+predict 0.116–0.147s ±28–33% 0.117–0.140s ±23–34% inconclusive²

@andrewdalpino
andrewdalpino requested review from a team and a lite review from Copilot August 22, 2026 16:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/Clusterers/GaussianMixture.php Outdated
Comment thread src/Kernels/Distance/Euclidean.php
andrewdalpino and others added 2 commits August 22, 2026 11:58
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@andrewdalpino andrewdalpino added the optimization Make something perform faster label Aug 23, 2026

$oldVariance -= $this->epsilon;

$delta = $n * $oldMean - $n * $mean;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we replace this with
$n * ($oldMean - $mean)
for better readability and it also 1 operation less? (x2 places)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimization Make something perform faster

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants