| 
 | 1 | +use codspeed_bencher_compat::{benchmark_group, benchmark_main, Bencher};  | 
 | 2 | +use std::hint::black_box;  | 
 | 3 | + | 
 | 4 | +use std::fs::File;  | 
 | 5 | +use std::io::Read;  | 
 | 6 | + | 
 | 7 | +use batson::get::{get_str, BatsonPath};  | 
 | 8 | +use batson::{batson_to_json_string, encode_from_json};  | 
 | 9 | +use jiter::JsonValue;  | 
 | 10 | + | 
 | 11 | +fn read_file(path: &str) -> String {  | 
 | 12 | +    let mut file = File::open(path).unwrap();  | 
 | 13 | +    let mut contents = String::new();  | 
 | 14 | +    file.read_to_string(&mut contents).unwrap();  | 
 | 15 | +    contents  | 
 | 16 | +}  | 
 | 17 | + | 
 | 18 | +/// taken from <https://github.com/datafusion-contrib/datafusion-functions-json/blob/v0.41.0/src/common.rs#L184-L216>  | 
 | 19 | +mod jiter_find {  | 
 | 20 | +    use jiter::{Jiter, Peek};  | 
 | 21 | + | 
 | 22 | +    #[derive(Debug)]  | 
 | 23 | +    pub enum JsonPath<'s> {  | 
 | 24 | +        Key(&'s str),  | 
 | 25 | +        Index(usize),  | 
 | 26 | +        None,  | 
 | 27 | +    }  | 
 | 28 | + | 
 | 29 | +    impl From<u64> for JsonPath<'_> {  | 
 | 30 | +        fn from(index: u64) -> Self {  | 
 | 31 | +            JsonPath::Index(usize::try_from(index).unwrap())  | 
 | 32 | +        }  | 
 | 33 | +    }  | 
 | 34 | + | 
 | 35 | +    impl From<i32> for JsonPath<'_> {  | 
 | 36 | +        fn from(index: i32) -> Self {  | 
 | 37 | +            match usize::try_from(index) {  | 
 | 38 | +                Ok(i) => Self::Index(i),  | 
 | 39 | +                Err(_) => Self::None,  | 
 | 40 | +            }  | 
 | 41 | +        }  | 
 | 42 | +    }  | 
 | 43 | + | 
 | 44 | +    impl<'s> From<&'s str> for JsonPath<'s> {  | 
 | 45 | +        fn from(key: &'s str) -> Self {  | 
 | 46 | +            JsonPath::Key(key)  | 
 | 47 | +        }  | 
 | 48 | +    }  | 
 | 49 | + | 
 | 50 | +    pub fn jiter_json_find<'j>(opt_json: Option<&'j str>, path: &[JsonPath]) -> Option<(Jiter<'j>, Peek)> {  | 
 | 51 | +        let json_str = opt_json?;  | 
 | 52 | +        let mut jiter = Jiter::new(json_str.as_bytes());  | 
 | 53 | +        let mut peek = jiter.peek().ok()?;  | 
 | 54 | +        for element in path {  | 
 | 55 | +            match element {  | 
 | 56 | +                JsonPath::Key(key) if peek == Peek::Object => {  | 
 | 57 | +                    let mut next_key = jiter.known_object().ok()??;  | 
 | 58 | + | 
 | 59 | +                    while next_key != *key {  | 
 | 60 | +                        jiter.next_skip().ok()?;  | 
 | 61 | +                        next_key = jiter.next_key().ok()??;  | 
 | 62 | +                    }  | 
 | 63 | + | 
 | 64 | +                    peek = jiter.peek().ok()?;  | 
 | 65 | +                }  | 
 | 66 | +                JsonPath::Index(index) if peek == Peek::Array => {  | 
 | 67 | +                    let mut array_item = jiter.known_array().ok()??;  | 
 | 68 | + | 
 | 69 | +                    for _ in 0..*index {  | 
 | 70 | +                        jiter.known_skip(array_item).ok()?;  | 
 | 71 | +                        array_item = jiter.array_step().ok()??;  | 
 | 72 | +                    }  | 
 | 73 | + | 
 | 74 | +                    peek = array_item;  | 
 | 75 | +                }  | 
 | 76 | +                _ => {  | 
 | 77 | +                    return None;  | 
 | 78 | +                }  | 
 | 79 | +            }  | 
 | 80 | +        }  | 
 | 81 | +        Some((jiter, peek))  | 
 | 82 | +    }  | 
 | 83 | + | 
 | 84 | +    pub fn get_str(json_data: Option<&str>, path: &[JsonPath]) -> Option<String> {  | 
 | 85 | +        if let Some((mut jiter, peek)) = jiter_json_find(json_data, path) {  | 
 | 86 | +            match peek {  | 
 | 87 | +                Peek::String => Some(jiter.known_str().ok()?.to_owned()),  | 
 | 88 | +                _ => None,  | 
 | 89 | +            }  | 
 | 90 | +        } else {  | 
 | 91 | +            None  | 
 | 92 | +        }  | 
 | 93 | +    }  | 
 | 94 | +}  | 
 | 95 | + | 
 | 96 | +mod serde_find {  | 
 | 97 | +    use batson::get::BatsonPath;  | 
 | 98 | +    use serde_json::Value;  | 
 | 99 | + | 
 | 100 | +    pub fn get_str(json_data: &[u8], path: &[BatsonPath]) -> Option<String> {  | 
 | 101 | +        let json_value: Value = serde_json::from_slice(json_data).ok()?;  | 
 | 102 | +        let mut current = &json_value;  | 
 | 103 | +        for key in path {  | 
 | 104 | +            current = match (key, current) {  | 
 | 105 | +                (BatsonPath::Key(k), Value::Object(map)) => map.get(*k)?,  | 
 | 106 | +                (BatsonPath::Index(i), Value::Array(vec)) => vec.get(*i)?,  | 
 | 107 | +                _ => return None,  | 
 | 108 | +            }  | 
 | 109 | +        }  | 
 | 110 | +        match current {  | 
 | 111 | +            Value::String(s) => Some(s.clone()),  | 
 | 112 | +            _ => None,  | 
 | 113 | +        }  | 
 | 114 | +    }  | 
 | 115 | +}  | 
 | 116 | + | 
 | 117 | +fn json_to_batson(json: &[u8]) -> Vec<u8> {  | 
 | 118 | +    let json_value = JsonValue::parse(json, false).unwrap();  | 
 | 119 | +    encode_from_json(&json_value).unwrap()  | 
 | 120 | +}  | 
 | 121 | + | 
 | 122 | +fn medium_get_str_found_batson(bench: &mut Bencher) {  | 
 | 123 | +    let json = read_file("../jiter/benches/medium_response.json");  | 
 | 124 | +    let json_data = json.as_bytes();  | 
 | 125 | +    let batson_data = json_to_batson(json_data);  | 
 | 126 | +    let path: Vec<BatsonPath> = vec!["person".into(), "linkedin".into(), "handle".into()];  | 
 | 127 | +    bench.iter(|| {  | 
 | 128 | +        let v = get_str(black_box(&batson_data), &path);  | 
 | 129 | +        black_box(v)  | 
 | 130 | +    });  | 
 | 131 | +}  | 
 | 132 | + | 
 | 133 | +fn medium_get_str_found_jiter(bench: &mut Bencher) {  | 
 | 134 | +    let json = read_file("../jiter/benches/medium_response.json");  | 
 | 135 | +    let path: Vec<jiter_find::JsonPath> = vec!["person".into(), "linkedin".into(), "handle".into()];  | 
 | 136 | +    bench.iter(|| {  | 
 | 137 | +        let v = jiter_find::get_str(black_box(Some(&json)), &path);  | 
 | 138 | +        black_box(v)  | 
 | 139 | +    });  | 
 | 140 | +}  | 
 | 141 | + | 
 | 142 | +fn medium_get_str_found_serde(bench: &mut Bencher) {  | 
 | 143 | +    let json = read_file("../jiter/benches/medium_response.json");  | 
 | 144 | +    let json_data = json.as_bytes();  | 
 | 145 | +    let path: Vec<BatsonPath> = vec!["person".into(), "linkedin".into(), "handle".into()];  | 
 | 146 | +    bench.iter(|| {  | 
 | 147 | +        let v = serde_find::get_str(black_box(json_data), &path).unwrap();  | 
 | 148 | +        black_box(v)  | 
 | 149 | +    });  | 
 | 150 | +}  | 
 | 151 | + | 
 | 152 | +fn medium_get_str_missing_batson(bench: &mut Bencher) {  | 
 | 153 | +    let json = read_file("../jiter/benches/medium_response.json");  | 
 | 154 | +    let json_data = json.as_bytes();  | 
 | 155 | +    let batson_data = json_to_batson(json_data);  | 
 | 156 | +    let path: Vec<BatsonPath> = vec!["squid".into(), "linkedin".into(), "handle".into()];  | 
 | 157 | +    bench.iter(|| {  | 
 | 158 | +        let v = get_str(black_box(&batson_data), &path);  | 
 | 159 | +        black_box(v)  | 
 | 160 | +    });  | 
 | 161 | +}  | 
 | 162 | + | 
 | 163 | +fn medium_get_str_missing_jiter(bench: &mut Bencher) {  | 
 | 164 | +    let json = read_file("../jiter/benches/medium_response.json");  | 
 | 165 | +    let path: Vec<jiter_find::JsonPath> = vec!["squid".into(), "linkedin".into(), "handle".into()];  | 
 | 166 | +    bench.iter(|| {  | 
 | 167 | +        let v = jiter_find::get_str(black_box(Some(&json)), &path);  | 
 | 168 | +        black_box(v)  | 
 | 169 | +    });  | 
 | 170 | +}  | 
 | 171 | + | 
 | 172 | +fn medium_get_str_missing_serde(bench: &mut Bencher) {  | 
 | 173 | +    let json = read_file("../jiter/benches/medium_response.json");  | 
 | 174 | +    let json_data = json.as_bytes();  | 
 | 175 | +    let path: Vec<BatsonPath> = vec!["squid".into(), "linkedin".into(), "handle".into()];  | 
 | 176 | +    bench.iter(|| {  | 
 | 177 | +        let v = serde_find::get_str(black_box(json_data), &path);  | 
 | 178 | +        black_box(v)  | 
 | 179 | +    });  | 
 | 180 | +}  | 
 | 181 | + | 
 | 182 | +fn medium_convert_batson_to_json(bench: &mut Bencher) {  | 
 | 183 | +    let json = read_file("../jiter/benches/medium_response.json");  | 
 | 184 | +    let json_data = json.as_bytes();  | 
 | 185 | +    let batson_data = json_to_batson(json_data);  | 
 | 186 | +    bench.iter(|| {  | 
 | 187 | +        let v = batson_to_json_string(black_box(&batson_data)).unwrap();  | 
 | 188 | +        black_box(v)  | 
 | 189 | +    });  | 
 | 190 | +}  | 
 | 191 | + | 
 | 192 | +fn medium_convert_json_to_batson(bench: &mut Bencher) {  | 
 | 193 | +    let json = read_file("../jiter/benches/medium_response.json");  | 
 | 194 | +    let json = json.as_bytes();  | 
 | 195 | +    bench.iter(|| {  | 
 | 196 | +        let json_value = JsonValue::parse(json, false).unwrap();  | 
 | 197 | +        let b = encode_from_json(&json_value).unwrap();  | 
 | 198 | +        black_box(b)  | 
 | 199 | +    });  | 
 | 200 | +}  | 
 | 201 | + | 
 | 202 | +benchmark_group!(  | 
 | 203 | +    benches,  | 
 | 204 | +    medium_get_str_found_batson,  | 
 | 205 | +    medium_get_str_found_jiter,  | 
 | 206 | +    medium_get_str_found_serde,  | 
 | 207 | +    medium_get_str_missing_batson,  | 
 | 208 | +    medium_get_str_missing_jiter,  | 
 | 209 | +    medium_get_str_missing_serde,  | 
 | 210 | +    medium_convert_batson_to_json,  | 
 | 211 | +    medium_convert_json_to_batson  | 
 | 212 | +);  | 
 | 213 | +benchmark_main!(benches);  | 
0 commit comments