egui icon indicating copy to clipboard operation
egui copied to clipboard

Reduce the amount of `FontDefinitions` cloning, wrap it in `Arc`

Open StarStarJ opened this issue 1 year ago • 2 comments

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 avatar Oct 16 '24 15:10 StarStarJ

E.g. it keeps a copy in ContextImpl and copies it into the fonts per pixels_per_point: https://github.com/emilk/egui/blob/707cd03357f926d040acb241a5c42d8e91a83d8e/crates/egui/src/context.rs#L599

from: https://github.com/emilk/egui/blob/707cd03357f926d040acb241a5c42d8e91a83d8e/crates/egui/src/context.rs#L587-L601

StarStarJ avatar Oct 16 '24 15:10 StarStarJ

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.

github-actions[bot] avatar Oct 16 '24 15:10 github-actions[bot]

Let's wait until after https://github.com/emilk/egui/pull/5228 is merged

emilk avatar Oct 23 '24 08:10 emilk

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 avatar Oct 29 '24 14:10 StarStarJ