1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 05:10: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

@@ -100,19 +100,27 @@ impl Color32 {
Self([l, l, l, 0])
}
#[inline(always)]
pub fn is_opaque(&self) -> bool {
self.a() == 255
}
#[inline(always)]
pub fn r(&self) -> u8 {
self.0[0]
}
#[inline(always)]
pub fn g(&self) -> u8 {
self.0[1]
}
#[inline(always)]
pub fn b(&self) -> u8 {
self.0[2]
}
#[inline(always)]
pub fn a(&self) -> u8 {
self.0[3]
}
@@ -129,11 +137,13 @@ impl Color32 {
}
/// Premultiplied RGBA
#[inline(always)]
pub fn to_array(&self) -> [u8; 4] {
[self.r(), self.g(), self.b(), self.a()]
}
/// Premultiplied RGBA
#[inline(always)]
pub fn to_tuple(&self) -> (u8, u8, u8, u8) {
(self.r(), self.g(), self.b(), self.a())
}
@@ -212,6 +222,7 @@ impl Rgba {
}
/// Multiply with e.g. 0.5 to make us half transparent
#[inline(always)]
pub fn multiply(self, alpha: f32) -> Self {
Self([
alpha * self[0],
@@ -221,15 +232,22 @@ impl Rgba {
])
}
#[inline(always)]
pub fn r(&self) -> f32 {
self.0[0]
}
#[inline(always)]
pub fn g(&self) -> f32 {
self.0[1]
}
#[inline(always)]
pub fn b(&self) -> f32 {
self.0[2]
}
#[inline(always)]
pub fn a(&self) -> f32 {
self.0[3]
}
@@ -258,6 +276,8 @@ impl Rgba {
impl std::ops::Add for Rgba {
type Output = Rgba;
#[inline(always)]
fn add(self, rhs: Rgba) -> Rgba {
Rgba([
self[0] + rhs[0],
@@ -270,6 +290,8 @@ impl std::ops::Add for Rgba {
impl std::ops::Mul<Rgba> for Rgba {
type Output = Rgba;
#[inline(always)]
fn mul(self, other: Rgba) -> Rgba {
Rgba([
self[0] * other[0],
@@ -282,6 +304,8 @@ impl std::ops::Mul<Rgba> for Rgba {
impl std::ops::Mul<f32> for Rgba {
type Output = Rgba;
#[inline(always)]
fn mul(self, factor: f32) -> Rgba {
Rgba([
self[0] * factor,
@@ -294,6 +318,8 @@ impl std::ops::Mul<f32> for Rgba {
impl std::ops::Mul<Rgba> for f32 {
type Output = Rgba;
#[inline(always)]
fn mul(self, rgba: Rgba) -> Rgba {
Rgba([
self * rgba[0],

View File

@@ -92,6 +92,7 @@ impl Mesh {
}
}
#[inline(always)]
pub fn colored_vertex(&mut self, pos: Pos2, color: Color32) {
debug_assert!(self.texture_id == TextureId::Egui);
self.vertices.push(Vertex {
@@ -102,6 +103,7 @@ impl Mesh {
}
/// Add a triangle.
#[inline(always)]
pub fn add_triangle(&mut self, a: u32, b: u32, c: u32) {
self.indices.push(a);
self.indices.push(b);
@@ -110,12 +112,14 @@ impl Mesh {
/// Make room for this many additional triangles (will reserve 3x as many indices).
/// See also `reserve_vertices`.
#[inline(always)]
pub fn reserve_triangles(&mut self, additional_triangles: usize) {
self.indices.reserve(3 * additional_triangles);
}
/// Make room for this many additional vertices.
/// See also `reserve_triangles`.
#[inline(always)]
pub fn reserve_vertices(&mut self, additional: usize) {
self.vertices.reserve(additional);
}
@@ -151,6 +155,7 @@ impl Mesh {
}
/// Uniformly colored rectangle.
#[inline(always)]
pub fn add_colored_rect(&mut self, rect: Rect, color: Color32) {
debug_assert!(self.texture_id == TextureId::Egui);
self.add_rect_with_uv(rect, [WHITE_UV, WHITE_UV].into(), color)

View File

@@ -34,10 +34,12 @@ pub struct PathPoint {
struct Path(Vec<PathPoint>);
impl Path {
#[inline(always)]
pub fn clear(&mut self) {
self.0.clear();
}
#[inline(always)]
pub fn reserve(&mut self, additional: usize) {
self.0.reserve(additional)
}

View File

@@ -138,10 +138,12 @@ impl FontImpl {
}
/// Height of one row of text. In points
#[inline(always)]
pub fn row_height(&self) -> f32 {
self.height_in_points
}
#[inline(always)]
pub fn pixels_per_point(&self) -> f32 {
self.pixels_per_point
}
@@ -202,11 +204,13 @@ impl Font {
slf
}
#[inline]
pub fn round_to_pixel(&self, point: f32) -> f32 {
(point * self.pixels_per_point).round() / self.pixels_per_point
}
/// Height of one row of text. In points
#[inline(always)]
pub fn row_height(&self) -> f32 {
self.row_height
}