naomijub / edn-rs

[DEPRECATED]: Crate to parse and emit EDN
https://crates.io/crates/edn-rs
MIT License
81 stars 14 forks source link

Improve performance #39

Closed naomijub closed 4 years ago

naomijub commented 4 years ago

By not using replace we could improve performance to somewhere around 25µs. Currently the parse performance is around 140µs:

use std::time::{Instant};
use edn_rs;
use std::str::FromStr;

fn main() {
    let edn = edn_str();

    let start = Instant::now();
    let value = edn_rs::Edn::from_str(&edn);
    let duration = start.elapsed();

    if value.is_ok() {
        println!("Time elapsed in edn_rs::from_str is: {:?}", duration);

        let start_nav = Instant::now();
        let role = value.unwrap()[":associates"][0][":role"].clone();
        let duration_nav = start_nav.elapsed();

        println!("Time elapsed to navigate to role_0 \"{}\": {:?}", role, duration_nav);
    } else {
        println!("Parse failed. Duration {:?}", duration);
    }
}

fn edn_str() -> String {
    "{
        :type :human
        :first-name \"bench\"
        :last-name \"mark\"
        :age 13
        :version 0.13
        :associates [
            {
                :name :julia
                :role :adm
            }
            {
                :name :otavio
                :role :contributor
            }
            {
                :name :juxt
                :role :great-ideas
            }
        ]
    }".to_string()
}
naomijub commented 4 years ago

@otaviopace

evaporei commented 4 years ago

I will probably use parser combinators https://github.com/Geal/nom

naomijub commented 4 years ago

Considering bench tool Criterion we got:

Benchmarking parse: Collecting 100 samples in estimated 5.1256 s (172k iteration                                                                              
parse                   time:   [29.361 us 29.447 us 29.541 us]
Found 7 outliers among 100 measurements (7.00%)
  2 (2.00%) low mild
  5 (5.00%) high severe

which can be improved to 5~7us

https://github.com/naomijub/edn-duration-benchmark/tree/master/criterion-parse-rs

naomijub commented 4 years ago

Current benchmark is around 13us: https://github.com/naomijub/edn-rs/pull/40