From 044496f65201355ead07dd166f0c74059fd1fe6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Mon, 12 Jan 2026 23:38:03 +0100 Subject: [PATCH] Sort using more deterministic algorithm `hypot` while useful for most of the cases, may break sorting assumptions like deterministic comparison. As seen in: https://github.com/pop-os/cosmic-launcher/issues/352 Add and use a new method that trades the performance for more determinism. --- core/src/point.rs | 12 ++++++++++++ graphics/src/damage.rs | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/core/src/point.rs b/core/src/point.rs index 55872911..e2b51e04 100644 --- a/core/src/point.rs +++ b/core/src/point.rs @@ -34,6 +34,18 @@ impl Point { a.hypot(b) } + + /// Computes the distance to another [`Point`], using a slower but more deterministic algorithm. + /// Useful for sorting algorithms. + pub fn distance_slow(&self, to: Self) -> T + where + T: Float, + { + let a = self.x - to.x; + let b = self.y - to.y; + + (a * a + b * b).sqrt() + } } impl From<[T; 2]> for Point diff --git a/graphics/src/damage.rs b/graphics/src/damage.rs index 8aa42798..6e2d1ab8 100644 --- a/graphics/src/damage.rs +++ b/graphics/src/damage.rs @@ -49,8 +49,8 @@ pub fn group(mut damage: Vec, bounds: Rectangle) -> Vec { damage.sort_by(|a, b| { a.center() - .distance(Point::ORIGIN) - .total_cmp(&b.center().distance(Point::ORIGIN)) + .distance_slow(Point::ORIGIN) + .total_cmp(&b.center().distance_slow(Point::ORIGIN)) }); let mut output = Vec::new();