ISSUE-23995: lazily generate unique_id for node

This commit is contained in:
Thomas Delacour 2019-09-01 19:43:35 -04:00 committed by Thomas Delacour
parent 4fe8238b14
commit d0c64d347d
No known key found for this signature in database
GPG key ID: CA2E572D77CFF40A
3 changed files with 27 additions and 19 deletions

View file

@ -152,8 +152,6 @@ pub struct Node {
/// Must be sent back to the layout thread to be destroyed when this /// Must be sent back to the layout thread to be destroyed when this
/// node is finalized. /// node is finalized.
style_and_layout_data: Cell<Option<OpaqueStyleAndLayoutData>>, style_and_layout_data: Cell<Option<OpaqueStyleAndLayoutData>>,
unique_id: UniqueId,
} }
bitflags! { bitflags! {
@ -1006,7 +1004,20 @@ impl Node {
} }
pub fn unique_id(&self) -> String { pub fn unique_id(&self) -> String {
self.unique_id.borrow().to_simple().to_string() let mut rare_data = self.ensure_rare_data();
if rare_data.unique_id.is_none() {
let id = UniqueId::new();
ScriptThread::save_node_id(id.borrow().to_simple().to_string());
rare_data.unique_id = Some(id);
}
rare_data
.unique_id
.as_ref()
.unwrap()
.borrow()
.to_simple()
.to_string()
} }
pub fn summarize(&self) -> NodeInfo { pub fn summarize(&self) -> NodeInfo {
@ -1654,7 +1665,7 @@ impl Node {
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
fn new_(flags: NodeFlags, doc: Option<&Document>) -> Node { fn new_(flags: NodeFlags, doc: Option<&Document>) -> Node {
let node = Node { Node {
eventtarget: EventTarget::new_inherited(), eventtarget: EventTarget::new_inherited(),
parent_node: Default::default(), parent_node: Default::default(),
@ -1671,13 +1682,7 @@ impl Node {
ranges: WeakRangeVec::new(), ranges: WeakRangeVec::new(),
style_and_layout_data: Cell::new(None), style_and_layout_data: Cell::new(None),
}
unique_id: UniqueId::new(),
};
ScriptThread::save_node_id(node.unique_id());
node
} }
// https://dom.spec.whatwg.org/#concept-node-adopt // https://dom.spec.whatwg.org/#concept-node-adopt
@ -3122,7 +3127,7 @@ impl<'a> UnbindContext<'a> {
} }
/// A node's unique ID, for devtools. /// A node's unique ID, for devtools.
struct UniqueId { pub struct UniqueId {
cell: UnsafeCell<Option<Box<Uuid>>>, cell: UnsafeCell<Option<Box<Uuid>>>,
} }

View file

@ -7,6 +7,7 @@ use crate::dom::customelementregistry::{
CustomElementDefinition, CustomElementReaction, CustomElementState, CustomElementDefinition, CustomElementReaction, CustomElementState,
}; };
use crate::dom::mutationobserver::RegisteredObserver; use crate::dom::mutationobserver::RegisteredObserver;
use crate::dom::node::UniqueId;
use crate::dom::shadowroot::ShadowRoot; use crate::dom::shadowroot::ShadowRoot;
use std::rc::Rc; use std::rc::Rc;
@ -22,6 +23,8 @@ pub struct NodeRareData {
pub containing_shadow_root: Option<Dom<ShadowRoot>>, pub containing_shadow_root: Option<Dom<ShadowRoot>>,
/// Registered observers for this node. /// Registered observers for this node.
pub mutation_observers: Vec<RegisteredObserver>, pub mutation_observers: Vec<RegisteredObserver>,
/// Lazily-generated Unique Id for this node.
pub unique_id: Option<UniqueId>,
} }
#[derive(Default, JSTraceable, MallocSizeOf)] #[derive(Default, JSTraceable, MallocSizeOf)]

View file

@ -30,10 +30,10 @@ macro_rules! sizeof_checker (
// Update the sizes here // Update the sizes here
sizeof_checker!(size_event_target, EventTarget, 56); sizeof_checker!(size_event_target, EventTarget, 56);
sizeof_checker!(size_node, Node, 184); sizeof_checker!(size_node, Node, 176);
sizeof_checker!(size_element, Element, 392); sizeof_checker!(size_element, Element, 384);
sizeof_checker!(size_htmlelement, HTMLElement, 408); sizeof_checker!(size_htmlelement, HTMLElement, 400);
sizeof_checker!(size_div, HTMLDivElement, 408); sizeof_checker!(size_div, HTMLDivElement, 400);
sizeof_checker!(size_span, HTMLSpanElement, 408); sizeof_checker!(size_span, HTMLSpanElement, 400);
sizeof_checker!(size_text, Text, 216); sizeof_checker!(size_text, Text, 208);
sizeof_checker!(size_characterdata, CharacterData, 216); sizeof_checker!(size_characterdata, CharacterData, 208);