Make Node::is_in_doc O(1)

Added a flags variable inside Node to represent boolean flags, with
is_in_doc being the first of them. It is updated whenever a node is
appended or removed from a parent.

This patch is for:
https://github.com/mozilla/servo/issues/1030
This commit is contained in:
Bruno de Oliveira Abinader 2013-12-17 14:40:23 -04:00
parent 0e14745762
commit cfb73ed123
3 changed files with 50 additions and 2 deletions

View file

@ -91,6 +91,9 @@ pub struct Node<View> {
/// The live list of children return by .childNodes.
child_list: Option<@mut NodeList>,
/// A bitfield of flags for node items.
priv flags: NodeFlags,
/// Layout information. Only the layout task may touch this data.
///
/// FIXME(pcwalton): We need to send these back to the layout task to be destroyed when this
@ -98,6 +101,23 @@ pub struct Node<View> {
layout_data: LayoutDataRef,
}
/// Flags for node items.
pub struct NodeFlags(u8);
impl NodeFlags {
pub fn new(type_id: NodeTypeId) -> NodeFlags {
let mut flags = NodeFlags(0);
match type_id {
DocumentNodeTypeId(_) => { flags.set_is_in_doc(true); }
_ => {}
}
flags
}
}
/// Specifies whether this node is in a document.
bitfield!(NodeFlags, is_in_doc, set_is_in_doc, 0x01)
#[unsafe_destructor]
impl<T> Drop for Node<T> {
fn drop(&mut self) {
@ -546,9 +566,8 @@ impl<'self, View> AbstractNode<View> {
self.node().children()
}
// Issue #1030: should not walk the tree
pub fn is_in_doc(&self) -> bool {
self.ancestors().any(|node| node.is_document())
self.node().flags.is_in_doc()
}
}
@ -649,6 +668,8 @@ impl Node<ScriptView> {
owner_doc: doc,
child_list: None,
flags: NodeFlags::new(type_id),
layout_data: LayoutDataRef::init(),
}
}
@ -1002,6 +1023,7 @@ impl Node<ScriptView> {
// Step 8.
for node in nodes.iter() {
parent.add_child(*node, child);
node.mut_node().flags.set_is_in_doc(parent.is_in_doc());
}
// Step 9.
@ -1080,6 +1102,7 @@ impl Node<ScriptView> {
// Step 6-7: mutation observers.
// Step 8.
parent.remove_child(node);
node.mut_node().flags.set_is_in_doc(false);
// Step 9.
if !suppress_observers {

View file

@ -0,0 +1,22 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#[macro_escape];
macro_rules! bitfield(
($bitfieldname:ident, $getter:ident, $setter:ident, $value:expr) => (
impl $bitfieldname {
#[inline]
pub fn $getter(self) -> bool {
(*self & $value) != 0
}
#[inline]
pub fn $setter(&mut self, value: bool) {
*self = $bitfieldname((**self & !$value) | (if value { $value } else { 0 }))
}
}
)
)

View file

@ -23,6 +23,9 @@ extern mod style;
extern mod servo_msg (name = "msg");
extern mod extra;
// Macros
mod macros;
pub mod dom {
pub mod bindings {
pub mod element;