From a731b25f0cc245bf949e86aa134ee0163cc76c54 Mon Sep 17 00:00:00 2001 From: Mukilan Thiyagarajan Date: Thu, 21 Nov 2024 17:53:14 +0530 Subject: [PATCH] fonts: fix broken caching of font template matches (#34325) After a cache miss, `find_matching_font_template` never updates the cache entry with the response from the `SystemFontService` leading to several unnecessary IPC calls during layout. Signed-off-by: Mukilan Thiyagarajan Co-authored-by: Martin Robinson --- components/fonts/system_font_service.rs | 8 ++++++-- components/fonts/tests/font_context.rs | 13 +++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/components/fonts/system_font_service.rs b/components/fonts/system_font_service.rs index 69244d12410..8bf34f76e54 100644 --- a/components/fonts/system_font_service.rs +++ b/components/fonts/system_font_service.rs @@ -533,11 +533,15 @@ impl SystemFontServiceProxy { ); panic!("SystemFontService has already exited."); }; - templates + + let templates: Vec<_> = templates .into_iter() .map(AtomicRefCell::new) .map(Arc::new) - .collect() + .collect(); + self.templates.write().insert(cache_key, templates.clone()); + + templates } pub(crate) fn generate_font_key(&self) -> FontKey { diff --git a/components/fonts/tests/font_context.rs b/components/fonts/tests/font_context.rs index 7b141cf2d3c..cf6268970d2 100644 --- a/components/fonts/tests/font_context.rs +++ b/components/fonts/tests/font_context.rs @@ -370,6 +370,19 @@ mod font_context { .matching_templates(&font_descriptor, &family_descriptor)[0] .clone(); + let _ = context + .context + .matching_templates(&font_descriptor, &family_descriptor); + + assert_eq!( + context + .system_font_service + .find_font_count + .fetch_add(0, Ordering::Relaxed), + 1, + "we should only have requested matching templates from the font service once" + ); + let font1 = context .context .font(font_template.clone(), &font_descriptor)