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

Improve performance using nightly features #2146

Closed
kdy1 opened this issue Jan 1, 2022 · 1 comment · Fixed by #2148
Closed

Improve performance using nightly features #2146

kdy1 opened this issue Jan 1, 2022 · 1 comment · Fixed by #2148

Comments

@kdy1
Copy link

kdy1 commented Jan 1, 2022

Context

I'm working to improve the performance of the deserializer. (See swc-project/swc#3160)

I found that serde::__private::de::Content is created quite frequently from __deserializer and __deserializer is serde::__private::de::ContentDeserailizer in many cases.

Try: specialization

So I copied code from serde, and specialized implementation of deserialize for Content.

impl<'de> Deserialize<'de> for Content<'de> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        <Content as DeserImpl<'de, D>>::deserialize_spec(deserializer)
    }
}

trait DeserImpl<'de, D>: Sized
where
    D: Deserializer<'de>,
{
    fn deserialize_spec(deserializer: D) -> Result<Self, D::Error>;
}

impl<'de, D> DeserImpl<'de, D> for Content<'de>
where
    D: Deserializer<'de>,
{
    default fn deserialize_spec(deserializer: D) -> Result<Self, D::Error> {
        // Untagged and internally tagged enums are only supported in
        // self-describing formats.
        let visitor = ContentVisitor { value: PhantomData };
        deserializer.deserialize_any(visitor)
    }
}

impl<'de, E> DeserImpl<'de, ContentDeserializer<'de, E>> for Content<'de>
where
    ContentDeserializer<'de, E>: Deserializer<'de, Error = E>,
{
    fn deserialize_spec(deserializer: ContentDeserializer<'de, E>) -> Result<Self, E> {
        Ok(deserializer.content)
    }
}

Previous

test json_deserialize                     ... bench:     1,226,131 ns/iter (+/- 58,655)

After

test json_deserialize                     ... bench:     621,480 ns/iter (+/- 69,766)

I spotted few more creation of Content from serde::__private::de::ContentDeserailizer due to #[flatten] and going to implement custom #[derive(Deserialize)] which uses my fork of ContentDeserailizer.

But this is useful for many users ofserde, so I'm filing an issue.

Using fork

To quickly apply specialization for ContentDeserailizer of #[flatten], I created a fork (https://github.com/kdy1/serde/tree/content-specialization). Using it, I could get the result before creating a custom dervie macro.

test json_deserialize                     ... bench:     339,526 ns/iter (+/- 41,530)
@dtolnay
Copy link
Member

dtolnay commented Jan 1, 2022

specialization is not suitable for this because of the unsound interaction with lifetimes, and I would guess min_specialization is not expressive enough to implement this.

The same behavior can be achieved without specialization though. #2148

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants