Skip to content

Commit 46872c8

Browse files
authored
Merge pull request #283 from conradludgate/improve-debug-performance
chore: improve debug performance
2 parents 500a314 + cc1acaa commit 46872c8

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/arena_tree.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,26 @@ where
3939
T: fmt::Debug,
4040
{
4141
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
42-
// FIXME: would be better not to build a vector for the children but I
43-
// can't presently figure out the borrowing to use the debug_list API
44-
let mut children = vec![];
45-
let mut child = self.first_child.get();
46-
while let Some(inner_child) = child {
47-
children.push(inner_child);
48-
child = inner_child.next_sibling.get();
42+
struct Children<'a, T>(Option<&'a Node<'a, T>>);
43+
impl<T: fmt::Debug> fmt::Debug for Children<'_, T> {
44+
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
45+
f.debug_list()
46+
.entries(std::iter::successors(self.0, |child| {
47+
child.next_sibling.get()
48+
}))
49+
.finish()
50+
}
4951
}
5052

5153
let mut struct_fmt = f.debug_struct("Node");
5254
struct_fmt.field("data", &self.data);
53-
struct_fmt.field("children", &children);
55+
struct_fmt.field("children", &Children(self.first_child.get()));
5456
struct_fmt.finish()?;
5557

5658
Ok(())
5759
}
5860
}
5961

60-
fn same_ref<T>(a: &T, b: &T) -> bool {
61-
let a: *const T = a;
62-
let b: *const T = b;
63-
a == b
64-
}
65-
6662
impl<'a, T> Node<'a, T> {
6763
/// Create a new node from its associated data.
6864
///
@@ -106,7 +102,7 @@ impl<'a, T> Node<'a, T> {
106102

107103
/// Returns whether two references point to the same node.
108104
pub fn same_node(&self, other: &Node<'a, T>) -> bool {
109-
same_ref(self, other)
105+
std::ptr::eq(self, other)
110106
}
111107

112108
/// Return an iterator of references to this node and its ancestors.
@@ -219,11 +215,14 @@ impl<'a, T> Node<'a, T> {
219215
new_sibling.parent.set(self.parent.get());
220216
new_sibling.previous_sibling.set(Some(self));
221217
if let Some(next_sibling) = self.next_sibling.take() {
222-
debug_assert!(same_ref(next_sibling.previous_sibling.get().unwrap(), self));
218+
debug_assert!(std::ptr::eq(
219+
next_sibling.previous_sibling.get().unwrap(),
220+
self
221+
));
223222
next_sibling.previous_sibling.set(Some(new_sibling));
224223
new_sibling.next_sibling.set(Some(next_sibling));
225224
} else if let Some(parent) = self.parent.get() {
226-
debug_assert!(same_ref(parent.last_child.get().unwrap(), self));
225+
debug_assert!(std::ptr::eq(parent.last_child.get().unwrap(), self));
227226
parent.last_child.set(Some(new_sibling));
228227
}
229228
self.next_sibling.set(Some(new_sibling));
@@ -236,10 +235,13 @@ impl<'a, T> Node<'a, T> {
236235
new_sibling.next_sibling.set(Some(self));
237236
if let Some(previous_sibling) = self.previous_sibling.take() {
238237
new_sibling.previous_sibling.set(Some(previous_sibling));
239-
debug_assert!(same_ref(previous_sibling.next_sibling.get().unwrap(), self));
238+
debug_assert!(std::ptr::eq(
239+
previous_sibling.next_sibling.get().unwrap(),
240+
self
241+
));
240242
previous_sibling.next_sibling.set(Some(new_sibling));
241243
} else if let Some(parent) = self.parent.get() {
242-
debug_assert!(same_ref(parent.first_child.get().unwrap(), self));
244+
debug_assert!(std::ptr::eq(parent.first_child.get().unwrap(), self));
243245
parent.first_child.set(Some(new_sibling));
244246
}
245247
self.previous_sibling.set(Some(new_sibling));

0 commit comments

Comments
 (0)