Skip to content

Optimize skip_slice by recording serialized length #1836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions webrender_api/src/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,25 @@ fn skip_slice<T: for<'de> Deserialize<'de>>(
data: &mut &[u8],
) -> (ItemRange<T>, usize) {
let base = list.data.as_ptr() as usize;

let byte_size: usize = bincode::deserialize_from(data, bincode::Infinite)
.expect("MEH: malicious input?");
let start = data.as_ptr() as usize;
let item_count: usize = bincode::deserialize_from(data, bincode::Infinite)
.expect("MEH: malicious input?");

// Read through the values (this is a bit of a hack to reuse logic)
let mut iter = AuxIter::<T>::new(*data);
let count = iter.len();
for _ in &mut iter {}
let end = iter.data.as_ptr() as usize;
// Remember how many bytes item_count occupied
let item_count_size = data.as_ptr() as usize - start;

let range = ItemRange {
start: start - base,
length: end - start,
start: start - base, // byte offset to item_count
length: byte_size + item_count_size, // number of bytes for item_count + payload
_boo: PhantomData,
};

// Adjust data pointer to skip read values
*data = &data[range.length ..];
(range, count)
*data = &data[byte_size ..];
(range, item_count)
}

impl<'a> BuiltDisplayListIter<'a> {
Expand Down Expand Up @@ -652,12 +654,29 @@ impl DisplayListBuilder {
let len = iter.len();
let mut count = 0;

// Format:
// payload_byte_size: usize, item_count: usize, [I; item_count]

// We write a dummy value so there's room for later
let byte_size_offset = self.data.len();
serialize_fast(&mut self.data, &0usize);
serialize_fast(&mut self.data, &len);
let payload_offset = self.data.len();

for elem in iter {
count += 1;
serialize_fast(&mut self.data, &elem);
}

// Now write the actual byte_size
let final_offset = self.data.len();
let byte_size = final_offset - payload_offset;

// Note we don't use serialize_fast because we don't want to change the Vec's len
bincode::serialize_into(&mut &mut self.data[byte_size_offset..],
&byte_size,
bincode::Infinite).unwrap();

debug_assert_eq!(len, count);
}

Expand Down