Skip to content

Commit

Permalink
script: Don't create a temporary vector on the heap when inserting
Browse files Browse the repository at this point in the history
non-fragment nodes.
  • Loading branch information
pcwalton committed Oct 28, 2014
1 parent 6a7a96a commit 8ab354a
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions components/script/dom/node.rs
Expand Up @@ -1352,29 +1352,8 @@ impl Node {
parent: JSRef<Node>,
child: Option<JSRef<Node>>,
suppress_observers: SuppressObserver) {
// XXX assert owner_doc
// Step 1-3: ranges.
// Step 4.
let mut nodes = match node.type_id() {
DocumentFragmentNodeTypeId => node.children().collect(),
_ => vec!(node.clone()),
};

// Step 5: DocumentFragment, mutation records.
// Step 6: DocumentFragment.
match node.type_id() {
DocumentFragmentNodeTypeId => {
for c in node.children() {
Node::remove(c, node, Suppressed);
}
},
_ => (),
}

// Step 7: mutation records.
// Step 8.
for node in nodes.iter_mut() {
parent.add_child(*node, child);
fn do_insert(node: JSRef<Node>, parent: JSRef<Node>, child: Option<JSRef<Node>>) {
parent.add_child(node, child);
let is_in_doc = parent.is_in_doc();
for kid in node.traverse_preorder() {
let mut flags = kid.flags.get();
Expand All @@ -1387,14 +1366,45 @@ impl Node {
}
}

// Step 9.
match suppress_observers {
Unsuppressed => {
for node in nodes.iter() {
node.node_inserted();
fn fire_observer_if_necessary(node: JSRef<Node>, suppress_observers: SuppressObserver) {
match suppress_observers {
Unsuppressed => node.node_inserted(),
Suppressed => ()
}
}

// XXX assert owner_doc
// Step 1-3: ranges.

match node.type_id() {
DocumentFragmentNodeTypeId => {
// Step 4.
// Step 5: DocumentFragment, mutation records.
// Step 6: DocumentFragment.
for c in node.children() {
Node::remove(c, node, Suppressed);
}

// Step 7: mutation records.
// Step 8.
for node in node.children() {
do_insert(node, parent, child);
}

for node in node.children() {
fire_observer_if_necessary(node, suppress_observers);
}
}
Suppressed => ()
_ => {
// Step 4.
// Step 5: DocumentFragment, mutation records.
// Step 6: DocumentFragment.
// Step 7: mutation records.
// Step 8.
do_insert(node, parent, child);
// Step 9.
fire_observer_if_necessary(node, suppress_observers);
}
}
}

Expand Down

0 comments on commit 8ab354a

Please sign in to comment.