mirror of
https://github.com/servo/servo.git
synced 2025-08-05 21:50:18 +01:00
script: Cache the <iframe>
list per-Document (#34702)
This change creates a new struct `IFrameCollection` that is used to cache the list of `<iframe>`s in a `Document` as long as the `Document`'s DOM has not changed. This prevent constantly iterating the entire DOM during *update the rendering*, which runs up to 60 times per second as well as for other operations. Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
parent
adfee3daa5
commit
a5c461146f
7 changed files with 225 additions and 107 deletions
|
@ -15,7 +15,6 @@ use std::sync::{LazyLock, Mutex};
|
|||
use std::time::{Duration, Instant};
|
||||
|
||||
use base::cross_process_instant::CrossProcessInstant;
|
||||
use base::id::BrowsingContextId;
|
||||
use canvas_traits::webgl::{self, WebGLContextId, WebGLMsg};
|
||||
use chrono::Local;
|
||||
use content_security_policy::{self as csp, CspList};
|
||||
|
@ -185,6 +184,7 @@ use crate::dom::window::Window;
|
|||
use crate::dom::windowproxy::WindowProxy;
|
||||
use crate::dom::xpathevaluator::XPathEvaluator;
|
||||
use crate::fetch::FetchCanceller;
|
||||
use crate::iframe_collection::IFrameCollection;
|
||||
use crate::network_listener::{NetworkListener, PreInvoke};
|
||||
use crate::realms::{enter_realm, AlreadyInRealm, InRealm};
|
||||
use crate::script_runtime::{CanGc, CommonScriptMsg, ScriptThreadEventCategory};
|
||||
|
@ -294,6 +294,8 @@ pub struct Document {
|
|||
scripts: MutNullableDom<HTMLCollection>,
|
||||
anchors: MutNullableDom<HTMLCollection>,
|
||||
applets: MutNullableDom<HTMLCollection>,
|
||||
/// Information about the `<iframes>` in this [`Document`].
|
||||
iframes: RefCell<IFrameCollection>,
|
||||
/// Lock use for style attributes and author-origin stylesheet objects in this document.
|
||||
/// Can be acquired once for accessing many objects.
|
||||
#[no_trace]
|
||||
|
@ -2251,9 +2253,12 @@ impl Document {
|
|||
}
|
||||
// Step 9
|
||||
if !recursive_flag {
|
||||
for iframe in self.iter_iframes() {
|
||||
// `prompt_to_unload` might cause futher modifications to the DOM so collecting here prevents
|
||||
// a double borrow if the `IFrameCollection` needs to be validated again.
|
||||
let iframes: Vec<_> = self.iframes().iter().collect();
|
||||
for iframe in &iframes {
|
||||
// TODO: handle the case of cross origin iframes.
|
||||
let document = document_from_node(&*iframe);
|
||||
let document = document_from_node(&**iframe);
|
||||
can_unload = document.prompt_to_unload(true, can_gc);
|
||||
if !document.salvageable() {
|
||||
self.salvageable.set(false);
|
||||
|
@ -2320,9 +2325,12 @@ impl Document {
|
|||
|
||||
// Step 13
|
||||
if !recursive_flag {
|
||||
for iframe in self.iter_iframes() {
|
||||
// `unload` might cause futher modifications to the DOM so collecting here prevents
|
||||
// a double borrow if the `IFrameCollection` needs to be validated again.
|
||||
let iframes: Vec<_> = self.iframes().iter().collect();
|
||||
for iframe in &iframes {
|
||||
// TODO: handle the case of cross origin iframes.
|
||||
let document = document_from_node(&*iframe);
|
||||
let document = document_from_node(&**iframe);
|
||||
document.unload(true, can_gc);
|
||||
if !document.salvageable() {
|
||||
self.salvageable.set(false);
|
||||
|
@ -2677,7 +2685,7 @@ impl Document {
|
|||
self.loader.borrow_mut().inhibit_events();
|
||||
|
||||
// Step 1.
|
||||
for iframe in self.iter_iframes() {
|
||||
for iframe in self.iframes().iter() {
|
||||
if let Some(document) = iframe.GetContentDocument() {
|
||||
// TODO: abort the active documents of every child browsing context.
|
||||
document.abort(can_gc);
|
||||
|
@ -2726,20 +2734,18 @@ impl Document {
|
|||
self.current_parser.get()
|
||||
}
|
||||
|
||||
/// Iterate over all iframes in the document.
|
||||
pub fn iter_iframes(&self) -> impl Iterator<Item = DomRoot<HTMLIFrameElement>> {
|
||||
self.upcast::<Node>()
|
||||
.traverse_preorder(ShadowIncluding::Yes)
|
||||
.filter_map(DomRoot::downcast::<HTMLIFrameElement>)
|
||||
/// A reference to the [`IFrameCollection`] of this [`Document`], holding information about
|
||||
/// `<iframe>`s found within it.
|
||||
pub(crate) fn iframes(&self) -> Ref<IFrameCollection> {
|
||||
self.iframes.borrow_mut().validate(self);
|
||||
self.iframes.borrow()
|
||||
}
|
||||
|
||||
/// Find an iframe element in the document.
|
||||
pub fn find_iframe(
|
||||
&self,
|
||||
browsing_context_id: BrowsingContextId,
|
||||
) -> Option<DomRoot<HTMLIFrameElement>> {
|
||||
self.iter_iframes()
|
||||
.find(|node| node.browsing_context_id() == Some(browsing_context_id))
|
||||
/// A mutable reference to the [`IFrameCollection`] of this [`Document`], holding information about
|
||||
/// `<iframe>`s found within it.
|
||||
pub(crate) fn iframes_mut(&self) -> RefMut<IFrameCollection> {
|
||||
self.iframes.borrow_mut().validate(self);
|
||||
self.iframes.borrow_mut()
|
||||
}
|
||||
|
||||
pub fn get_dom_interactive(&self) -> Option<CrossProcessInstant> {
|
||||
|
@ -3266,6 +3272,7 @@ impl Document {
|
|||
scripts: Default::default(),
|
||||
anchors: Default::default(),
|
||||
applets: Default::default(),
|
||||
iframes: Default::default(),
|
||||
style_shared_lock: {
|
||||
/// Per-process shared lock for author-origin stylesheets
|
||||
///
|
||||
|
|
|
@ -804,7 +804,7 @@ impl VirtualMethods for HTMLIFrameElement {
|
|||
);
|
||||
let exited_window = exited_document.window();
|
||||
exited_window.discard_browsing_context();
|
||||
for exited_iframe in exited_document.iter_iframes() {
|
||||
for exited_iframe in exited_document.iframes().iter() {
|
||||
debug!("Discarding nested browsing context");
|
||||
exited_iframe.destroy_nested_browsing_context();
|
||||
}
|
||||
|
|
|
@ -51,14 +51,14 @@ use profile_traits::ipc as ProfiledIpc;
|
|||
use profile_traits::mem::ProfilerChan as MemProfilerChan;
|
||||
use profile_traits::time::ProfilerChan as TimeProfilerChan;
|
||||
use script_layout_interface::{
|
||||
combine_id_with_fragment_type, FragmentType, IFrameSizes, Layout, PendingImageState, QueryMsg,
|
||||
Reflow, ReflowGoal, ReflowRequest, TrustedNodeAddress,
|
||||
combine_id_with_fragment_type, FragmentType, Layout, PendingImageState, QueryMsg, Reflow,
|
||||
ReflowGoal, ReflowRequest, TrustedNodeAddress,
|
||||
};
|
||||
use script_traits::webdriver_msg::{WebDriverJSError, WebDriverJSResult};
|
||||
use script_traits::{
|
||||
ConstellationControlMsg, DocumentState, IFrameSizeMsg, LoadData, NavigationHistoryBehavior,
|
||||
ScriptMsg, ScriptToConstellationChan, ScrollState, StructuredSerializedData, Theme,
|
||||
TimerSchedulerMsg, WindowSizeData, WindowSizeType,
|
||||
ConstellationControlMsg, DocumentState, LoadData, NavigationHistoryBehavior, ScriptMsg,
|
||||
ScriptToConstellationChan, ScrollState, StructuredSerializedData, Theme, TimerSchedulerMsg,
|
||||
WindowSizeData, WindowSizeType,
|
||||
};
|
||||
use selectors::attr::CaseSensitivity;
|
||||
use servo_arc::Arc as ServoArc;
|
||||
|
@ -151,7 +151,7 @@ use crate::script_runtime::{
|
|||
CanGc, CommonScriptMsg, JSContext, Runtime, ScriptChan, ScriptPort, ScriptThreadEventCategory,
|
||||
};
|
||||
use crate::script_thread::{
|
||||
with_script_thread, ImageCacheMsg, MainThreadScriptChan, MainThreadScriptMsg, ScriptThread,
|
||||
ImageCacheMsg, MainThreadScriptChan, MainThreadScriptMsg, ScriptThread,
|
||||
SendableMainThreadScriptChan,
|
||||
};
|
||||
use crate::task_manager::TaskManager;
|
||||
|
@ -377,17 +377,6 @@ pub struct Window {
|
|||
|
||||
/// <https://dom.spec.whatwg.org/#window-current-event>
|
||||
current_event: DomRefCell<Option<Dom<Event>>>,
|
||||
|
||||
/// Sizes of the various `<iframes>` that we might have on this [`Window`].
|
||||
/// This is used to:
|
||||
/// - Let same-`ScriptThread` `<iframe>`s know synchronously when their
|
||||
/// size has changed, ensuring that the next layout has the right size.
|
||||
/// - Send the proper size for the `<iframe>` during new-Pipeline creation
|
||||
/// when the `src` attribute changes.
|
||||
/// - Let the `Constellation` know about `BrowsingContext` (one per `<iframe>`)
|
||||
/// size changes when an `<iframe>` changes size during layout.
|
||||
#[no_trace]
|
||||
iframe_sizes: RefCell<IFrameSizes>,
|
||||
}
|
||||
|
||||
impl Window {
|
||||
|
@ -997,8 +986,7 @@ impl WindowMethods<crate::DomTypeHolder> for Window {
|
|||
|
||||
// https://html.spec.whatwg.org/multipage/#accessing-other-browsing-contexts
|
||||
fn Length(&self) -> u32 {
|
||||
let doc = self.Document();
|
||||
doc.iter_iframes().count() as u32
|
||||
self.Document().iframes().iter().count() as u32
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-parent
|
||||
|
@ -1447,7 +1435,8 @@ impl WindowMethods<crate::DomTypeHolder> for Window {
|
|||
|
||||
// https://html.spec.whatwg.org/multipage/#document-tree-child-browsing-context-name-property-set
|
||||
let iframes: Vec<_> = document
|
||||
.iter_iframes()
|
||||
.iframes()
|
||||
.iter()
|
||||
.filter(|iframe| {
|
||||
if let Some(window) = iframe.GetContentWindow() {
|
||||
return window.get_name() == name;
|
||||
|
@ -1967,7 +1956,13 @@ impl Window {
|
|||
}
|
||||
}
|
||||
|
||||
self.handle_new_iframe_sizes_after_layout(results.iframe_sizes);
|
||||
let size_messages = self
|
||||
.Document()
|
||||
.iframes_mut()
|
||||
.handle_new_iframe_sizes_after_layout(results.iframe_sizes, self.device_pixel_ratio());
|
||||
if !size_messages.is_empty() {
|
||||
self.send_to_constellation(ScriptMsg::IFrameSizes(size_messages));
|
||||
}
|
||||
|
||||
document.update_animations_post_reflow();
|
||||
self.update_constellation_epoch();
|
||||
|
@ -1975,58 +1970,6 @@ impl Window {
|
|||
true
|
||||
}
|
||||
|
||||
/// Update the recorded iframe sizes of the contents of layout. When these sizes change,
|
||||
/// send a message to the `Constellation` informing it of the new sizes.
|
||||
fn handle_new_iframe_sizes_after_layout(&self, new_iframe_sizes: IFrameSizes) {
|
||||
let old_iframe_sizes = self.iframe_sizes.replace(new_iframe_sizes);
|
||||
let new_iframe_sizes = self.iframe_sizes.borrow();
|
||||
if new_iframe_sizes.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Batch resize message to any local `Pipeline`s now, rather than waiting for them
|
||||
// to filter asynchronously through the `Constellation`. This allows the new value
|
||||
// to be reflected immediately in layout.
|
||||
let device_pixel_ratio = self.device_pixel_ratio();
|
||||
with_script_thread(|script_thread| {
|
||||
for iframe_size in new_iframe_sizes.values() {
|
||||
script_thread.handle_resize_message(
|
||||
iframe_size.pipeline_id,
|
||||
WindowSizeData {
|
||||
initial_viewport: iframe_size.size,
|
||||
device_pixel_ratio,
|
||||
},
|
||||
// TODO: This might send an extra resize event. This can fixed by explicitly
|
||||
// supporting `<iframe>` creation when the size isn't known yet.
|
||||
WindowSizeType::Resize,
|
||||
);
|
||||
}
|
||||
});
|
||||
// Send asynchronous updates to `Constellation.`
|
||||
let size_messages: Vec<_> = new_iframe_sizes
|
||||
.iter()
|
||||
.filter_map(|(browsing_context_id, size)| {
|
||||
match old_iframe_sizes.get(browsing_context_id) {
|
||||
Some(old_size) if old_size.size == size.size => None,
|
||||
Some(..) => Some(IFrameSizeMsg {
|
||||
browsing_context_id: *browsing_context_id,
|
||||
size: size.size,
|
||||
type_: WindowSizeType::Resize,
|
||||
}),
|
||||
None => Some(IFrameSizeMsg {
|
||||
browsing_context_id: *browsing_context_id,
|
||||
size: size.size,
|
||||
type_: WindowSizeType::Initial,
|
||||
}),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
if !size_messages.is_empty() {
|
||||
self.send_to_constellation(ScriptMsg::IFrameSizes(size_messages));
|
||||
}
|
||||
}
|
||||
|
||||
/// Reflows the page if it's possible to do so and the page is dirty. Returns true if layout
|
||||
/// actually happened, false otherwise.
|
||||
///
|
||||
|
@ -2327,10 +2270,10 @@ impl Window {
|
|||
) -> Option<Size2D<f32, CSSPixel>> {
|
||||
// Reflow might fail, but do a best effort to return the right size.
|
||||
self.layout_reflow(QueryMsg::InnerWindowDimensionsQuery, can_gc);
|
||||
self.iframe_sizes
|
||||
.borrow()
|
||||
.get(&browsing_context_id)
|
||||
.map(|iframe_size| iframe_size.size)
|
||||
self.Document()
|
||||
.iframes()
|
||||
.get(browsing_context_id)
|
||||
.and_then(|iframe| iframe.size)
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
|
@ -2865,7 +2808,6 @@ impl Window {
|
|||
layout_marker: DomRefCell::new(Rc::new(Cell::new(true))),
|
||||
current_event: DomRefCell::new(None),
|
||||
theme: Cell::new(PrefersColorScheme::Light),
|
||||
iframe_sizes: RefCell::default(),
|
||||
});
|
||||
|
||||
unsafe { WindowBinding::Wrap(JSContext::from_ptr(runtime.cx()), win) }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue