Skip to content
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

Use an unsafe serializer. #1799

Merged
merged 1 commit into from
Oct 4, 2017
Merged

Conversation

jrmuizel
Copy link
Collaborator

@jrmuizel jrmuizel commented Oct 3, 2017

This gives a noticable impact on serialization performance in Gecko.
wr_dp_push_text() goes from 35.6% of
nsDisplayText::CreateWebRenderCommands down to 24%.

The generated code is still pretty bad but hopefully adding
proper noalias information to rust will fix that.


This change is Reviewable

@glennw
Copy link
Member

glennw commented Oct 3, 2017

r? @gankro

Copy link

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done!

Please document more clearly what makes this unsafe. By using this implementation, we are trusting all of the Serialize impls that it ever serializes. I can break the trust with a Serialize like this:

struct S {
    first: Cell<bool>,
}

impl Serialize for S {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer
    {
        if self.first.get() {
            self.first.set(false);
            ().serialize(serializer)
        } else {
            0.serialize(serializer)
        }
    }
}

@Gankra
Copy link
Contributor

Gankra commented Oct 3, 2017

I'm currently building a rustc with the noalias flag we discussed, would like to hold off on merging that until we've looked at the codegen of the 4 combos of alias x unsafe

Copy link
Contributor

@Gankra Gankra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impl otherwise seems fine, with mine and dtolnay's requested doc fixes.

@@ -483,6 +485,48 @@ impl<'a, 'b> Serialize for DisplayItemRef<'a, 'b> {
}
}

// This is a replacement for bincode::serialize_into(&vec)
// The default implementation Write for Vec will basically
// call extend_from_slice(). Serde ends up calling that for ever
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/ever/every

@Gankra
Copy link
Contributor

Gankra commented Oct 4, 2017

Some codgen tests here. Looks like this is an all around win, and even positions us for bigger wins with mutable noalias:

rust-lang/rust#45012 (comment)

@Gankra
Copy link
Contributor

Gankra commented Oct 4, 2017

@bors-servo r+

@bors-servo
Copy link
Contributor

@gankro: 🔑 Insufficient privileges: Not in reviewers

This gives a noticable impact on serialization performance in Gecko.
wr_dp_push_text() goes from 35.6% of
nsDisplayText::CreateWebRenderCommands down to 24%.

The generated code is still pretty bad but hopefully adding
proper noalias information to rust will fix that.
fn serialize_fast<T: Serialize>(vec: &mut Vec<u8>, e: &T) {
// manually counting the size is faster than vec.reserve(bincode::serialized_size(&e) as usize) for some reason
let mut size = SizeCounter(0);
bincode::serialize_into(&mut size,e , bincode::Infinite).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing space before e

unsafe {
let old_len = self.0.len();
self.0.set_len(old_len + buf.len());
ptr::copy_nonoverlapping(buf.as_ptr(), self.0.as_mut_ptr().offset(old_len as isize), buf.len());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need some debug assertions for the capacity guarantee

@kvark
Copy link
Member

kvark commented Oct 4, 2017

@bors-servo r=Gankro,kvark

@bors-servo
Copy link
Contributor

📌 Commit 214a48c has been approved by Gankro,kvark

@bors-servo
Copy link
Contributor

⌛ Testing commit 214a48c with merge 29d325f...

bors-servo pushed a commit that referenced this pull request Oct 4, 2017
Use an unsafe serializer.

This gives a noticable impact on serialization performance in Gecko.
wr_dp_push_text() goes from 35.6% of
nsDisplayText::CreateWebRenderCommands down to 24%.

The generated code is still pretty bad but hopefully adding
proper noalias information to rust will fix that.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/webrender/1799)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

☀️ Test successful - status-appveyor, status-travis
Approved by: Gankro,kvark
Pushing 29d325f to master...

@bors-servo bors-servo merged commit 214a48c into servo:master Oct 4, 2017
glennw pushed a commit to glennw/saltfs that referenced this pull request Oct 20, 2017
bors-servo pushed a commit to servo/saltfs that referenced this pull request Oct 20, 2017
Add Gankro to WR reviewers list.

Some reviews in WR that Gankro has worked on:

servo/webrender#1830
servo/webrender#1799
servo/webrender#1834
servo/webrender#1664

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/saltfs/740)
<!-- Reviewable:end -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants