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:
1 parent
d75a1e2129
commit
4cbb242a5d
2 files changed
+14
-4
No files matched your search
+11
-1
@@ -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 {
|
||||
|
||||
@@ -1146,9 +1146,9 @@ impl AxisRemap {
|
||||
true => offset,
|
||||
false => offset / scale.extent,
|
||||
};
|
||||
let from_px = scale.from_px + scale.from_px_span.mul(fraction);
|
||||
let to_rel = scale.to_rel + scale.to_rel_span.mul(fraction);
|
||||
let to_px = scale.to_px + scale.to_px_span.mul(fraction);
|
||||
let from_px = scale.from_px + scale.from_px_span.scaled(fraction);
|
||||
let to_rel = scale.to_rel + scale.to_rel_span.scaled(fraction);
|
||||
let to_px = scale.to_px + scale.to_px_span.scaled(fraction);
|
||||
Len::from_parts(to_rel, scalar.px - from_px + to_px)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user