Skip to content

Commit

Permalink
Reduce the amount of FontDefinitions cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
StarStarJ committed Oct 16, 2024
1 parent 707cd03 commit 851759c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 22 deletions.
10 changes: 5 additions & 5 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ struct ContextImpl {
/// This is because the `Fonts` depend on `pixels_per_point` for the font atlas
/// as well as kerning, font sizes, etc.
fonts: std::collections::BTreeMap<OrderedFloat<f32>, Fonts>,
font_definitions: FontDefinitions,
font_definitions: Arc<FontDefinitions>,

memory: Memory,
animation_manager: AnimationManager,
Expand Down Expand Up @@ -1727,7 +1727,7 @@ impl Context {
/// but you can call this to install additional fonts that support e.g. korean characters.
///
/// The new fonts will become active at the start of the next pass.
pub fn set_fonts(&self, font_definitions: FontDefinitions) {
pub fn set_fonts(&self, font_definitions: Arc<FontDefinitions>) {
crate::profile_function!();

let pixels_per_point = self.pixels_per_point();
Expand All @@ -1737,7 +1737,7 @@ impl Context {
self.read(|ctx| {
if let Some(current_fonts) = ctx.fonts.get(&pixels_per_point.into()) {
// NOTE: this comparison is expensive since it checks TTF data for equality
if current_fonts.lock().fonts.definitions() == &font_definitions {
if current_fonts.lock().fonts.definitions() == font_definitions.as_ref() {
update_fonts = false; // no need to update
}
}
Expand Down Expand Up @@ -2879,7 +2879,7 @@ impl Context {
}

fn fonts_tweak_ui(&self, ui: &mut Ui) {
let mut font_definitions = self.write(|ctx| ctx.font_definitions.clone());
let mut font_definitions = self.write(|ctx| ctx.font_definitions.as_ref().clone());
let mut changed = false;

for (name, data) in &mut font_definitions.font_data {
Expand All @@ -2891,7 +2891,7 @@ impl Context {
}

if changed {
self.set_fonts(font_definitions);
self.set_fonts(Arc::new(font_definitions));
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ pub enum WidgetType {
/// For use in tests; especially doctests.
pub fn __run_test_ctx(mut run_ui: impl FnMut(&Context)) {
let ctx = Context::default();
ctx.set_fonts(FontDefinitions::empty()); // prevent fonts from being loaded (save CPU time)
ctx.set_fonts(Default::default()); // prevent fonts from being loaded (save CPU time)
let _ = ctx.run(Default::default(), |ctx| {
run_ui(ctx);
});
Expand All @@ -676,7 +676,7 @@ pub fn __run_test_ctx(mut run_ui: impl FnMut(&Context)) {
/// For use in tests; especially doctests.
pub fn __run_test_ui(add_contents: impl Fn(&mut Ui)) {
let ctx = Context::default();
ctx.set_fonts(FontDefinitions::empty()); // prevent fonts from being loaded (save CPU time)
ctx.set_fonts(Default::default()); // prevent fonts from being loaded (save CPU time)
let _ = ctx.run(Default::default(), |ctx| {
crate::CentralPanel::default().show(ctx, |ui| {
add_contents(ui);
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub struct Memory {
// ------------------------------------------
/// new fonts that will be applied at the start of the next frame
#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) new_font_definitions: Option<epaint::text::FontDefinitions>,
pub(crate) new_font_definitions: Option<std::sync::Arc<epaint::text::FontDefinitions>>,

// Current active viewport
#[cfg_attr(feature = "persistence", serde(skip))]
Expand Down
7 changes: 2 additions & 5 deletions crates/egui_demo_lib/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,8 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let wrap_width = 512.0;
let font_id = egui::FontId::default();
let text_color = egui::Color32::WHITE;
let fonts = egui::epaint::text::Fonts::new(
pixels_per_point,
max_texture_side,
egui::FontDefinitions::default(),
);
let fonts =
egui::epaint::text::Fonts::new(pixels_per_point, max_texture_side, Default::default());
{
let mut locked_fonts = fonts.lock();
c.bench_function("text_layout_uncached", |b| {
Expand Down
6 changes: 3 additions & 3 deletions crates/epaint/src/text/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ impl Fonts {
pub fn new(
pixels_per_point: f32,
max_texture_side: usize,
definitions: FontDefinitions,
definitions: Arc<FontDefinitions>,
) -> Self {
let fonts_and_cache = FontsAndCache {
fonts: FontsImpl::new(pixels_per_point, max_texture_side, definitions),
Expand Down Expand Up @@ -570,7 +570,7 @@ impl FontsAndCache {
pub struct FontsImpl {
pixels_per_point: f32,
max_texture_side: usize,
definitions: FontDefinitions,
definitions: Arc<FontDefinitions>,
atlas: Arc<Mutex<TextureAtlas>>,
font_impl_cache: FontImplCache,
sized_family: ahash::HashMap<(OrderedFloat<f32>, FontFamily), Font>,
Expand All @@ -582,7 +582,7 @@ impl FontsImpl {
pub fn new(
pixels_per_point: f32,
max_texture_side: usize,
definitions: FontDefinitions,
definitions: Arc<FontDefinitions>,
) -> Self {
assert!(
0.0 < pixels_per_point && pixels_per_point < 100.0,
Expand Down
10 changes: 5 additions & 5 deletions crates/epaint/src/text/text_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ mod tests {

#[test]
fn test_zero_max_width() {
let mut fonts = FontsImpl::new(1.0, 1024, FontDefinitions::default());
let mut fonts = FontsImpl::new(1.0, 1024, Default::default());
let mut layout_job = LayoutJob::single_section("W".into(), TextFormat::default());
layout_job.wrap.max_width = 0.0;
let galley = layout(&mut fonts, layout_job.into());
Expand All @@ -1053,7 +1053,7 @@ mod tests {
fn test_truncate_with_newline() {
// No matter where we wrap, we should be appending the newline character.

let mut fonts = FontsImpl::new(1.0, 1024, FontDefinitions::default());
let mut fonts = FontsImpl::new(1.0, 1024, Default::default());
let text_format = TextFormat {
font_id: FontId::monospace(12.0),
..Default::default()
Expand Down Expand Up @@ -1098,7 +1098,7 @@ mod tests {

#[test]
fn test_cjk() {
let mut fonts = FontsImpl::new(1.0, 1024, FontDefinitions::default());
let mut fonts = FontsImpl::new(1.0, 1024, Default::default());
let mut layout_job = LayoutJob::single_section(
"日本語とEnglishの混在した文章".into(),
TextFormat::default(),
Expand All @@ -1113,7 +1113,7 @@ mod tests {

#[test]
fn test_pre_cjk() {
let mut fonts = FontsImpl::new(1.0, 1024, FontDefinitions::default());
let mut fonts = FontsImpl::new(1.0, 1024, Default::default());
let mut layout_job = LayoutJob::single_section(
"日本語とEnglishの混在した文章".into(),
TextFormat::default(),
Expand All @@ -1128,7 +1128,7 @@ mod tests {

#[test]
fn test_truncate_width() {
let mut fonts = FontsImpl::new(1.0, 1024, FontDefinitions::default());
let mut fonts = FontsImpl::new(1.0, 1024, Default::default());
let mut layout_job =
LayoutJob::single_section("# DNA\nMore text".into(), TextFormat::default());
layout_job.wrap.max_width = f32::INFINITY;
Expand Down
4 changes: 3 additions & 1 deletion examples/custom_font/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![allow(rustdoc::missing_crate_level_docs)] // it's an example

use std::sync::Arc;

use eframe::egui;

fn main() -> eframe::Result {
Expand Down Expand Up @@ -44,7 +46,7 @@ fn setup_custom_fonts(ctx: &egui::Context) {
.push("my_font".to_owned());

// Tell egui to use these fonts:
ctx.set_fonts(fonts);
ctx.set_fonts(Arc::new(fonts));
}

struct MyApp {
Expand Down

0 comments on commit 851759c

Please sign in to comment.