Skip to content

Commit

Permalink
Store Margin using i8 to reduce its size (#5567)
Browse files Browse the repository at this point in the history
Adds `Marginf` to fill the previous niche.

This is all in a pursuit to shrink the sizes of often-used structs, to
improve performance (less cache misses, less memcpy:s, etc).

* On the path towards #4019
  • Loading branch information
emilk authored Jan 2, 2025
1 parent aeea70d commit d58d137
Show file tree
Hide file tree
Showing 12 changed files with 449 additions and 107 deletions.
28 changes: 20 additions & 8 deletions crates/egui/src/containers/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
epaint, layers::ShapeIdx, InnerResponse, Response, Sense, Style, Ui, UiBuilder, UiKind,
UiStackInfo,
};
use epaint::{Color32, Margin, Rect, Rounding, Shadow, Shape, Stroke};
use epaint::{Color32, Margin, Marginf, Rect, Rounding, Shadow, Shape, Stroke};

/// Add a background, frame and/or margin to a rectangular background of a [`Ui`].
///
Expand Down Expand Up @@ -73,6 +73,18 @@ pub struct Frame {
pub stroke: Stroke,
}

#[test]
fn frame_size() {
assert_eq!(
std::mem::size_of::<Frame>(), 44,
"Frame changed size! If it shrank - good! Update this test. If it grew - bad! Try to find a way to avoid it."
);
assert!(
std::mem::size_of::<Frame>() <= 64,
"Frame is getting way too big!"
);
}

impl Frame {
pub fn none() -> Self {
Self::default()
Expand All @@ -81,7 +93,7 @@ impl Frame {
/// For when you want to group a few widgets together within a frame.
pub fn group(style: &Style) -> Self {
Self {
inner_margin: Margin::same(6.0), // same and symmetric looks best in corners when nesting groups
inner_margin: Margin::same(6), // same and symmetric looks best in corners when nesting groups
rounding: style.visuals.widgets.noninteractive.rounding,
stroke: style.visuals.widgets.noninteractive.bg_stroke,
..Default::default()
Expand All @@ -90,15 +102,15 @@ impl Frame {

pub fn side_top_panel(style: &Style) -> Self {
Self {
inner_margin: Margin::symmetric(8.0, 2.0),
inner_margin: Margin::symmetric(8, 2),
fill: style.visuals.panel_fill,
..Default::default()
}
}

pub fn central_panel(style: &Style) -> Self {
Self {
inner_margin: Margin::same(8.0),
inner_margin: Margin::same(8),
fill: style.visuals.panel_fill,
..Default::default()
}
Expand Down Expand Up @@ -143,7 +155,7 @@ impl Frame {
/// and in dark mode this will be very dark.
pub fn canvas(style: &Style) -> Self {
Self {
inner_margin: Margin::same(2.0),
inner_margin: Margin::same(2),
rounding: style.visuals.widgets.noninteractive.rounding,
fill: style.visuals.extreme_bg_color,
stroke: style.visuals.window_stroke(),
Expand Down Expand Up @@ -213,10 +225,10 @@ impl Frame {
}

impl Frame {
/// inner margin plus outer margin.
/// Inner margin plus outer margin.
#[inline]
pub fn total_margin(&self) -> Margin {
self.inner_margin + self.outer_margin
pub fn total_margin(&self) -> Marginf {
Marginf::from(self.inner_margin) + Marginf::from(self.outer_margin)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ impl<'open> Window<'open> {
// Calculate roughly how much larger the window size is compared to the inner rect
let (title_bar_height, title_content_spacing) = if with_title_bar {
let style = ctx.style();
let spacing = window_margin.top + window_margin.bottom;
let spacing = window_margin.sum().y;
let height = ctx.fonts(|f| title.font_height(f, &style)) + spacing;
let half_height = (height / 2.0).round() as _;
window_frame.rounding.ne = window_frame.rounding.ne.clamp(0, half_height);
Expand Down
18 changes: 13 additions & 5 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1238,8 +1238,8 @@ impl Default for Spacing {
fn default() -> Self {
Self {
item_spacing: vec2(8.0, 3.0),
window_margin: Margin::same(6.0),
menu_margin: Margin::same(6.0),
window_margin: Margin::same(6),
menu_margin: Margin::same(6),
button_padding: vec2(4.0, 1.0),
indent: 18.0, // match checkbox/radio-button with `button_padding.x + icon_width + icon_spacing`
interact_size: vec2(40.0, 18.0),
Expand Down Expand Up @@ -2371,9 +2371,17 @@ impl Widget for &mut Margin {

// Apply the checkbox:
if same {
*self = Margin::same((self.left + self.right + self.top + self.bottom) / 4.0);
} else if self.is_same() {
self.right *= 1.00001; // prevent collapsing into sameness
*self =
Margin::from((self.leftf() + self.rightf() + self.topf() + self.bottomf()) / 4.0);
} else {
// Make sure it is not same:
if self.is_same() {
if self.right == i8::MAX {
self.right = i8::MAX - 1;
} else {
self.right += 1;
}
}
}

response
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'t> TextEdit<'t> {
layouter: None,
password: false,
frame: true,
margin: Margin::symmetric(4.0, 2.0),
margin: Margin::symmetric(4, 2),
multiline: true,
interactive: true,
desired_width: None,
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/pan_zoom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl crate::View for PanZoom {
ui.set_clip_rect(transform.inverse() * rect);
egui::Frame::default()
.rounding(egui::Rounding::same(4))
.inner_margin(egui::Margin::same(8.0))
.inner_margin(egui::Margin::same(8))
.stroke(ui.ctx().style().visuals.window_stroke)
.fill(ui.style().visuals.panel_fill)
.show(ui, |ui| {
Expand Down
6 changes: 3 additions & 3 deletions crates/egui_extras/src/datepicker/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ impl<'a> Widget for DatePickerButton<'a> {
let mut pos = button_response.rect.left_bottom();
let width_with_padding = width
+ ui.style().spacing.item_spacing.x
+ ui.style().spacing.window_margin.left
+ ui.style().spacing.window_margin.right;
+ ui.style().spacing.window_margin.leftf()
+ ui.style().spacing.window_margin.rightf();
if pos.x + width_with_padding > ui.clip_rect().right() {
pos.x = button_response.rect.right() - width_with_padding;
}

// Check to make sure the calendar never is displayed out of window
pos.x = pos.x.max(ui.style().spacing.window_margin.left);
pos.x = pos.x.max(ui.style().spacing.window_margin.leftf());

//TODO(elwerene): Better positioning

Expand Down
2 changes: 2 additions & 0 deletions crates/epaint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod brush;
pub mod color;
pub mod image;
mod margin;
mod marginf;
mod mesh;
pub mod mutex;
mod rounding;
Expand All @@ -49,6 +50,7 @@ pub use self::{
color::ColorMode,
image::{ColorImage, FontImage, ImageData, ImageDelta},
margin::Margin,
marginf::Marginf,
mesh::{Mesh, Mesh16, Vertex},
rounding::Rounding,
roundingf::Roundingf,
Expand Down
Loading

0 comments on commit d58d137

Please sign in to comment.