Skip to content

Truly independent web browser

LibWeb: Resolve the fonts of a style pass on the threads that ask

Andreas Kling93f01c5b4204
diff --git a/Libraries/LibWeb/Rust/src/css/style/font_resolution.rs b/Libraries/LibWeb/Rust/src/css/style/font_resolution.rs
index b9a59bd52c3..06ab311948d 100644
--- a/Libraries/LibWeb/Rust/src/css/style/font_resolution.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/font_resolution.rs
@@ -398,6 +398,26 @@ impl FontResolutionCache {
})
}
+ /// Takes in what the threads of a pass resolved. Nothing waits on the requests carried from the generations before
+ /// any more: a pass resolves what it asks for. Answers how many fonts they resolved.
+ pub(super) fn take_in_pass_fonts(&mut self, pass_fonts: PassFonts) -> usize {
+ let mut resolved = pass_fonts.into_resolved();
+ let Some(generation) = resolved.generation else {
+ return 0;
+ };
+ self.prepare(generation);
+ self.carried.clear();
+ if let (Some(held), Some(resolved_held)) = (&mut self.held_cascades, resolved.held_cascades) {
+ held.extend(resolved_held);
+ }
+ let resolutions = resolved.resolutions.take().resolutions;
+ let count = resolutions.len();
+ for resolution in resolutions {
+ self.insert(resolution.request, resolution.ffi, None);
+ }
+ count
+ }
+
fn insert(&mut self, request: FontRequest, ffi: FfiResolvedFont, font_cascade_list: Option<HeldFontCascadeList>) {
// The host resolves every request, to the fallback font where nothing else matches, so the
// resolution always names a list.
@@ -452,8 +472,8 @@ struct FontCascadeMemoObject {
// SAFETY: The memo counts its references atomically, so a shared reference to it may be taken and
// given up on any thread. Resolving through it reads the document's font face state, which the
-// memo's lock does not cover, so only `FontResolverHost::refill` resolves, and only the document
-// thread reaches the host.
+// memo's lock does not cover, so only `FontResolverHost::refill` resolves, on one thread at a time:
+// the one running the rounds of a pass, or a thread of a pass holding the lock of `PassFonts`.
unsafe impl Sync for FontCascadeMemoObject {}
/// One reference to a host object, given up on drop. It may cross threads exactly when the object
@@ -534,8 +554,8 @@ impl PublishedFontFaces {
}
}
-/// The host's synchronous font resolver and the table it resolves against. This is host state, and
-/// only a round between evaluation passes may call it.
+/// The host's synchronous font resolver and the table it resolves against. This is host state, which
+/// one thread at a time may call.
#[derive(Clone)]
pub(super) struct FontResolverHost {
resolve: ResolveFontCallback,
@@ -566,7 +586,7 @@ impl FontResolverHost {
self.font_faces = font_faces;
}
- /// Service a synchronous request between evaluation passes, on the document thread: a memo miss
+ /// Service a synchronous request: a memo miss
/// builds and freezes a cascade from the document's font face state. Pending web faces remain
/// in the returned cascade and retain the host's rendering-triggered loading behavior.
pub fn refill(&self, cache: &mut FontResolutionCache, request: FontRequest) {
@@ -593,6 +613,48 @@ impl FontResolverHost {
}
}
+/// The fonts the threads of a style pass resolve as they first ask for them, one thread at a time under the lock, the
+/// others going on with what they have. What they resolve joins the cache as the pass ends.
+pub(super) struct PassFonts {
+ resolver: FontResolverHost,
+ resolved: std::sync::RwLock<FontResolutionCache>,
+}
+
+impl PassFonts {
+ /// What a pass reading `cache` resolves through `resolver`.
+ pub(super) fn new(resolver: &FontResolverHost, cache: &FontResolutionCache) -> Self {
+ Self {
+ resolver: resolver.clone(),
+ resolved: std::sync::RwLock::new(FontResolutionCache {
+ matches_by_content: cache.matches_by_content,
+ held_cascades: cache.held_cascades.as_ref().map(|_| Vec::new()),
+ ..Default::default()
+ }),
+ }
+ }
+
+ /// The resolution of `request`, resolved now where no thread of the pass did yet.
+ pub(super) fn resolve(&self, request: FfiFontResolutionRequest) -> FfiResolvedFont {
+ let lock_failed = "no thread panicked while resolving a font";
+ if let Some(resolved) = self.resolved.read().expect(lock_failed).lookup(request) {
+ return resolved;
+ }
+ let mut resolved = self.resolved.write().expect(lock_failed);
+ if let Some(resolution) = resolved.lookup(request) {
+ return resolution;
+ }
+ self.resolver.refill(&mut resolved, FontRequest::new(request));
+ resolved.lookup(request).expect("a resolved request has its resolution")
+ }
+
+ /// What the pass resolved.
+ fn into_resolved(self) -> FontResolutionCache {
+ self.resolved
+ .into_inner()
+ .expect("no thread panicked while resolving a font")
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/Libraries/LibWeb/Rust/src/css/style/inputs.rs b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
index aa71073d4ff..4346c96e119 100644
--- a/Libraries/LibWeb/Rust/src/css/style/inputs.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
@@ -1566,6 +1566,7 @@ impl StyleEngine {
),
),
font_resolution: None,
+ pass_fonts: None,
computed_group_sets: Default::default(),
style_record_view_epoch_depth: 0,
custom_property_environments: Default::default(),
@@ -2455,6 +2456,7 @@ impl RetainedState {
document_functions: _,
custom_property_registry: _,
font_resolution: _,
+ pass_fonts: _,
monospace_font_family: _,
computed_group_sets,
style_record_view_epoch_depth: _,
diff --git a/Libraries/LibWeb/Rust/src/css/style/mod.rs b/Libraries/LibWeb/Rust/src/css/style/mod.rs
index bfd83dec855..2081d18a2b1 100644
--- a/Libraries/LibWeb/Rust/src/css/style/mod.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/mod.rs
@@ -246,6 +246,8 @@ pub struct RetainedState {
/// Every font resolution this document has been given. An evaluation step reads it; only a
/// host round between passes adds to it.
font_resolution: Option<font_resolution::FontResolutionCache>,
+ /// The fonts the threads of the style pass under way resolve, where they do.
+ pass_fonts: Option<std::sync::Arc<font_resolution::PassFonts>>,
/// `font-family: monospace`, the family the monospace font-size recascade resolves an
/// ancestor's font-relative lengths against.
monospace_font_family: RetainedStyleValueData,
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
index 0f822081a63..7474daf803b 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
@@ -1512,11 +1512,7 @@ impl RetainedState {
inputs,
|| self.font_feature_values_scope(node),
);
- let Some(resolved) = self
- .font_resolution
- .as_ref()
- .and_then(|resolutions| resolutions.lookup(request))
- else {
+ let Some(resolved) = self.resolved_font(request) else {
*suspended_font = Some(font_resolution::FontRequest::new(request));
return Err(Unanswered::Suspended(Suspension::Font));
};
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
index e2910073155..3ded5300f72 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
@@ -244,11 +244,7 @@ impl RetainedState {
font_feature_values_scope: TreeScopeID::DOCUMENT.0,
font_environment_generation: inputs.font_environment_generation,
};
- let Some(resolved) = self
- .font_resolution
- .as_ref()
- .and_then(|resolutions| resolutions.lookup(request))
- else {
+ let Some(resolved) = self.resolved_font(request) else {
return Some(MonospaceRecascade::AwaitsFont(request));
};
// NB: The ancestor's own font is the one being recascaded; its line height is the one it
@@ -758,11 +754,7 @@ impl RetainedState {
|| self.font_feature_values_scope(target.node()),
);
let font_size = request.font_size();
- let Some(resolved) = self
- .font_resolution
- .as_ref()
- .and_then(|resolutions| resolutions.lookup(request))
- else {
+ let Some(resolved) = self.resolved_font(request) else {
let progress = DriveProgress {
root_font_complete: false,
color_scheme_complete: false,
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
index e4154353799..fcf0fed69e7 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
@@ -4374,6 +4374,14 @@ impl StyleEngine {
fn live_pass_rounds(&mut self, root: StyleNodeID, everything: bool) {
let threads = self.retained.live_style.as_ref().map_or(0, |live| live.threads);
let pool = self.style_pool(threads);
+ // NB: A thread that first asks for a font resolves it there and then, under a lock, rather than leave it for a
+ // round between all the threads of the pass, each a barrier.
+ self.retained.pass_fonts = self
+ .host
+ .font_resolver
+ .as_ref()
+ .zip(self.retained.font_resolution.as_ref())
+ .map(|(resolver, cache)| Arc::new(super::super::font_resolution::PassFonts::new(resolver, cache)));
let mut scope = match everything {
true => RestyleScope::Everything,
false => RestyleScope::Pending,
@@ -4402,6 +4410,14 @@ impl StyleEngine {
// that it resolved, as the last pass left it hinted.
scope = RestyleScope::Pending;
}
+ if let Some(pass_fonts) = self.retained.pass_fonts.take().and_then(Arc::into_inner)
+ && let Some(cache) = self.retained.font_resolution.as_mut()
+ {
+ let resolved = cache.take_in_pass_fonts(pass_fonts);
+ self.retained
+ .counters
+ .add(Counter::FontResolutionRequests, resolved as u64);
+ }
if let Some(live) = self.retained.live_style.as_mut() {
self.retained.live_counters.take_pass(&mut live.counts);
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/matched_declarations.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/matched_declarations.rs
index ede376d7fed..17f61e06691 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/matched_declarations.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/matched_declarations.rs
@@ -840,7 +840,7 @@ mod tests {
}
#[test]
- fn elements_with_one_key_wait_on_a_font_once() {
+ fn elements_with_one_key_resolve_a_font_once() {
let (mut engine, nodes) = document_with_children(12);
let sheet = add_sheet(&mut engine, TreeScopeID::DOCUMENT);
add_rules(&mut engine, sheet, ".item { font-size: 13px; }");
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rs
index e729d78979f..65bfb324513 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs
@@ -884,6 +884,15 @@ impl StyleEngine {
}
impl RetainedState {
+ /// The resolution of `request`: the one the cache holds, or, on a thread of a pass that resolves as it goes, one of
+ /// the pass.
+ pub(super) fn resolved_font(&self, request: bridge::FfiFontResolutionRequest) -> Option<bridge::FfiResolvedFont> {
+ self.font_resolution
+ .as_ref()
+ .and_then(|resolutions| resolutions.lookup(request))
+ .or_else(|| self.pass_fonts.as_ref().map(|pass_fonts| pass_fonts.resolve(request)))
+ }
+
/// Resolve `request` through `resolver` into the font resolution cache, between passes or the
/// rounds of one, on the thread that runs them.
pub(super) fn refill_font_request(