Do not multiply by a part of nothing

`lerp` is `a + (b - a) * f`, and `b - a` is nothing often enough to be worth
asking: a box with the same pixels at both ends of an axis, a span with no
fraction of one, a part of a subtree whose box did not move on that axis.
`Fixed::scaled` is `mul` that answers a zero receiver without widening to
`i64`, rounding and narrowing back, and `lerp` uses it -- so every lerp in
layout gets it rather than the two places that were about to grow their own
comparison.

`many` over 500 frames: 1,705,786,553 instructions to 1,657,571,216, and
638.9M cycles against 657.9M, averaged over four runs each.

Checked: fmt, clippy, 105 tests, all five shrinker cases at 300 seeds, and
`tabs`, `text`, `random`, `minimal` and `view` byte-identical at 1920x1200.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
iris-aiandClaude Opus 5 committed 2026-09-16 14:01:52 -04:00
1 parent d75a1e2129
commit 4cbb242a5d
2 files changed
+14 -4

No files matched your search

+11 -1
View File
@@ -138,6 +138,16 @@ impl<const SHIFT: u32> Fixed<SHIFT> {
Self(narrow(shift_round(self.0 as i64 * by.0 as i64, BY)))
}
/// A part of a span that is often nothing: no part of nothing is
/// nothing, for the cost of a comparison rather than a widening
/// multiply and a rounding.
pub const fn scaled<const BY: u32>(self, by: Fixed<BY>) -> Self {
match self.0 == 0 {
true => self,
false => self.mul(by),
}
}
/// Repeated a whole number of times, which no grid rounds.
pub const fn mul_int(self, by: i32) -> Self {
Self(narrow(self.0 as i64 * by as i64))
@@ -179,7 +189,7 @@ impl<const SHIFT: u32> Fixed<SHIFT> {
/// `from` and `to` a fraction of the way apart, the fraction being the
/// receiver -- the argument order [`crate::util::LerpUtil`] already uses.
pub const fn lerp<const OF: u32>(self, from: Fixed<OF>, to: Fixed<OF>) -> Fixed<OF> {
from.add(to.sub(from).mul(self))
from.add(to.sub(from).scaled(self))
}
pub const fn min(self, other: Self) -> Self {