servo: Merge #10983 - Minor reduction in the amount of allocation display list building does (from notriddle:no_alloc_sort_display); r=mbrubeck
authorMichael Howell <michael@notriddle.com>
Tue, 03 May 2016 23:51:53 -0700
changeset 338703 523067d92392e8e0a6644cb83e68a1307f5fba9b
parent 338702 91857bb893c194954f49d2c13dd2eb747cb41133
child 338704 36634609ea90b79d84ab07a959df7644a8f40d37
push id31307
push usergszorc@mozilla.com
push dateSat, 04 Feb 2017 00:59:06 +0000
treeherdermozilla-central@94079d43835f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmbrubeck
servo: Merge #10983 - Minor reduction in the amount of allocation display list building does (from notriddle:no_alloc_sort_display); r=mbrubeck Use `mem::replace` to perform the lifetime-trick without allocating a whole new buffer. (An older of this switched from the built-in heapsort to a non-allocating introsort. Unfortunately, introsort is not a stable sorting algorithm, and the display list system relies on it being stable.) Source-Repo: https://github.com/servo/servo Source-Revision: 2e849b7064bdb30beedc4af13033b3f6a407f4b1
servo/components/gfx/display_list/mod.rs
--- a/servo/components/gfx/display_list/mod.rs
+++ b/servo/components/gfx/display_list/mod.rs
@@ -32,16 +32,17 @@ use range::Range;
 use serde::de::{self, Deserialize, Deserializer, MapVisitor, Visitor};
 use serde::ser::impls::MapIteratorVisitor;
 use serde::ser::{Serialize, Serializer};
 use std::cmp::{self, Ordering};
 use std::collections::HashMap;
 use std::fmt;
 use std::hash::{BuildHasherDefault, Hash};
 use std::marker::PhantomData;
+use std::mem;
 use std::ops::{Deref, DerefMut};
 use std::sync::Arc;
 use style::computed_values::{border_style, filter, image_rendering, mix_blend_mode};
 use style::properties::{ComputedValues};
 use style_traits::cursor::Cursor;
 use text::TextRun;
 use text::glyph::ByteIndex;
 use util::geometry::{self, MAX_RECT, ScreenPx};
@@ -241,27 +242,26 @@ impl DisplayList {
             DisplayListSection::BlockBackgroundsAndBorders =>
                 offsets.block_backgrounds_and_borders,
             DisplayListSection::Content => offsets.content,
             DisplayListSection::Outlines => offsets.outlines,
         }
     }
 
     fn sort(&mut self) {
-        let mut list = Vec::new();
-        list.append(&mut self.list);
+        let mut list = mem::replace(&mut self.list, Vec::new());
 
         list.sort_by(|a, b| {
             if a.base().stacking_context_id == b.base().stacking_context_id {
                 return a.base().section.cmp(&b.base().section);
             }
             self.get_offset_for_item(a).cmp(&self.get_offset_for_item(b))
         });
 
-        self.list.append(&mut list);
+        mem::replace(&mut self.list, list);
     }
 
     pub fn print(&self) {
         let mut print_tree = PrintTree::new("Display List".to_owned());
         self.print_with_tree(&mut print_tree);
     }
 
     fn sort_and_count_stacking_contexts(