Auto merge of #24217 - tdelacour:ISSUE-23995, r=jdm

ISSUE-23995: lazily generate UniqueIds for Nodes

<!-- Please describe your changes on the following line: -->
Updated `Node` so that its `unique_id` is only generated when `unique_id()` is called. This getter now also has assumed the responsibility for saving the `id` to the global hashset on `ScriptThread`.

Felt like I was fighting the borrow-checker a little bit on this one. Definitely a good learning experience for me, but the result strikes me as a little messy?

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #23995 (GitHub issue number if applicable)

<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/24217)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2019-09-24 13:40:38 -04:00 committed by GitHub
commit 5ab2d4ea83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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
/// node is finalized.
style_and_layout_data: Cell<Option<OpaqueStyleAndLayoutData>>,
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<Option<Box<Uuid>>>,
}

View file

@ -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<Dom<ShadowRoot>>,
/// Registered observers for this node.
pub mutation_observers: Vec<RegisteredObserver>,
/// Lazily-generated Unique Id for this node.
pub unique_id: Option<UniqueId>,
}
#[derive(Default, JSTraceable, MallocSizeOf)]