Skip to content

Put font data into Arc to reduce memory consumption - #5276

Merged
emilk merged 1 commit into
emilk:masterfrom
StarStarJ:pr_reduce_memory_usage_font_definitions
Nov 1, 2024
Merged

emilk merged 1 commit into
emilk:masterfrom
StarStarJ:pr_reduce_memory_usage_font_definitions

Conversation

@StarStarJ

@StarStarJ StarStarJ commented Oct 16, 2024

Copy link
Copy Markdown
Contributor

egui never accesses the FontDefinitions' member fields mutably, except in fonts_tweak_ui where it cloned the FontDefinitions object anyway.

This patch reduces system memory consumption for shared font definitions.
And also removes some overhead from copying (e.g. for the per pixel_per_points font atlas)

Also it allows to keep a copy of the font definitions outside of egui.

In my App that uses international fonts:
Before:
image

New:
image

Note: If Arc is not wanted, then it could ofc be abstracted away.

I know this is quite a breaking change API wise, but would like to hear your opinion.

@StarStarJ

Copy link
Copy Markdown
Contributor Author

E.g. it keeps a copy in ContextImpl and copies it into the fonts per pixels_per_point:

self.font_definitions.clone(),

from:

let fonts = self
.fonts
.entry(pixels_per_point.into())
.or_insert_with(|| {
#[cfg(feature = "log")]
log::trace!("Creating new Fonts for pixels_per_point={pixels_per_point}");
is_new = true;
crate::profile_scope!("Fonts::new");
Fonts::new(
pixels_per_point,
max_texture_side,
self.font_definitions.clone(),
)
});

@github-actions

Copy link
Copy Markdown

Preview available at https://egui-pr-preview.github.io/pr/5276-pr_reduce_memory_usage_font_definitions
Note that it might take a couple seconds for the update to show up after the preview_build workflow has completed.

@emilk

emilk commented Oct 23, 2024

Copy link
Copy Markdown
Owner

Let's wait until after #5228 is merged

@StarStarJ
StarStarJ force-pushed the pr_reduce_memory_usage_font_definitions branch 3 times, most recently from 26d52dc to e624075 Compare October 26, 2024 13:59

@emilk emilk left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a simpler and smaller change would be

@@ -243,7 +243,7 @@ pub struct FontDefinitions {
     /// List of font names and their definitions.
     ///
     /// `epaint` has built-in-default for these, but you can override them if you like.
-    pub font_data: BTreeMap<String, FontData>,
+    pub font_data: BTreeMap<String, Arc<FontData>>,

@StarStarJ
StarStarJ force-pushed the pr_reduce_memory_usage_font_definitions branch from e624075 to 18be843 Compare October 29, 2024 14:07
@StarStarJ StarStarJ changed the title Reduce the amount of FontDefinitions cloning, wrap it in Arc Put font data into Arc to reduce memory consumption Oct 29, 2024
@StarStarJ

Copy link
Copy Markdown
Contributor Author

I think a simpler and smaller change would be

@@ -243,7 +243,7 @@ pub struct FontDefinitions {
     /// List of font names and their definitions.
     ///
     /// `epaint` has built-in-default for these, but you can override them if you like.
-    pub font_data: BTreeMap<String, FontData>,
+    pub font_data: BTreeMap<String, Arc<FontData>>,

Sounds good aswell

@StarStarJ
StarStarJ force-pushed the pr_reduce_memory_usage_font_definitions branch 2 times, most recently from 753d87c to 522b1ad Compare October 29, 2024 14:20

@emilk emilk left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better!

Comment thread crates/egui/src/context.rs Outdated
@emilk emilk added performance Lower CPU/GPU usage (optimize) egui epaint labels Oct 29, 2024
And additionally make cloning `FontDefinitions` cheaper.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

egui epaint performance Lower CPU/GPU usage (optimize)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants