From 7b35f8f567946e71a6409873398d351293153e99 Mon Sep 17 00:00:00 2001 From: piny4man Date: Sat, 17 Oct 2026 10:34:28 +0200 Subject: [PATCH] perf(gpu): skip fontconfig for icon fonts and cache font resolution resolve_fonts cost 39-43 ms on every open, about half of native first presentation: even with `[font] path` set it spawned `fc-match monospace` and, because the default mono is not a Nerd Font, an `fc-list` scan of every installed font. - When the first parseable `[font] path` / HYPRBURST_FONT font already has Nerd Font icons, fontconfig is skipped; only the hard-coded fallback paths that exist are kept for missing text glyphs. - Otherwise the fontconfig answer is cached in $XDG_CACHE_HOME/hyprburst/fonts, keyed by the font inputs and the fontconfig cache directories' mtimes, and looked up again when the key changes or a cached font no longer exists. resolve_fonts is now 1.1-2.2 ms. First-content p95 (n=30) drops by 40-51 ms for both frontends in performance and balanced, and every balanced-vs-performance first-presentation target now passes. docs/hotpath-findings.md records the re-measure and links the filed follow-ups. --- docs/hotpath-findings.md | 59 ++++++- docs/hotpath-profiling.md | 6 +- src/gpu/font.rs | 360 +++++++++++++++++++++++++++++++++++--- 3 files changed, 390 insertions(+), 35 deletions(-) diff --git a/docs/hotpath-findings.md b/docs/hotpath-findings.md index 0cad114..84a83c2 100644 --- a/docs/hotpath-findings.md +++ b/docs/hotpath-findings.md @@ -99,6 +99,9 @@ Ranked by expected impact on what the user waits for. ### 1. `resolve_fonts` spawns two fontconfig processes on every open (~42 ms) +**Fixed in [NUKE-111](https://linear.app/tiendacables/issue/NUKE-111); see +[After NUKE-111](#after-nuke-111--2026-10-17).** + `resolve_fonts` takes 41–47 ms on every open in both frontends, about half of native first presentation. Even with `[font] path` set, it runs `fc-match monospace` (~9.5 ms measured standalone) and, because that match (Noto Sans @@ -163,11 +166,57 @@ Rio this blocked the PTY wake: content appeared at ~185 ms instead of ~107 ms. With `HYPRBURST_PERF` set, `--measure` now reports after content has been presented; the reported cold-start value is unchanged. +## After NUKE-111 — 2026-10-17 + +Branch `perf/font-resolution` (on `9554c90`). Same machine, procedure and +sample counts as the baseline (30 cold + 30 warm opens per frontend and +profile), with this machine's config (`[font] path` set to 0xProto Nerd Font +Mono). + +`resolve_fonts` no longer runs fontconfig when the configured (or +`HYPRBURST_FONT`) font already has Nerd Font icons; otherwise the fontconfig +answer is cached in `$XDG_CACHE_HOME/hyprburst/fonts` and looked up again only +when the font inputs or the fontconfig cache directories' mtimes change, or a +cached font is gone. + +`resolve_fonts` per open (hotpath): 39–43 ms before; 1.1–1.4 ms after on the +skip path, 1.4–2.2 ms on a cache hit without `[font] path`. An uncached lookup +still costs 30–40 ms and happens once after fonts change. + +### First presentation, p95 (ms) + +| Open | Frontend | Performance | Δ baseline | Balanced | Δ baseline | Balanced vs perf | ≤ 10% | +| --- | --- | ---: | ---: | ---: | ---: | ---: | :---: | +| cold | native | 42.9 | −43.1 | 46.9 | −47.0 | +9.4% | pass | +| cold | rio | 74.9 | −39.6 | 77.8 | −45.5 | +3.9% | pass | +| warm | native | 49.4 | −41.7 | 52.6 | −51.3 | +6.3% | pass | +| warm | rio | 80.6 | −41.9 | 80.1 | −50.2 | −0.6% | pass | +| cold | rio, blank frame | 39.9 | −39.4 | 43.0 | −46.2 | +7.8% | (pass) | +| warm | rio, blank frame | 45.8 | −43.4 | 44.5 | −54.2 | −2.7% | (pass) | + +Medians (performance → balanced): cold native 39.6 → 43.5, cold rio 70.0 → +74.3, warm native 42.0 → 46.4, warm rio 70.3 → 73.9. + +NUKE-111's done-when holds: `resolve_fonts` is under 15 ms with a warm cache, +and native first-content p95 dropped by 41–51 ms (≥ 25 ms required) in both +profiles. Every first-presentation target now passes, including warm native +(finding 4, tracked in NUKE-115), although cold native at +9.4% has little +margin. + +What remains, in order: + +- `GlState::new` is now the largest open phase: 28–30 ms in performance, 42 ms + in one balanced report (NUKE-112). +- Rio content still arrives 31–35 ms after its blank frame (NUKE-113). + ## Follow-ups -Drafted for approval, not yet filed: +Filed as sub-issues of NUKE-109: -1. Cache font resolution and skip the Nerd Font scan (finding 1). -2. Overlap font loading with GL context creation (finding 2). -3. Rio: first draw before first poll, earlier child spawn (finding 3). -4. Probe: consistent no-op key handling across frontends (finding 5). +| Issue | Finding | Blocked by | Status | +| --- | --- | --- | --- | +| [NUKE-111](https://linear.app/tiendacables/issue/NUKE-111) Cache font resolution and skip the Nerd Font scan | 1 | — | fixed (see above) | +| [NUKE-112](https://linear.app/tiendacables/issue/NUKE-112) Load fonts while the window and GL context are created | 2 | NUKE-111 | open | +| [NUKE-113](https://linear.app/tiendacables/issue/NUKE-113) Rio: draw before the first input poll, spawn the child earlier | 3 | — | open | +| [NUKE-114](https://linear.app/tiendacables/issue/NUKE-114) Probe: count no-op keys the same way in both frontends | 5 | — | open | +| [NUKE-115](https://linear.app/tiendacables/issue/NUKE-115) Re-measure warm native open in balanced | 4 | NUKE-111, NUKE-112 | passes after NUKE-111; confirm after NUKE-112 | diff --git a/docs/hotpath-profiling.md b/docs/hotpath-profiling.md index 9af705d..dd6c792 100644 --- a/docs/hotpath-profiling.md +++ b/docs/hotpath-profiling.md @@ -109,7 +109,11 @@ per metric and frontend; other stderr lines are ignored. ### Cold and warm open Each open is a fresh process. Without root the page cache cannot be dropped, -so both cases run with binaries, fonts, and `.desktop` files cached: +so both cases run with binaries, fonts, and `.desktop` files cached. Font +resolution is cached too (`$XDG_CACHE_HOME/hyprburst/fonts`, unless the +configured font already has icons); the first open after installing or +removing fonts pays a fontconfig lookup of 30–40 ms, so do one throwaway open +before a run: - **cold**: the first open after at least 2 s idle, so the CPU has clocked down and its caches are cold — what pressing the launcher key costs; diff --git a/src/gpu/font.rs b/src/gpu/font.rs index 6028c07..404578c 100644 --- a/src/gpu/font.rs +++ b/src/gpu/font.rs @@ -14,12 +14,22 @@ //! plain default monospace (may lack icon glyphs), //! 5. a few common hard-coded paths, as a last resort. //! -//! Cold-start matters here: the common case (a Nerd Font as the default -//! monospace) costs exactly one `fc-match` spawn; the scan-for-any-Nerd-Font -//! path adds one `fc-list`. - -use std::path::PathBuf; +//! Cold-start matters here (fontconfig spawns cost ~10–30 ms per open): +//! +//! - when the first parseable font from 1–2 already has Nerd Font icons, +//! fontconfig is skipped entirely and only the hard-coded paths that exist +//! are kept as text fallbacks; +//! - otherwise the fontconfig answers (3–4) are cached in +//! `$XDG_CACHE_HOME/hyprburst/fonts`, keyed by the inputs and the fontconfig +//! cache directories' mtimes, and looked up again only when the key changes +//! or a cached font no longer exists. Uncached, the common case (a Nerd Font +//! as the default monospace) is one `fc-match` spawn; the +//! scan-for-any-Nerd-Font path adds one `fc-list`. + +use std::collections::HashSet; +use std::path::{Path, PathBuf}; use std::process::Command; +use std::time::SystemTime; use ab_glyph::{Font, FontVec, PxScale}; @@ -112,21 +122,72 @@ fn is_icon(ch: char) -> bool { /// remain characters: a given character always selects the same face and glyph. #[hotpath::measure] pub(crate) fn resolve_fonts(config_path: Option<&str>, family: Option<&str>) -> Option { - fonts_from_paths(candidate_paths(config_path, family)) + let env_path = std::env::var(FONT_ENV).ok(); + let fallbacks: Vec = FALLBACK_PATHS.iter().map(PathBuf::from).collect(); + resolve_with(config_path, env_path.as_deref(), &fallbacks, || { + cached_fontconfig_paths(config_path, env_path.as_deref(), family) + }) +} + +/// A Font Awesome icon every Nerd Font carries and the launcher draws +/// (nf-fa-firefox). A configured font that has it needs no fontconfig-derived +/// icon fallback. +const ICON_PROBE: char = '\u{f269}'; + +fn has_icons(font: &FontVec) -> bool { + font.glyph_id(ICON_PROBE).0 != 0 } +/// Resolve from the explicit `config_path` / `env_path` fonts, consulting +/// `fontconfig` only when the first parseable explicit font lacks icons. +fn resolve_with( + config_path: Option<&str>, + env_path: Option<&str>, + fallbacks: &[PathBuf], + fontconfig: impl FnOnce() -> Vec, +) -> Option { + let mut seen = HashSet::new(); + let mut fonts = load_fonts( + collect_candidates(config_path, env_path, Vec::new(), []), + &mut seen, + ); + let derived = if fonts.first().is_some_and(has_icons) { + Vec::new() + } else { + fontconfig() + }; + fonts.extend(load_fonts( + derived.into_iter().chain(fallbacks.iter().cloned()), + &mut seen, + )); + font_set(fonts) +} + +#[cfg(test)] fn fonts_from_paths(paths: Vec) -> Option { - let mut seen = std::collections::HashSet::new(); - let mut fonts = paths - .into_iter() - .filter(|p| seen.insert(p.clone())) - .filter_map(|p| FontVec::try_from_vec(std::fs::read(p).ok()?).ok()); + font_set(load_fonts(paths, &mut HashSet::new())) +} + +fn font_set(fonts: Vec) -> Option { + let mut fonts = fonts.into_iter(); Some(FontSet { primary: fonts.next()?, fallbacks: fonts.collect(), }) } +/// Every parseable font among `paths` not already in `seen`, in order. +fn load_fonts( + paths: impl IntoIterator, + seen: &mut HashSet, +) -> Vec { + paths + .into_iter() + .filter(|p| seen.insert(p.clone())) + .filter_map(|p| FontVec::try_from_vec(std::fs::read(p).ok()?).ok()) + .collect() +} + /// The environment variable that pins the cell font, overriding `fc-match`. const FONT_ENV: &str = "HYPRBURST_FONT"; @@ -143,30 +204,115 @@ const FALLBACK_PATHS: &[&str] = &[ /// if nothing resolved. Validation happens here so a readable-but-corrupt file /// falls through to the next candidate instead of aborting resolution upstream. pub fn resolve_font(config_path: Option<&str>, family: Option<&str>) -> Option { - candidate_paths(config_path, family) - .into_iter() - .find_map(|p| { - let bytes = std::fs::read(&p).ok()?; - FontVec::try_from_vec(bytes).ok() + resolve_fonts(config_path, family).map(|fonts| fonts.primary) +} + +/// The cache key for fontconfig-derived paths: every input that can change the +/// answer, plus the fontconfig cache directories' `stamps` (mtimes), which +/// `fc-cache` bumps when fonts are installed or removed. Always one line. +fn cache_key( + config_path: Option<&str>, + env_path: Option<&str>, + family: Option<&str>, + stamps: &[Option], +) -> String { + let stamps: Vec> = stamps + .iter() + .map(|t| { + Some( + t.as_ref()? + .duration_since(SystemTime::UNIX_EPOCH) + .ok()? + .as_nanos(), + ) }) + .collect(); + // `Debug` escapes control characters, so the key never spans lines. + format!("v1 {:?}", (config_path, env_path, family, stamps)) } -/// The ordered list of font paths to try, most-specific first. -fn candidate_paths(config_path: Option<&str>, family: Option<&str>) -> Vec { - let env_path = std::env::var(FONT_ENV).ok(); - let family_paths = family +/// Cached fontconfig-derived paths in `file` for `key`, or `lookup()` (written +/// back best-effort) when the cache is missing, stale, corrupt, or names a +/// font that no longer exists. +fn cached_paths(file: &Path, key: &str, lookup: impl FnOnce() -> Vec) -> Vec { + if let Some(paths) = read_cache(file, key) { + return paths; + } + let paths = lookup(); + write_cache(file, key, &paths); + paths +} + +fn read_cache(file: &Path, key: &str) -> Option> { + let text = std::fs::read_to_string(file).ok()?; + let mut lines = text.lines(); + if lines.next()? != key { + return None; + } + let paths: Vec = lines.map(PathBuf::from).collect(); + paths.iter().all(|p| p.exists()).then_some(paths) +} + +/// Best effort: a failed write only means the next open looks up again. +fn write_cache(file: &Path, key: &str, paths: &[PathBuf]) { + let mut text = format!("{key}\n"); + for path in paths { + match path.to_str() { + Some(p) if !p.contains('\n') => text.push_str(&format!("{p}\n")), + _ => return, + } + } + let Some(dir) = file.parent() else { + return; + }; + // Write-then-rename so a concurrent open never reads a half-written file. + let tmp = file.with_extension(format!("tmp{}", std::process::id())); + if std::fs::create_dir_all(dir).is_ok() && std::fs::write(&tmp, text).is_ok() { + let _ = std::fs::rename(&tmp, file); + } +} + +/// `$XDG_CACHE_HOME`, else `$HOME/.cache`. +fn cache_home() -> Option { + std::env::var_os("XDG_CACHE_HOME") + .filter(|v| !v.is_empty()) + .map(PathBuf::from) + .or_else(|| Some(PathBuf::from(std::env::var_os("HOME")?).join(".cache"))) +} + +fn modified(path: &Path) -> Option { + std::fs::metadata(path).ok()?.modified().ok() +} + +/// fontconfig-derived paths through the on-disk cache, when there is a cache +/// directory to use. +fn cached_fontconfig_paths( + config_path: Option<&str>, + env_path: Option<&str>, + family: Option<&str>, +) -> Vec { + let Some(home) = cache_home() else { + return fontconfig_paths(family); + }; + let stamps = [ + modified(&home.join("fontconfig")), + modified(Path::new("/var/cache/fontconfig")), + ]; + let key = cache_key(config_path, env_path, family, &stamps); + cached_paths(&home.join("hyprburst").join("fonts"), &key, || { + fontconfig_paths(family) + }) +} + +/// fontconfig-derived paths, most specific first: the configured family, then +/// the default monospace / any Nerd Font. +fn fontconfig_paths(family: Option<&str>) -> Vec { + family .filter(|f| !f.is_empty()) .and_then(fc_match_family) .into_iter() - .collect(); - collect_candidates( - config_path, - env_path.as_deref(), - family_paths, - fc_font_candidates() - .into_iter() - .chain(FALLBACK_PATHS.iter().map(PathBuf::from)), - ) + .chain(fc_font_candidates()) + .collect() } fn collect_candidates( @@ -434,6 +580,162 @@ mod tests { )); } + fn fixture(name: &str) -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests/fixtures/fonts") + .join(name) + } + + #[test] + fn has_icons_detects_the_nerd_font_probe_glyph() { + let fonts = fixture_fonts(); + assert!(!has_icons(&fonts.primary)); + assert!(has_icons(&fonts.fallbacks[0])); + } + + #[test] + fn configured_icon_font_skips_fontconfig() { + for (config, env) in [ + (Some(fixture("icons.ttf")), None), + (None, Some(fixture("icons.ttf"))), + ] { + let fonts = resolve_with( + config.as_deref().and_then(|p| p.to_str()), + env.as_deref().and_then(|p| p.to_str()), + &[fixture("primary.ttf")], + || panic!("fontconfig must not be consulted"), + ) + .unwrap(); + assert!(has_icons(&fonts.primary)); + assert_eq!(fonts.fallbacks.len(), 1); + assert_ne!(fonts.fallbacks[0].glyph_id('M').0, 0); + } + } + + #[test] + fn configured_text_font_still_gets_fontconfig_icons() { + let mut called = false; + let fonts = resolve_with(fixture("primary.ttf").to_str(), None, &[], || { + called = true; + vec![fixture("icons.ttf")] + }) + .unwrap(); + assert!(called); + assert!(!has_icons(&fonts.primary)); + assert!(has_icons(&fonts.fallbacks[0])); + } + + #[test] + fn unparseable_or_missing_config_falls_through_to_fontconfig() { + let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml"); + for config in [manifest.to_str(), None] { + let mut called = false; + let fonts = resolve_with(config, None, &[fixture("icons.ttf")], || { + called = true; + vec![fixture("primary.ttf")] + }) + .unwrap(); + assert!(called); + assert_ne!(fonts.primary.glyph_id('M').0, 0); + assert!(!has_icons(&fonts.primary)); + assert_eq!(fonts.fallbacks.len(), 1); + } + } + + fn cache_file(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!( + "hyprburst-font-cache-{}-{name}", + std::process::id() + )); + let _ = std::fs::remove_dir_all(&dir); + dir.join("hyprburst").join("fonts") + } + + #[test] + fn cache_miss_runs_lookup_and_a_hit_reuses_it() { + let file = cache_file("hit"); + let fonts = vec![fixture("icons.ttf"), fixture("primary.ttf")]; + let mut calls = 0; + assert_eq!( + cached_paths(&file, "k", || { + calls += 1; + fonts.clone() + }), + fonts + ); + assert_eq!(calls, 1); + assert_eq!( + cached_paths(&file, "k", || panic!("cache hit must not look up")), + fonts + ); + } + + #[test] + fn changed_key_reruns_lookup() { + let file = cache_file("key"); + cached_paths(&file, "old", || vec![fixture("icons.ttf")]); + assert_eq!( + cached_paths(&file, "new", || vec![fixture("primary.ttf")]), + vec![fixture("primary.ttf")] + ); + assert_eq!( + cached_paths(&file, "new", || panic!("rewritten cache must hit")), + vec![fixture("primary.ttf")] + ); + } + + #[test] + fn vanished_cached_font_reruns_lookup() { + let file = cache_file("gone"); + let gone = file.with_file_name("gone.ttf"); + std::fs::create_dir_all(file.parent().unwrap()).unwrap(); + std::fs::write(&gone, b"x").unwrap(); + cached_paths(&file, "k", || vec![gone.clone()]); + std::fs::remove_file(&gone).unwrap(); + let mut called = false; + cached_paths(&file, "k", || { + called = true; + vec![fixture("icons.ttf")] + }); + assert!(called); + } + + #[test] + fn empty_or_corrupt_cache_reruns_lookup() { + for contents in ["", "not the key\n/some/font.ttf\n"] { + let file = cache_file("corrupt"); + std::fs::create_dir_all(file.parent().unwrap()).unwrap(); + std::fs::write(&file, contents).unwrap(); + let mut called = false; + cached_paths(&file, "k", || { + called = true; + Vec::new() + }); + assert!(called, "{contents:?}"); + } + } + + #[test] + fn cache_key_tracks_every_input_on_one_line() { + let t = Some(SystemTime::UNIX_EPOCH); + let later = Some(SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1)); + let base = cache_key(Some("/c.ttf"), Some("/e.ttf"), Some("Mono"), &[t, None]); + for other in [ + cache_key(None, Some("/e.ttf"), Some("Mono"), &[t, None]), + cache_key(Some("/c.ttf"), None, Some("Mono"), &[t, None]), + cache_key(Some("/c.ttf"), Some("/e.ttf"), Some("Sans"), &[t, None]), + cache_key(Some("/c.ttf"), Some("/e.ttf"), Some("Mono"), &[later, None]), + cache_key(Some("/c.ttf"), Some("/e.ttf"), Some("Mono"), &[t, t]), + ] { + assert_ne!(base, other); + } + assert_eq!( + base, + cache_key(Some("/c.ttf"), Some("/e.ttf"), Some("Mono"), &[t, None]) + ); + assert!(!cache_key(Some("a\nb"), None, None, &[]).contains('\n')); + } + #[test] fn family_is_nerd_detects_nerd_fonts() { assert!(family_is_nerd("JetBrainsMono Nerd Font Mono"));