Improve loading times for extension themes (#40015)

This PR primarily does two things:
- replace `serde_json::from_reader` with `serde_json::from_slice`, as
the latter is much much faster, even with loading the file into memory
first.
- runs the initial loading of themes and icon themes coming from
extensions in parallel instead of sequential.

Measuring the `eager_load_active_theme_and_icon_theme` method, this
drastically improves the speed at which this happens (tested this method
primarily with debug builds on my MacBook Pro, but the `Before`
measurement was also confirmed against a `release-fast` build):
- Before: ~260ms on average (in one run, it even took 600ms)
- After: ~20ms on average

Which reduces the time this method takes to load these by around ~92%.

Given that we block on this during the initial app startup, this should
drastically improve Zeds initial startup loading time. Yet, it also
improves responsiveness when installing theme extensions and trying
these.

I also replaced all other `serde_json::from_reader` implementations with
`serde_json::from_slice` and added the former to `disallowed_methods`,
given
https://github.com/serde-rs/json/issues/160#issuecomment-253446892.

Release Notes:

- Improved Zed startup speed when using themes provided by extensions
This commit is contained in:
Finn Evers
2025-10-13 11:53:19 +02:00
committed by GitHub
parent 47a66c938f
commit 81cd435e08
6 changed files with 115 additions and 55 deletions
+79 -35
View File
@@ -70,10 +70,7 @@ use std::{
sync::atomic::{self, AtomicBool},
};
use terminal_view::terminal_panel::{self, TerminalPanel};
use theme::{
ActiveTheme, GlobalTheme, IconThemeNotFoundError, SystemAppearance, ThemeNotFoundError,
ThemeRegistry, ThemeSettings,
};
use theme::{ActiveTheme, GlobalTheme, SystemAppearance, ThemeRegistry, ThemeSettings};
use ui::{PopoverMenuHandle, prelude::*};
use util::markdown::MarkdownString;
use util::rel_path::RelPath;
@@ -2032,41 +2029,88 @@ pub(crate) fn eager_load_active_theme_and_icon_theme(fs: Arc<dyn Fs>, cx: &mut A
let theme_settings = ThemeSettings::get_global(cx);
let appearance = SystemAppearance::global(cx).0;
let theme_name = theme_settings.theme.name(appearance);
if matches!(
theme_registry.get(&theme_name.0),
Err(ThemeNotFoundError(_))
) && let Some(theme_path) = extension_store
.read(cx)
.path_to_extension_theme(&theme_name.0)
{
if cx
.background_executor()
.block(theme_registry.load_user_theme(&theme_path, fs.clone()))
.log_err()
.is_some()
{
GlobalTheme::reload_theme(cx);
}
enum LoadTarget {
Theme(PathBuf),
IconTheme((PathBuf, PathBuf)),
}
let theme_settings = ThemeSettings::get_global(cx);
let theme_name = theme_settings.theme.name(appearance);
let icon_theme_name = theme_settings.icon_theme.name(appearance);
if matches!(
theme_registry.get_icon_theme(&icon_theme_name.0),
Err(IconThemeNotFoundError(_))
) && let Some((icon_theme_path, icons_root_path)) = extension_store
.read(cx)
.path_to_extension_icon_theme(&icon_theme_name.0)
{
if cx
.background_executor()
.block(theme_registry.load_icon_theme(&icon_theme_path, &icons_root_path, fs))
.log_err()
.is_some()
{
GlobalTheme::reload_icon_theme(cx);
let themes_to_load = [
theme_registry
.get(&theme_name.0)
.is_err()
.then(|| {
extension_store
.read(cx)
.path_to_extension_theme(&theme_name.0)
})
.flatten()
.map(LoadTarget::Theme),
theme_registry
.get_icon_theme(&icon_theme_name.0)
.is_err()
.then(|| {
extension_store
.read(cx)
.path_to_extension_icon_theme(&icon_theme_name.0)
})
.flatten()
.map(LoadTarget::IconTheme),
];
enum ReloadTarget {
Theme,
IconTheme,
}
let executor = cx.background_executor();
let reload_tasks = parking_lot::Mutex::new(Vec::with_capacity(themes_to_load.len()));
let mut themes_to_load = themes_to_load.into_iter().flatten().peekable();
if themes_to_load.peek().is_none() {
return;
}
executor.block(executor.scoped(|scope| {
for load_target in themes_to_load {
let theme_registry = &theme_registry;
let reload_tasks = &reload_tasks;
let fs = fs.clone();
scope.spawn(async {
match load_target {
LoadTarget::Theme(theme_path) => {
if theme_registry
.load_user_theme(&theme_path, fs)
.await
.log_err()
.is_some()
{
reload_tasks.lock().push(ReloadTarget::Theme);
}
}
LoadTarget::IconTheme((icon_theme_path, icons_root_path)) => {
if theme_registry
.load_icon_theme(&icon_theme_path, &icons_root_path, fs)
.await
.log_err()
.is_some()
{
reload_tasks.lock().push(ReloadTarget::IconTheme);
}
}
}
});
}
}));
for reload_target in reload_tasks.into_inner() {
match reload_target {
ReloadTarget::Theme => GlobalTheme::reload_theme(cx),
ReloadTarget::IconTheme => GlobalTheme::reload_icon_theme(cx),
};
}
}