1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Optimize: add #[inline(always)] to various low-level things

saves up to 20% (text tesselation), and at least 5% overall
This commit is contained in:
Emil Ernerfeldt
2021-03-28 23:16:19 +02:00
parent ccc501f302
commit 46425f1e38
10 changed files with 144 additions and 7 deletions

View File

@@ -92,11 +92,13 @@ pub trait One {
fn one() -> Self;
}
impl One for f32 {
#[inline(always)]
fn one() -> Self {
1.0
}
}
impl One for f64 {
#[inline(always)]
fn one() -> Self {
1.0
}
@@ -121,6 +123,7 @@ impl Real for f64 {}
// ----------------------------------------------------------------------------
/// Linear interpolation.
#[inline(always)]
pub fn lerp<R, T>(range: RangeInclusive<R>, t: T) -> R
where
T: Real + Mul<R, Output = R>,
@@ -309,9 +312,11 @@ pub trait NumExt {
macro_rules! impl_num_ext {
($t: ty) => {
impl NumExt for $t {
#[inline(always)]
fn at_least(self, lower_limit: Self) -> Self {
self.max(lower_limit)
}
#[inline(always)]
fn at_most(self, upper_limit: Self) -> Self {
self.min(upper_limit)
}

View File

@@ -20,9 +20,13 @@ macro_rules! impl_numeric_float {
const INTEGRAL: bool = false;
const MIN: Self = std::$t::MIN;
const MAX: Self = std::$t::MAX;
#[inline(always)]
fn to_f64(self) -> f64 {
self as f64
}
#[inline(always)]
fn from_f64(num: f64) -> Self {
num as Self
}
@@ -36,9 +40,13 @@ macro_rules! impl_numeric_integer {
const INTEGRAL: bool = true;
const MIN: Self = std::$t::MIN;
const MAX: Self = std::$t::MAX;
#[inline(always)]
fn to_f64(self) -> f64 {
self as f64
}
#[inline(always)]
fn from_f64(num: f64) -> Self {
num as Self
}

View File

@@ -26,24 +26,28 @@ pub const fn pos2(x: f32, y: f32) -> Pos2 {
// Compatibility and convenience conversions to and from [f32; 2]:
impl From<[f32; 2]> for Pos2 {
#[inline(always)]
fn from(v: [f32; 2]) -> Self {
Self { x: v[0], y: v[1] }
}
}
impl From<&[f32; 2]> for Pos2 {
#[inline(always)]
fn from(v: &[f32; 2]) -> Self {
Self { x: v[0], y: v[1] }
}
}
impl From<Pos2> for [f32; 2] {
#[inline(always)]
fn from(v: Pos2) -> Self {
[v.x, v.y]
}
}
impl From<&Pos2> for [f32; 2] {
#[inline(always)]
fn from(v: &Pos2) -> Self {
[v.x, v.y]
}
@@ -53,24 +57,28 @@ impl From<&Pos2> for [f32; 2] {
// Compatibility and convenience conversions to and from (f32, f32):
impl From<(f32, f32)> for Pos2 {
#[inline(always)]
fn from(v: (f32, f32)) -> Self {
Self { x: v.0, y: v.1 }
}
}
impl From<&(f32, f32)> for Pos2 {
#[inline(always)]
fn from(v: &(f32, f32)) -> Self {
Self { x: v.0, y: v.1 }
}
}
impl From<Pos2> for (f32, f32) {
#[inline(always)]
fn from(v: Pos2) -> Self {
(v.x, v.y)
}
}
impl From<&Pos2> for (f32, f32) {
#[inline(always)]
fn from(v: &Pos2) -> Self {
(v.x, v.y)
}
@@ -95,6 +103,7 @@ impl Pos2 {
/// The vector from origin to this position.
/// `p.to_vec2()` is equivalent to `p - Pos2::default()`.
#[inline(always)]
pub fn to_vec2(self) -> Vec2 {
Vec2 {
x: self.x,
@@ -153,6 +162,7 @@ impl Pos2 {
impl std::ops::Index<usize> for Pos2 {
type Output = f32;
fn index(&self, index: usize) -> &f32 {
match index {
0 => &self.x,
@@ -175,6 +185,7 @@ impl std::ops::IndexMut<usize> for Pos2 {
impl Eq for Pos2 {}
impl AddAssign<Vec2> for Pos2 {
#[inline(always)]
fn add_assign(&mut self, rhs: Vec2) {
*self = Pos2 {
x: self.x + rhs.x,
@@ -184,6 +195,7 @@ impl AddAssign<Vec2> for Pos2 {
}
impl SubAssign<Vec2> for Pos2 {
#[inline(always)]
fn sub_assign(&mut self, rhs: Vec2) {
*self = Pos2 {
x: self.x - rhs.x,
@@ -194,6 +206,8 @@ impl SubAssign<Vec2> for Pos2 {
impl Add<Vec2> for Pos2 {
type Output = Pos2;
#[inline(always)]
fn add(self, rhs: Vec2) -> Pos2 {
Pos2 {
x: self.x + rhs.x,
@@ -204,6 +218,8 @@ impl Add<Vec2> for Pos2 {
impl Sub for Pos2 {
type Output = Vec2;
#[inline(always)]
fn sub(self, rhs: Pos2) -> Vec2 {
Vec2 {
x: self.x - rhs.x,
@@ -214,6 +230,8 @@ impl Sub for Pos2 {
impl Sub<Vec2> for Pos2 {
type Output = Pos2;
#[inline(always)]
fn sub(self, rhs: Vec2) -> Pos2 {
Pos2 {
x: self.x - rhs.x,

View File

@@ -74,6 +74,7 @@ impl Rect {
Self::NAN
}
#[inline(always)]
pub const fn from_min_max(min: Pos2, max: Pos2) -> Self {
Rect { min, max }
}
@@ -196,11 +197,9 @@ impl Rect {
}
#[must_use]
#[inline(always)]
pub fn contains(&self, p: Pos2) -> bool {
self.min.x <= p.x
&& p.x <= self.min.x + self.size().x
&& self.min.y <= p.y
&& p.y <= self.min.y + self.size().y
self.min.x <= p.x && p.x <= self.max.x && self.min.y <= p.y && p.y <= self.max.y
}
/// Return the given points clamped to be inside the rectangle
@@ -234,18 +233,25 @@ impl Rect {
}
}
#[inline(always)]
pub fn center(&self) -> Pos2 {
Pos2 {
x: self.min.x + self.size().x / 2.0,
y: self.min.y + self.size().y / 2.0,
x: (self.min.x + self.max.x) / 2.0,
y: (self.min.y + self.max.y) / 2.0,
}
}
#[inline(always)]
pub fn size(&self) -> Vec2 {
self.max - self.min
}
#[inline(always)]
pub fn width(&self) -> f32 {
self.max.x - self.min.x
}
#[inline(always)]
pub fn height(&self) -> f32 {
self.max.y - self.min.y
}
@@ -292,6 +298,7 @@ impl Rect {
}
/// `max.x < min.x` or `max.y < min.y`.
#[inline(always)]
pub fn is_negative(&self) -> bool {
self.max.x < self.min.x || self.max.y < self.min.y
}
@@ -314,79 +321,106 @@ impl Rect {
/// ## Convenience functions (assumes origin is towards left top):
impl Rect {
/// `min.x`
/// `min.x
#[inline(always)]
pub fn left(&self) -> f32 {
self.min.x
}
/// `min.x`
#[inline(always)]
pub fn left_mut(&mut self) -> &mut f32 {
&mut self.min.x
}
/// `min.x`
#[inline(always)]
pub fn set_left(&mut self, x: f32) {
self.min.x = x;
}
/// `max.x`
#[inline(always)]
pub fn right(&self) -> f32 {
self.max.x
}
/// `max.x`
#[inline(always)]
pub fn right_mut(&mut self) -> &mut f32 {
&mut self.max.x
}
/// `max.x`
#[inline(always)]
pub fn set_right(&mut self, x: f32) {
self.max.x = x;
}
/// `min.y`
#[inline(always)]
pub fn top(&self) -> f32 {
self.min.y
}
/// `min.y`
#[inline(always)]
pub fn top_mut(&mut self) -> &mut f32 {
&mut self.min.y
}
/// `min.y`
#[inline(always)]
pub fn set_top(&mut self, y: f32) {
self.min.y = y;
}
/// `max.y`
#[inline(always)]
pub fn bottom(&self) -> f32 {
self.max.y
}
/// `max.y`
#[inline(always)]
pub fn bottom_mut(&mut self) -> &mut f32 {
&mut self.max.y
}
/// `max.y`
#[inline(always)]
pub fn set_bottom(&mut self, y: f32) {
self.max.y = y;
}
#[inline(always)]
pub fn left_top(&self) -> Pos2 {
pos2(self.left(), self.top())
}
#[inline(always)]
pub fn center_top(&self) -> Pos2 {
pos2(self.center().x, self.top())
}
#[inline(always)]
pub fn right_top(&self) -> Pos2 {
pos2(self.right(), self.top())
}
#[inline(always)]
pub fn left_center(&self) -> Pos2 {
pos2(self.left(), self.center().y)
}
#[inline(always)]
pub fn right_center(&self) -> Pos2 {
pos2(self.right(), self.center().y)
}
#[inline(always)]
pub fn left_bottom(&self) -> Pos2 {
pos2(self.left(), self.bottom())
}
#[inline(always)]
pub fn center_bottom(&self) -> Pos2 {
pos2(self.center().x, self.bottom())
}
#[inline(always)]
pub fn right_bottom(&self) -> Pos2 {
pos2(self.right(), self.bottom())
}

View File

@@ -23,24 +23,28 @@ pub const fn vec2(x: f32, y: f32) -> Vec2 {
// Compatibility and convenience conversions to and from [f32; 2]:
impl From<[f32; 2]> for Vec2 {
#[inline(always)]
fn from(v: [f32; 2]) -> Self {
Self { x: v[0], y: v[1] }
}
}
impl From<&[f32; 2]> for Vec2 {
#[inline(always)]
fn from(v: &[f32; 2]) -> Self {
Self { x: v[0], y: v[1] }
}
}
impl From<Vec2> for [f32; 2] {
#[inline(always)]
fn from(v: Vec2) -> Self {
[v.x, v.y]
}
}
impl From<&Vec2> for [f32; 2] {
#[inline(always)]
fn from(v: &Vec2) -> Self {
[v.x, v.y]
}
@@ -50,24 +54,28 @@ impl From<&Vec2> for [f32; 2] {
// Compatibility and convenience conversions to and from (f32, f32):
impl From<(f32, f32)> for Vec2 {
#[inline(always)]
fn from(v: (f32, f32)) -> Self {
Self { x: v.0, y: v.1 }
}
}
impl From<&(f32, f32)> for Vec2 {
#[inline(always)]
fn from(v: &(f32, f32)) -> Self {
Self { x: v.0, y: v.1 }
}
}
impl From<Vec2> for (f32, f32) {
#[inline(always)]
fn from(v: Vec2) -> Self {
(v.x, v.y)
}
}
impl From<&Vec2> for (f32, f32) {
#[inline(always)]
fn from(v: &Vec2) -> Self {
(v.x, v.y)
}
@@ -92,16 +100,20 @@ impl Vec2 {
Self::INFINITY
}
#[inline(always)]
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
/// Set both `x` and `y` to the same value.
#[inline(always)]
pub const fn splat(v: f32) -> Self {
Self { x: v, y: v }
}
/// Safe normalize: returns zero if input is zero.
#[must_use]
#[inline(always)]
pub fn normalized(self) -> Self {
let len = self.length();
if len <= 0.0 {
@@ -118,10 +130,12 @@ impl Vec2 {
vec2(self.y, -self.x)
}
#[inline(always)]
pub fn length(self) -> f32 {
self.x.hypot(self.y)
}
#[inline(always)]
pub fn length_sq(self) -> f32 {
self.x * self.x + self.y * self.y
}
@@ -129,6 +143,7 @@ impl Vec2 {
/// Create a unit vector with the given angle (in radians).
/// * An angle of zero gives the unit X axis.
/// * An angle of 𝞃/4 = 90° gives the unit Y axis.
#[inline(always)]
pub fn angled(angle: f32) -> Self {
vec2(angle.cos(), angle.sin())
}
@@ -191,6 +206,7 @@ impl Vec2 {
impl std::ops::Index<usize> for Vec2 {
type Output = f32;
fn index(&self, index: usize) -> &f32 {
match index {
0 => &self.x,
@@ -215,12 +231,14 @@ impl Eq for Vec2 {}
impl Neg for Vec2 {
type Output = Vec2;
#[inline(always)]
fn neg(self) -> Vec2 {
vec2(-self.x, -self.y)
}
}
impl AddAssign for Vec2 {
#[inline(always)]
fn add_assign(&mut self, rhs: Vec2) {
*self = Vec2 {
x: self.x + rhs.x,
@@ -230,6 +248,7 @@ impl AddAssign for Vec2 {
}
impl SubAssign for Vec2 {
#[inline(always)]
fn sub_assign(&mut self, rhs: Vec2) {
*self = Vec2 {
x: self.x - rhs.x,
@@ -240,6 +259,8 @@ impl SubAssign for Vec2 {
impl Add for Vec2 {
type Output = Vec2;
#[inline(always)]
fn add(self, rhs: Vec2) -> Vec2 {
Vec2 {
x: self.x + rhs.x,
@@ -250,6 +271,8 @@ impl Add for Vec2 {
impl Sub for Vec2 {
type Output = Vec2;
#[inline(always)]
fn sub(self, rhs: Vec2) -> Vec2 {
Vec2 {
x: self.x - rhs.x,
@@ -261,6 +284,8 @@ impl Sub for Vec2 {
/// Element-wise multiplication
impl Mul<Vec2> for Vec2 {
type Output = Vec2;
#[inline(always)]
fn mul(self, vec: Vec2) -> Vec2 {
Vec2 {
x: self.x * vec.x,
@@ -272,6 +297,8 @@ impl Mul<Vec2> for Vec2 {
/// Element-wise division
impl Div<Vec2> for Vec2 {
type Output = Vec2;
#[inline(always)]
fn div(self, rhs: Vec2) -> Vec2 {
Vec2 {
x: self.x / rhs.x,
@@ -281,6 +308,7 @@ impl Div<Vec2> for Vec2 {
}
impl MulAssign<f32> for Vec2 {
#[inline(always)]
fn mul_assign(&mut self, rhs: f32) {
self.x *= rhs;
self.y *= rhs;
@@ -289,6 +317,8 @@ impl MulAssign<f32> for Vec2 {
impl Mul<f32> for Vec2 {
type Output = Vec2;
#[inline(always)]
fn mul(self, factor: f32) -> Vec2 {
Vec2 {
x: self.x * factor,
@@ -299,6 +329,8 @@ impl Mul<f32> for Vec2 {
impl Mul<Vec2> for f32 {
type Output = Vec2;
#[inline(always)]
fn mul(self, vec: Vec2) -> Vec2 {
Vec2 {
x: self * vec.x,
@@ -309,6 +341,8 @@ impl Mul<Vec2> for f32 {
impl Div<f32> for Vec2 {
type Output = Vec2;
#[inline(always)]
fn div(self, factor: f32) -> Vec2 {
Vec2 {
x: self.x / factor,