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

Optimize deserialization of recursive buffered types #2148

Merged
merged 1 commit into from Jan 1, 2022

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Jan 1, 2022

Closes #2146.

@dtolnay dtolnay merged commit 1f57084 into master Jan 1, 2022
@dtolnay dtolnay deleted the deserializecontent branch January 1, 2022 21:16
@dtolnay
Copy link
Member Author

dtolnay commented Jan 2, 2022

Benchmark:

#![feature(test)]

extern crate test;

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap as Map;

#[derive(Deserialize, Serialize)]
#[serde(tag = "type")]
enum Enum {
    Rec(Map<String, Enum>),
    End,
}

#[bench]
fn bench(b: &mut test::Bencher) {
    let j = serde_json::to_string(&obj()).unwrap();
    b.iter(|| serde_json::from_str::<Enum>(&j).unwrap());
}

fn obj() -> Enum {
    let mut map = Map::new();
    for i in 0..100 {
        let mut outer = Map::new();
        outer.insert(i.to_string(), Enum::Rec(map));
        map = outer;
    }
    Enum::Rec(map)
}
  • Before: 834 μs/iter
  • After: 49 μs/iter

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

Successfully merging this pull request may close these issues.

Improve performance using nightly features
1 participant