nojson 0.3.11

A flexible Rust JSON library with no dependencies, no macros, no unsafe and optional no_std support
Documentation
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Parse and format benchmarks for nojson.
//!
//! Run with: `cargo run --release --example benchmark`
//!
//! Optional env var `BENCH_REPEATS` (default 30) controls the number of
//! best-of-N samples per case. Each sample auto-scales its inner loop count
//! to land near 100 ms of wall time, so reported numbers stay stable across
//! the size range covered here (a few hundred bytes through a few KB).
//!
//! Uses `std::time::Instant` rather than `criterion` so the crate keeps a
//! lean dev-dep tree.

use std::time::{Duration, Instant};

const TARGET_DURATION: Duration = Duration::from_millis(100);

struct Row {
    name: String,
    bytes: usize,
    ns_per_op: f64,
}

fn measure_one<F: FnMut()>(mut op: F) -> f64 {
    for _ in 0..3 {
        op();
    }
    let mut iters: u64 = 1;
    loop {
        let start = Instant::now();
        for _ in 0..iters {
            op();
        }
        let elapsed = start.elapsed();
        if elapsed >= TARGET_DURATION {
            return elapsed.as_nanos() as f64 / iters as f64;
        }
        let factor =
            (TARGET_DURATION.as_nanos() as f64 / elapsed.as_nanos().max(1) as f64).max(2.0);
        iters = (iters as f64 * factor).ceil() as u64;
    }
}

fn best_of<F: FnMut() -> f64>(repeats: usize, mut measure: F) -> f64 {
    (0..repeats)
        .map(|_| measure())
        .fold(f64::INFINITY, f64::min)
}

fn print_section(title: &str, rows: &[Row]) {
    println!("=== {title} ===");
    println!(
        "  {:<32} {:>10} {:>14} {:>12}",
        "case", "size", "ns/op", "MB/s"
    );
    for r in rows {
        let mb_s = if r.ns_per_op > 0.0 {
            r.bytes as f64 / r.ns_per_op * 1_000.0
        } else {
            f64::INFINITY
        };
        let size = format!("{} B", r.bytes);
        println!(
            "  {:<32} {:>10} {:>14.1} {:>12.1}",
            r.name, size, r.ns_per_op, mb_s
        );
    }
    println!();
}

// ----- Parse input generators -----

fn gen_long_ascii(len: usize) -> String {
    let base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ,.:;!?-_";
    let body: String = base.chars().cycle().take(len).collect();
    format!(r#""{body}""#)
}

fn gen_ascii_with_escapes(len: usize, interval: usize) -> String {
    let mut body = String::with_capacity(len + len / interval * 2);
    let base = "abcdefghijklmnopqrstuvwxyz";
    let mut chars = base.chars().cycle();
    for i in 0..len {
        if i > 0 && i % interval == 0 {
            body.push_str(r#"\""#);
        } else {
            body.push(chars.next().unwrap());
        }
    }
    format!(r#""{body}""#)
}

fn gen_many_short_keys(count: usize) -> String {
    let mut s = String::from("{");
    for i in 0..count {
        if i > 0 {
            s.push(',');
        }
        s.push_str(&format!(r#""key{i}":"val{i}""#));
    }
    s.push('}');
    s
}

fn gen_unicode_heavy_quoted(len: usize) -> String {
    let chars = "あいうえおかきくけこ日本語テスト🎉🚀✨";
    let body: String = chars.chars().cycle().take(len).collect();
    format!(r#""{body}""#)
}

fn gen_unicode_escapes(count: usize) -> String {
    let mut s = String::with_capacity(2 + count * 6);
    s.push('"');
    for i in 0..count {
        let code = match i % 4 {
            0 => "3042", //            1 => "65e5", //            2 => "672c", //            _ => "8a9e", //        };
        s.push_str(r#"\u"#);
        s.push_str(code);
    }
    s.push('"');
    s
}

fn gen_int_array(count: usize) -> String {
    let mut s = String::from("[");
    for i in 0..count {
        if i > 0 {
            s.push(',');
        }
        // Mix small and larger integers, plus a few negatives.
        let n: i64 = match i % 5 {
            0 => i as i64,
            1 => -(i as i64),
            2 => (i as i64) * 1_000,
            3 => (i as i64) * 1_000_000,
            _ => (i as i64) - 50,
        };
        s.push_str(&n.to_string());
    }
    s.push(']');
    s
}

fn gen_float_array(count: usize) -> String {
    let mut s = String::from("[");
    for i in 0..count {
        if i > 0 {
            s.push(',');
        }
        // Mix plain decimals, exponents, negatives, and small magnitudes.
        match i % 5 {
            0 => s.push_str(&format!("{:.4}", i as f64 * 1.5)),
            1 => s.push_str(&format!("{:e}", i as f64 * 2.7e-3 + 1.0)),
            2 => s.push_str(&format!("-{:.6}", i as f64 * 0.001)),
            3 => s.push_str(&format!("{:.2}", i as f64 + 0.25)),
            _ => s.push_str(&format!("{:.3e}", i as f64 * 1.234e6 + 1.0)),
        }
    }
    s.push(']');
    s
}

fn gen_full_json_document() -> String {
    let mut s = String::from(r#"{"users":["#);
    for i in 0..50 {
        if i > 0 {
            s.push(',');
        }
        s.push_str(&format!(
            r#"{{"id":{i},"name":"User {i} with a reasonably long name for testing","email":"user{i}@example.com","bio":"This is a biography text that contains several sentences. It is meant to test the performance of string parsing with typical content. Nothing special here, just plain ASCII text that goes on for a while to provide a realistic benchmark scenario.","active":true,"score":98.6}}"#
        ));
    }
    s.push_str("]}");
    s
}

fn gen_jsonc_document() -> String {
    // Realistic JSONC: line + block comments, trailing commas, light indent.
    let mut s = String::from("// User export, generated for benchmarks.\n{\n  \"users\": [\n");
    for i in 0..50 {
        if i > 0 && i % 10 == 0 {
            s.push_str(&format!("    /* batch boundary at {i} */\n"));
        }
        s.push_str(&format!(
            "    // user #{i}\n    {{\"id\":{i},\"name\":\"User {i} with a reasonably long name for testing\",\"email\":\"user{i}@example.com\",\"bio\":\"This is a biography text that contains several sentences. It is meant to test the performance of string parsing with typical content. Nothing special here, just plain ASCII text that goes on for a while to provide a realistic benchmark scenario.\",\"active\":true,\"score\":98.6}},\n"
        ));
    }
    s.push_str("  ],\n}\n");
    s
}

// ----- Format input generators -----

fn gen_plain_ascii(len: usize) -> String {
    let base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ,.:;!?-_";
    base.chars().cycle().take(len).collect()
}

fn gen_mixed_escapes(len: usize) -> String {
    let base = "hello world\nthis has \"quotes\" and \\backslashes\r\nand\ttabs";
    base.chars().cycle().take(len).collect()
}

fn gen_unicode_heavy_raw(len: usize) -> String {
    let chars = "あいうえおかきくけこ日本語テスト🎉🚀✨";
    chars.chars().cycle().take(len).collect()
}

fn gen_int_vec(count: usize) -> Vec<i64> {
    (0..count)
        .map(|i| match i % 5 {
            0 => i as i64,
            1 => -(i as i64),
            2 => (i as i64) * 1_000,
            3 => (i as i64) * 1_000_000,
            _ => (i as i64) - 50,
        })
        .collect()
}

fn gen_float_vec(count: usize) -> Vec<f64> {
    (0..count)
        .map(|i| match i % 5 {
            0 => i as f64 * 1.5,
            1 => i as f64 * 2.7e-3 + 1.0,
            2 => -(i as f64) * 0.001,
            3 => i as f64 + 0.25,
            _ => i as f64 * 1.234e6 + 1.0,
        })
        .collect()
}

struct UserRecord {
    id: i32,
    name: String,
    email: String,
    bio: String,
    active: bool,
    score: f64,
    tags: Vec<&'static str>,
}

fn gen_user_records(count: usize) -> Vec<UserRecord> {
    (0..count)
        .map(|i| UserRecord {
            id: i as i32,
            name: format!("User {i} with a reasonably long name for testing"),
            email: format!("user{i}@example.com"),
            bio: "This is a biography text that contains several sentences. It is meant to test the performance of string parsing with typical content. Nothing special here, just plain ASCII text that goes on for a while to provide a realistic benchmark scenario.".to_string(),
            active: i % 3 != 0,
            score: 98.6 + i as f64 * 0.01,
            tags: match i % 3 {
                0 => vec!["alpha", "beta"],
                1 => vec!["gamma"],
                _ => vec!["alpha", "delta", "epsilon"],
            },
        })
        .collect()
}

// ----- Sections -----

fn bench_parse(repeats: usize) -> Vec<Row> {
    let cases: Vec<(String, String)> = {
        let mut v: Vec<(String, String)> = Vec::new();
        for &len in &[64usize, 256, 1024] {
            v.push((format!("long_ascii_no_escape_{len}B"), gen_long_ascii(len)));
        }
        v.push((
            "ascii_with_escapes_256B/32".into(),
            gen_ascii_with_escapes(256, 32),
        ));
        v.push(("many_short_keys_100".into(), gen_many_short_keys(100)));
        v.push(("unicode_heavy_200ch".into(), gen_unicode_heavy_quoted(200)));
        v.push(("unicode_escapes_128".into(), gen_unicode_escapes(128)));
        v.push(("int_array_1000".into(), gen_int_array(1000)));
        v.push(("float_array_1000".into(), gen_float_array(1000)));
        v.push((
            "full_json_document_50users".into(),
            gen_full_json_document(),
        ));
        v
    };

    let mut rows: Vec<Row> = cases
        .iter()
        .map(|(name, input)| {
            let bytes = input.len();
            let ns = best_of(repeats, || {
                measure_one(|| {
                    let _ = nojson::RawJson::parse(input).unwrap();
                })
            });
            Row {
                name: name.clone(),
                bytes,
                ns_per_op: ns,
            }
        })
        .collect();

    // JSONC parse via the dedicated entry point.
    let jsonc = gen_jsonc_document();
    let bytes = jsonc.len();
    let ns = best_of(repeats, || {
        measure_one(|| {
            let _ = nojson::RawJson::parse_jsonc(&jsonc).unwrap();
        })
    });
    rows.push(Row {
        name: "jsonc_document_50users".into(),
        bytes,
        ns_per_op: ns,
    });

    rows
}

fn bench_format(repeats: usize) -> Vec<Row> {
    let mut rows: Vec<Row> = Vec::new();

    for &len in &[64usize, 256, 1024] {
        let input = gen_plain_ascii(len);
        let out_len = nojson::json(|f| f.value(input.as_str())).to_string().len();
        let ns = best_of(repeats, || {
            measure_one(|| {
                let _ = nojson::json(|f| f.value(input.as_str())).to_string();
            })
        });
        rows.push(Row {
            name: format!("plain_ascii_{len}B"),
            bytes: out_len,
            ns_per_op: ns,
        });
    }

    let input = gen_mixed_escapes(256);
    let out_len = nojson::json(|f| f.value(input.as_str())).to_string().len();
    let ns = best_of(repeats, || {
        measure_one(|| {
            let _ = nojson::json(|f| f.value(input.as_str())).to_string();
        })
    });
    rows.push(Row {
        name: "mixed_escapes_256B".into(),
        bytes: out_len,
        ns_per_op: ns,
    });

    let input = gen_unicode_heavy_raw(200);
    let out_len = nojson::json(|f| f.value(input.as_str())).to_string().len();
    let ns = best_of(repeats, || {
        measure_one(|| {
            let _ = nojson::json(|f| f.value(input.as_str())).to_string();
        })
    });
    rows.push(Row {
        name: "unicode_heavy_200ch".into(),
        bytes: out_len,
        ns_per_op: ns,
    });

    // Object with many string key-value pairs.
    let keys: Vec<String> = (0..50).map(|i| format!("key_{i}")).collect();
    let values: Vec<String> = (0..50)
        .map(|i| {
            format!("This is value number {i} with a reasonably long text content for benchmarking")
        })
        .collect();
    let pairs: Vec<(&str, &str)> = keys
        .iter()
        .zip(values.iter())
        .map(|(k, v)| (k.as_str(), v.as_str()))
        .collect();
    let out_len = nojson::json(|f| {
        f.object(|f| {
            for &(k, v) in pairs.iter() {
                f.member(k, v)?;
            }
            Ok(())
        })
    })
    .to_string()
    .len();
    let ns = best_of(repeats, || {
        measure_one(|| {
            let _ = nojson::json(|f| {
                f.object(|f| {
                    for &(k, v) in pairs.iter() {
                        f.member(k, v)?;
                    }
                    Ok(())
                })
            })
            .to_string();
        })
    });
    rows.push(Row {
        name: "object_formatting_50pairs".into(),
        bytes: out_len,
        ns_per_op: ns,
    });

    // Numeric arrays.
    let ints = gen_int_vec(1000);
    let out_len = nojson::json(|f| f.value(&ints)).to_string().len();
    let ns = best_of(repeats, || {
        measure_one(|| {
            let _ = nojson::json(|f| f.value(&ints)).to_string();
        })
    });
    rows.push(Row {
        name: "int_array_1000".into(),
        bytes: out_len,
        ns_per_op: ns,
    });

    let floats = gen_float_vec(1000);
    let out_len = nojson::json(|f| f.value(&floats)).to_string().len();
    let ns = best_of(repeats, || {
        measure_one(|| {
            let _ = nojson::json(|f| f.value(&floats)).to_string();
        })
    });
    rows.push(Row {
        name: "float_array_1000".into(),
        bytes: out_len,
        ns_per_op: ns,
    });

    // Mixed-type document: parallel to the parse-side full_json_document case
    // but exercises the full formatter (numbers, strings, bools, nested arrays).
    let users = gen_user_records(50);
    let format_users = |f: &mut nojson::JsonFormatter<'_, '_>| {
        f.object(|f| {
            f.member(
                "users",
                nojson::json(|f| {
                    f.array(|f| {
                        for u in &users {
                            f.element(nojson::json(|f| {
                                f.object(|f| {
                                    f.member("id", u.id)?;
                                    f.member("name", u.name.as_str())?;
                                    f.member("email", u.email.as_str())?;
                                    f.member("bio", u.bio.as_str())?;
                                    f.member("active", u.active)?;
                                    f.member("score", u.score)?;
                                    f.member("tags", &u.tags)
                                })
                            }))?;
                        }
                        Ok(())
                    })
                }),
            )
        })
    };
    let out_len = nojson::json(format_users).to_string().len();
    let ns = best_of(repeats, || {
        measure_one(|| {
            let _ = nojson::json(format_users).to_string();
        })
    });
    rows.push(Row {
        name: "mixed_document_50users".into(),
        bytes: out_len,
        ns_per_op: ns,
    });

    rows
}

fn main() {
    let repeats: usize = std::env::var("BENCH_REPEATS")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(30);

    println!("nojson benchmark");
    println!("repeats per case: {repeats} (best-of-N reported)");
    println!();

    let rows = bench_parse(repeats);
    print_section("parse", &rows);

    let rows = bench_format(repeats);
    print_section("format", &rows);
}