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

Add faster serde serialization, sacrificing invariant-safety for speed in release mode; test that it roudtrips #252

Closed
wants to merge 4 commits into from

Conversation

Manishearth
Copy link
Member

@Manishearth Manishearth commented Dec 17, 2016

r? @SimonSapin


This change is Reviewable

@Manishearth
Copy link
Member Author

r? @erickt on the custom serialization

I didn't use the serialize_struct and serialize_struct_elt stuff because this isn't going to be used for json serialization. Should I have?

@erickt
Copy link

erickt commented Dec 17, 2016

cc @dtolnay

Don't do this, it'll break formats like json that need to wrap aggregates in {...} or [..]. Instead, if you want zero overhead with bincode, serialize into a tuple, as in:

fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: serde::Serializer {
    (&self.serialization, schema_end, ...).serialize(serializer)
}

We got a ticket open to write a container-level attribute that'll do this for you.

HostInternal::None => 0u8.serialize(serializer),
HostInternal::Domain => 1u8.serialize(serializer),
HostInternal::Ipv4(addr) => {
2u8.serialize(serializer)?;
Copy link

Choose a reason for hiding this comment

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

would be better as (2u8, addr).serialize(serializer)

addr.serialize(serializer)
}
HostInternal::Ipv6(addr) => {
3u8.serialize(serializer)?;
Copy link

Choose a reason for hiding this comment

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

same here

@erickt
Copy link

erickt commented Dec 17, 2016

Actually, serde_macros + bincode would generate the same code here as what you manually wrote, and would be safe to use with multiple formats, so I recommend just using that if possible.

@Manishearth
Copy link
Member Author

Picked up the code generator from #254, and added a travis check to ensure that its in sync.

r? @SimonSapin

@erickt
Copy link

erickt commented Dec 17, 2016

FYI, once we implement serde-rs/serde#642, you should be able to replace your custom serde impls for Url with this hook.

@nox
Copy link
Contributor

nox commented Dec 18, 2016

@Manishearth I'm very much against that patch. No invalid URL should be produced. Why was that needed?

@Manishearth
Copy link
Member Author

Split it out as an extra pair of methods

@nox
Copy link
Contributor

nox commented Dec 18, 2016

Why must we put into the repos generated code?

@Manishearth
Copy link
Member Author

Need it to work on stable and don't want heavy dependencies on aster/syntex. It's temporary till macros 1.1 comes out.

Trying to keep the dependency list small for now.

@SimonSapin
Copy link
Member

Need it to work on stable and don't want heavy dependencies on aster/syntex. It's temporary till macros 1.1 comes out.

Doesn’t serde with macros 1.1 use aster and syntax?

@Manishearth
Copy link
Member Author

No, it uses syn, and doesn't need build scripts.

@nox
Copy link
Contributor

nox commented Dec 18, 2016

serde can use syn with a build script on stable without needing syntex. I would rather we do that instead of checking in generated code.

#[cfg(feature="serde")]
impl Url {
/// Serialize the URL efficiently, for use with IPC
pub fn serialize_efficient<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: serde::Serializer {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please don't name if efficient. Its main property is that it is risky, not efficient.

Copy link
Member Author

Choose a reason for hiding this comment

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

Renamed to _unsafe

query_start: query_start,
fragment_start: fragment_start
};
#[cfg(debug_assertions)] {
Copy link
Contributor

Choose a reason for hiding this comment

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

Without debug_assertions, type errors in that block won't be emitted. Do if cfg!(debug_assertions) instead.

@Manishearth
Copy link
Member Author

serde can use syn with a build script on stable without needing syntex.

Fair. I also want to avoid a build script. And I want to avoid new dependencies for as long as possible, until https://bugzilla.mozilla.org/show_bug.cgi?id=1322798 is resolved. Right now the state of rust-url dependencies is acceptable for Gecko, but until that issue is resolved I'd prefer to wait before adding extra dependencies that can be avoided.

@nox
Copy link
Contributor

nox commented Dec 18, 2016

Fair. I also want to avoid a build script. And I want to avoid new dependencies for as long as possible, until https://bugzilla.mozilla.org/show_bug.cgi?id=1322798 is resolved. Right now the state of rust-url dependencies is acceptable for Gecko, but until that issue is resolved I'd prefer to wait before adding extra dependencies that can be avoided.

I would rather we make things more complicated on the Gecko side than on the Rust side for every contributor stumbling upon generated code.

@Manishearth
Copy link
Member Author

The only generated code is for the host struct, which isn't modified very often.

@Manishearth
Copy link
Member Author

And if we use syn+build script on this side you still have to deal with generated code. And debugging gets harder to do.

SimonSapin pushed a commit that referenced this pull request Dec 19, 2016
SimonSapin pushed a commit that referenced this pull request Dec 19, 2016
@SimonSapin
Copy link
Member

Closing in favor of #259, which is largely based on this.

@SimonSapin SimonSapin closed this Dec 19, 2016
@nox nox deleted the serde branch July 19, 2019 08:28
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.

4 participants