File: serialization.rs

package info (click to toggle)
rust-yrs 0.23.5-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,572 kB
  • sloc: makefile: 2
file content (177 lines) | stat: -rw-r--r-- 5,762 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use criterion::{black_box, criterion_group, criterion_main, Criterion, SamplingMode};
use yrs::encoding::read::{Cursor, Read};
use yrs::encoding::write::Write;
use yrs::Any;

const BENCHMARK_SIZE: u32 = 100000;

fn bench_encoding(c: &mut Criterion) {
    let mut encoding_group = c.benchmark_group("encoding");
    encoding_group.sampling_mode(SamplingMode::Flat);

    encoding_group.bench_function("var_int (64 bit)", |b| {
        b.iter(|| {
            let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8);
            for i in 0..(BENCHMARK_SIZE as i64) {
                encoder.write_var(i);
            }
            let mut decoder = Cursor::from(&encoder);
            for i in 0..(BENCHMARK_SIZE as i64) {
                let num: i64 = decoder.read_var().unwrap();
                assert_eq!(num, i);
            }
        })
    });

    encoding_group.bench_function("var_uint (32 bit)", |b| {
        b.iter(|| {
            let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8);
            for i in 0..BENCHMARK_SIZE {
                encoder.write_var(i);
            }
            let mut decoder = Cursor::from(&encoder);
            for i in 0..BENCHMARK_SIZE {
                let num: u32 = decoder.read_var().unwrap();
                assert_eq!(num, i);
            }
        })
    });

    encoding_group.bench_function("uint32", |b| {
        b.iter(|| {
            let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8);
            for i in 0..BENCHMARK_SIZE {
                encoder.write_u32(i);
            }
            let mut decoder = Cursor::from(&encoder);
            for i in 0..BENCHMARK_SIZE {
                let num: u32 = decoder.read_u32().unwrap();
                assert_eq!(num, i);
            }
        })
    });

    encoding_group.bench_function("var_uint (64 bit)", |b| {
        b.iter(|| {
            let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8);
            for i in 0..(BENCHMARK_SIZE as u64) {
                encoder.write_var(i);
            }
            let mut decoder = Cursor::from(&encoder);
            for i in 0..(BENCHMARK_SIZE as u64) {
                let num: u64 = decoder.read_var().unwrap();
                assert_eq!(num, i);
            }
        })
    });

    encoding_group.bench_function("uint64", |b| {
        b.iter(|| {
            let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8);
            for i in 0..(BENCHMARK_SIZE as u64) {
                encoder.write_u64(i)
            }
            let mut decoder = Cursor::from(&encoder);
            for i in 0..(BENCHMARK_SIZE as u64) {
                let num = decoder.read_u64().unwrap();
                assert_eq!(num, i);
            }
        })
    });
}

fn bench_serialization(c: &mut Criterion) {
    use std::collections::HashMap;

    let any = Any::from(HashMap::from([
        ("bool".into(), Any::from(true)),
        ("int".into(), Any::from(1)),
        ("negativeInt".into(), Any::from(-1)),
        ("maxInt".into(), Any::from(i64::MAX)),
        ("minInt".into(), Any::from(i64::MIN)),
        ("realNumber".into(), Any::from(-123.2387f64)),
        ("maxNumber".into(), Any::from(f64::MIN)),
        ("minNumber".into(), Any::from(f64::MAX)),
        ("null".into(), Any::Null),
        (
            "map".into(),
            Any::from(HashMap::from([
                ("bool".into(), Any::from(true)),
                ("int".into(), Any::from(1)),
                ("negativeInt".into(), Any::from(-1)),
                ("maxInt".into(), Any::from(i64::MAX)),
                ("minInt".into(), Any::from(i64::MIN)),
                ("realNumber".into(), Any::from(-123.2387f64)),
                ("maxNumber".into(), Any::from(f64::MIN)),
                ("minNumber".into(), Any::from(f64::MAX)),
                ("null".into(), Any::Null),
            ])),
        ),
        (
            "key6".into(),
            Any::from(vec![
                Any::from(true),
                Any::from(1),
                Any::from(-1),
                Any::from(i64::MAX),
                Any::from(i64::MIN),
                Any::from(-123.2387f64),
                Any::from(f64::MIN),
                Any::from(f64::MAX),
                Any::Null,
            ]),
        ),
    ]));

    let any_json = serde_json::to_string(&any).unwrap();

    let mut serde_group = c.benchmark_group("serde");
    serde_group.bench_function("Any-JSON roundtrip", |b| {
        b.iter(|| {
            black_box(serde_json::from_str::<Any>(&serde_json::to_string(&any).unwrap()).unwrap());
        })
    });

    serde_group.bench_function("Any serialize", |b| {
        b.iter(|| {
            black_box(serde_json::to_string(&any).unwrap());
        })
    });

    serde_group.bench_function("Any deserialize", |b| {
        b.iter(|| {
            black_box(serde_json::to_string(&any).unwrap());
        })
    });

    serde_group.finish();

    let mut custom_group = c.benchmark_group("custom json serialization");

    custom_group.bench_function("Any-JSON roundtrip", |b| {
        b.iter(|| {
            let mut roundtrip = String::new();
            any.to_json(&mut roundtrip);

            black_box(Any::from_json(&roundtrip));
        })
    });

    custom_group.bench_function("Any serialize", |b| {
        b.iter(|| {
            let mut roundtrip = String::new();
            black_box(any.to_json(&mut roundtrip));
        })
    });

    custom_group.bench_function("Any deserialize", |b| {
        b.iter(|| {
            black_box(Any::from_json(&any_json));
        })
    });

    custom_group.finish();
}

criterion_group!(serialization, bench_encoding, bench_serialization);
criterion_main!(serialization);