From 24d4f8ca1808138738b84bbf7cb6d006796ffa1a Mon Sep 17 00:00:00 2001 From: tidely <43219534+tidely@users.noreply.github.com> Date: Thu, 10 Apr 2025 23:39:50 +0300 Subject: [PATCH] gpui: Optimize coalesce float sign checking (#28072) Optimize away a multiplication during in the `coalesce` function. Our goal is to check whether the sign of two floats is the same. Instead of multiplying each `.signum()` and checking that the result is positive, we can simply check that the signum's are the same. This removes a float multiplication. ```rust a.signum() * b.signum() >= 0.0 ``` turns into ```rust a.signum() == b.signum() ``` Release Notes: - Fix documentation for `Pixels::signum` --- crates/gpui/src/geometry.rs | 1 - crates/gpui/src/interactive.rs | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/gpui/src/geometry.rs b/crates/gpui/src/geometry.rs index bb750e80f8f98e3fadcbc822673663a8d7a2ce7f..f2d11029156f37d577b71d644a89cec95eda758d 100644 --- a/crates/gpui/src/geometry.rs +++ b/crates/gpui/src/geometry.rs @@ -2622,7 +2622,6 @@ impl Pixels { /// Returns: /// * `1.0` if the value is positive /// * `-1.0` if the value is negative - /// * `0.0` if the value is zero pub fn signum(&self) -> f32 { self.0.signum() } diff --git a/crates/gpui/src/interactive.rs b/crates/gpui/src/interactive.rs index c6eab26186a079f781d4d91d766d1130c8c4e86c..02d0aaac38ce6824ea9263d7d7f333246db2cb60 100644 --- a/crates/gpui/src/interactive.rs +++ b/crates/gpui/src/interactive.rs @@ -311,13 +311,13 @@ impl ScrollDelta { pub fn coalesce(self, other: ScrollDelta) -> ScrollDelta { match (self, other) { (ScrollDelta::Pixels(a), ScrollDelta::Pixels(b)) => { - let x = if a.x.signum() * b.x.signum() >= 0. { + let x = if a.x.signum() == b.x.signum() { a.x + b.x } else { b.x }; - let y = if a.y.signum() * b.y.signum() >= 0. { + let y = if a.y.signum() == b.y.signum() { a.y + b.y } else { b.y @@ -327,13 +327,13 @@ impl ScrollDelta { } (ScrollDelta::Lines(a), ScrollDelta::Lines(b)) => { - let x = if a.x.signum() * b.x.signum() >= 0. { + let x = if a.x.signum() == b.x.signum() { a.x + b.x } else { b.x }; - let y = if a.y.signum() * b.y.signum() >= 0. { + let y = if a.y.signum() == b.y.signum() { a.y + b.y } else { b.y