From 0f112db5505d5c19e5aa088d44436cc367bddffe Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 24 Apr 2021 09:54:11 +0200 Subject: [PATCH] Reduce binary size with more inlining and less monomorphization 5%=150kB savings on egui_demo_app wasm --- egui/src/layout.rs | 18 +++++++++++ egui/src/placer.rs | 10 ++++++ egui/src/ui.rs | 81 +++++++++++++++++++++++++++++++++++----------- emath/src/align.rs | 6 ++++ 4 files changed, 97 insertions(+), 18 deletions(-) diff --git a/egui/src/layout.rs b/egui/src/layout.rs index 383b8a83..729231c1 100644 --- a/egui/src/layout.rs +++ b/egui/src/layout.rs @@ -98,6 +98,7 @@ pub enum Direction { } impl Direction { + #[inline(always)] pub fn is_horizontal(self) -> bool { match self { Direction::LeftToRight | Direction::RightToLeft => true, @@ -105,6 +106,7 @@ impl Direction { } } + #[inline(always)] pub fn is_vertical(self) -> bool { match self { Direction::LeftToRight | Direction::RightToLeft => false, @@ -161,6 +163,7 @@ impl Default for Layout { /// ## Constructors impl Layout { + #[inline(always)] pub fn left_to_right() -> Self { Self { main_dir: Direction::LeftToRight, @@ -172,6 +175,7 @@ impl Layout { } } + #[inline(always)] pub fn right_to_left() -> Self { Self { main_dir: Direction::RightToLeft, @@ -183,6 +187,7 @@ impl Layout { } } + #[inline(always)] pub fn top_down(cross_align: Align) -> Self { Self { main_dir: Direction::TopDown, @@ -195,10 +200,12 @@ impl Layout { } /// Top-down layout justifed so that buttons etc fill the full available width. + #[inline(always)] pub fn top_down_justified(cross_align: Align) -> Self { Self::top_down(cross_align).with_cross_justify(true) } + #[inline(always)] pub fn bottom_up(cross_align: Align) -> Self { Self { main_dir: Direction::BottomUp, @@ -210,6 +217,7 @@ impl Layout { } } + #[inline(always)] pub fn from_main_dir_and_cross_align(main_dir: Direction, cross_align: Align) -> Self { Self { main_dir, @@ -221,6 +229,7 @@ impl Layout { } } + #[inline(always)] pub fn centered_and_justified(main_dir: Direction) -> Self { Self { main_dir, @@ -242,10 +251,12 @@ impl Layout { Self::left_to_right().with_cross_align(cross_align) } + #[inline(always)] pub fn with_main_wrap(self, main_wrap: bool) -> Self { Self { main_wrap, ..self } } + #[inline(always)] pub fn with_cross_align(self, cross_align: Align) -> Self { Self { cross_align, @@ -253,6 +264,7 @@ impl Layout { } } + #[inline(always)] pub fn with_cross_justify(self, cross_justify: bool) -> Self { Self { cross_justify, @@ -263,26 +275,32 @@ impl Layout { /// ## Inspectors impl Layout { + #[inline(always)] pub fn main_dir(&self) -> Direction { self.main_dir } + #[inline(always)] pub fn main_wrap(&self) -> bool { self.main_wrap } + #[inline(always)] pub fn cross_align(&self) -> Align { self.cross_align } + #[inline(always)] pub fn cross_justify(&self) -> bool { self.cross_justify } + #[inline(always)] pub fn is_horizontal(&self) -> bool { self.main_dir().is_horizontal() } + #[inline(always)] pub fn is_vertical(&self) -> bool { self.main_dir().is_vertical() } diff --git a/egui/src/placer.rs b/egui/src/placer.rs index f9dd424d..cea1546c 100644 --- a/egui/src/placer.rs +++ b/egui/src/placer.rs @@ -17,6 +17,7 @@ impl Placer { } } + #[inline(always)] pub(crate) fn set_grid(&mut self, grid: grid::GridLayout) { self.grid = Some(grid); } @@ -27,38 +28,47 @@ impl Placer { } } + #[inline(always)] pub(crate) fn grid(&self) -> Option<&grid::GridLayout> { self.grid.as_ref() } + #[inline(always)] pub(crate) fn is_grid(&self) -> bool { self.grid.is_some() } + #[inline(always)] pub(crate) fn layout(&self) -> &Layout { &self.layout } + #[inline(always)] pub(crate) fn prefer_right_to_left(&self) -> bool { self.layout.prefer_right_to_left() } + #[inline(always)] pub(crate) fn min_rect(&self) -> Rect { self.region.min_rect } + #[inline(always)] pub(crate) fn max_rect(&self) -> Rect { self.region.max_rect } + #[inline(always)] pub(crate) fn max_rect_finite(&self) -> Rect { self.region.max_rect_finite() } + #[inline(always)] pub(crate) fn force_set_min_rect(&mut self, min_rect: Rect) { self.region.min_rect = min_rect; } + #[inline(always)] pub(crate) fn cursor(&self) -> Rect { self.region.cursor } diff --git a/egui/src/ui.rs b/egui/src/ui.rs index e91e38d2..e67a18f3 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -689,6 +689,7 @@ impl Ui { /// If the contents overflow, more space will be allocated. /// When finished, the amount of space actually used (`min_rect`) will be allocated. /// So you can request a lot of space and then use less. + #[inline(always)] pub fn allocate_ui( &mut self, desired_size: Vec2, @@ -701,11 +702,21 @@ impl Ui { /// If the contents overflow, more space will be allocated. /// When finished, the amount of space actually used (`min_rect`) will be allocated. /// So you can request a lot of space and then use less. + #[inline(always)] pub fn allocate_ui_with_layout( &mut self, desired_size: Vec2, layout: Layout, add_contents: impl FnOnce(&mut Self) -> R, + ) -> InnerResponse { + self.allocate_ui_with_layout_dyn(desired_size, layout, Box::new(add_contents)) + } + + fn allocate_ui_with_layout_dyn<'c, R>( + &mut self, + desired_size: Vec2, + layout: Layout, + add_contents: Box R + 'c>, ) -> InnerResponse { debug_assert!(desired_size.x >= 0.0 && desired_size.y >= 0.0); let item_spacing = self.spacing().item_spacing; @@ -798,6 +809,7 @@ impl Ui { /// let response = ui.add(egui::Slider::new(&mut my_value, 0..=100)); /// response.on_hover_text("Drag me!"); /// ``` + #[inline(always)] pub fn add(&mut self, widget: impl Widget) -> Response { widget.ui(self) } @@ -835,6 +847,7 @@ impl Ui { /// This will be in addition to the [`Spacing::item_spacing`}. /// /// [`Self::min_rect`] will expand to contain the space. + #[inline(always)] pub fn add_space(&mut self, amount: f32) { self.placer.advance_cursor(amount); } @@ -847,8 +860,9 @@ impl Ui { /// Shortcut for `add(Label::new(text))` /// /// See also [`Label`]. + #[inline(always)] pub fn label(&mut self, label: impl Into