From 2234bc56a54c488d39762060b4dbd3e613c8174b Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Fri, 4 Oct 2024 16:46:40 +0200 Subject: [PATCH] fonts: Eliminate overhead of first font instance creation (#33638) The first font instance creation in the `SystemFontService` was triggering fetching font keys, which added an approximately 1ms lag to this operation. Doing this as soon as the `SystemFontService` thread is spawned eliminates this lag. In addition increase the size of the font key and font instance key batch in order to avoid having to fetch new keys so frequently. This change also adds profiling spans to the `SystemFontService` so it is easier to see where it is spending its time when using perfetto. Signed-off-by: Martin Robinson --- components/fonts/system_font_service.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/components/fonts/system_font_service.rs b/components/fonts/system_font_service.rs index 36130c5ed8b..14421800a89 100644 --- a/components/fonts/system_font_service.rs +++ b/components/fonts/system_font_service.rs @@ -147,6 +147,7 @@ impl SystemFontService { free_font_instance_keys: Default::default(), }; + cache.fetch_new_keys(); cache.refresh_local_families(); cache.run(); }) @@ -160,14 +161,18 @@ impl SystemFontService { loop { let msg = self.port.recv().unwrap(); + let span = span!( + Level::TRACE, + "SystemFontServiceMessage", + servo_profiling = true + ); + let _enter = span.enter(); match msg { SystemFontServiceMessage::GetFontTemplates( font_descriptor, font_family, result_sender, ) => { - let span = span!(Level::TRACE, "GetFontTemplates", servo_profiling = true); - let _span = span.enter(); let _ = result_sender.send(self.get_font_templates(font_descriptor, font_family)); }, @@ -191,13 +196,14 @@ impl SystemFontService { } } + #[tracing::instrument(skip(self), fields(servo_profiling = true))] fn fetch_new_keys(&mut self) { if !self.free_font_keys.is_empty() && !self.free_font_instance_keys.is_empty() { return; } - const FREE_FONT_KEYS_BATCH_SIZE: usize = 20; - const FREE_FONT_INSTANCE_KEYS_BATCH_SIZE: usize = 20; + const FREE_FONT_KEYS_BATCH_SIZE: usize = 40; + const FREE_FONT_INSTANCE_KEYS_BATCH_SIZE: usize = 40; let (mut new_font_keys, mut new_font_instance_keys) = self.webrender_api.fetch_font_keys( FREE_FONT_KEYS_BATCH_SIZE - self.free_font_keys.len(), FREE_FONT_INSTANCE_KEYS_BATCH_SIZE - self.free_font_instance_keys.len(), @@ -207,6 +213,7 @@ impl SystemFontService { .append(&mut new_font_instance_keys); } + #[tracing::instrument(skip(self), fields(servo_profiling = true))] fn get_font_templates( &mut self, font_descriptor: Option, @@ -239,6 +246,7 @@ impl SystemFontService { } } + #[tracing::instrument(skip(self), fields(servo_profiling = true))] fn refresh_local_families(&mut self) { self.local_families.clear(); for_each_available_family(|family_name| { @@ -273,6 +281,7 @@ impl SystemFontService { .unwrap_or_default() } + #[tracing::instrument(skip(self), fields(servo_profiling = true))] fn get_font_instance( &mut self, identifier: FontIdentifier,