diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs
index 8746a30a7e0..4885d87c64b 100644
--- a/components/script/dom/node.rs
+++ b/components/script/dom/node.rs
@@ -152,8 +152,6 @@ pub struct Node {
/// Must be sent back to the layout thread to be destroyed when this
/// node is finalized.
style_and_layout_data: Cell>,
-
- unique_id: UniqueId,
}
bitflags! {
@@ -1006,7 +1004,20 @@ impl Node {
}
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 {
@@ -1654,7 +1665,7 @@ impl Node {
#[allow(unrooted_must_root)]
fn new_(flags: NodeFlags, doc: Option<&Document>) -> Node {
- let node = Node {
+ Node {
eventtarget: EventTarget::new_inherited(),
parent_node: Default::default(),
@@ -1671,13 +1682,7 @@ impl Node {
ranges: WeakRangeVec::new(),
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
@@ -3122,7 +3127,7 @@ impl<'a> UnbindContext<'a> {
}
/// A node's unique ID, for devtools.
-struct UniqueId {
+pub struct UniqueId {
cell: UnsafeCell >>,
}
diff --git a/components/script/dom/raredata.rs b/components/script/dom/raredata.rs
index 2372b0b89ab..8c481425982 100644
--- a/components/script/dom/raredata.rs
+++ b/components/script/dom/raredata.rs
@@ -7,6 +7,7 @@ use crate::dom::customelementregistry::{
CustomElementDefinition, CustomElementReaction, CustomElementState,
};
use crate::dom::mutationobserver::RegisteredObserver;
+use crate::dom::node::UniqueId;
use crate::dom::shadowroot::ShadowRoot;
use std::rc::Rc;
@@ -22,6 +23,8 @@ pub struct NodeRareData {
pub containing_shadow_root: Option>,
/// Registered observers for this node.
pub mutation_observers: Vec,
+ /// Lazily-generated Unique Id for this node.
+ pub unique_id: Option,
}
#[derive(Default, JSTraceable, MallocSizeOf)]
diff --git a/tests/unit/script/size_of.rs b/tests/unit/script/size_of.rs
index 1c5bcace945..4ab28494e06 100644
--- a/tests/unit/script/size_of.rs
+++ b/tests/unit/script/size_of.rs
@@ -30,10 +30,10 @@ macro_rules! sizeof_checker (
// Update the sizes here
sizeof_checker!(size_event_target, EventTarget, 56);
-sizeof_checker!(size_node, Node, 184);
-sizeof_checker!(size_element, Element, 392);
-sizeof_checker!(size_htmlelement, HTMLElement, 408);
-sizeof_checker!(size_div, HTMLDivElement, 408);
-sizeof_checker!(size_span, HTMLSpanElement, 408);
-sizeof_checker!(size_text, Text, 216);
-sizeof_checker!(size_characterdata, CharacterData, 216);
+sizeof_checker!(size_node, Node, 176);
+sizeof_checker!(size_element, Element, 384);
+sizeof_checker!(size_htmlelement, HTMLElement, 400);
+sizeof_checker!(size_div, HTMLDivElement, 400);
+sizeof_checker!(size_span, HTMLSpanElement, 400);
+sizeof_checker!(size_text, Text, 208);
+sizeof_checker!(size_characterdata, CharacterData, 208);