Skip to main content

bench_to_json_null_utf8/
bench_to_json_null_utf8.rs

1//! to_json(records) over a DataFrame with all-valid numeric columns + a NULLABLE Utf8 column.
2//! Before the JCol::UN arm, one null-bearing string column forced the WHOLE frame onto the
3//! serde tree. NEW builds the nullable Utf8 column as a LazyNullableUtf8 backing (→ typed fast
4//! writer); CONTROL builds it eagerly (→ serde tree) so the two are byte-identical but exercise
5//! different writers.
6//!
7//! Run: cargo run -p fp-io --release --example bench_to_json_null_utf8 -- 1000000 6
8use std::collections::BTreeMap;
9
10use fp_columnar::{Column, ValidityMask};
11use fp_frame::DataFrame;
12use fp_index::Index;
13use fp_io::JsonOrient;
14use fp_types::Scalar;
15
16fn main() {
17    let a: Vec<String> = std::env::args().collect();
18    let n: usize = a.get(1).and_then(|s| s.parse().ok()).unwrap_or(1_000_000);
19    let iters: usize = a.get(2).and_then(|s| s.parse().ok()).unwrap_or(6);
20
21    let ids: Vec<i64> = (0..n as i64).collect();
22    let vals: Vec<f64> = (0..n).map(|i| (i % 100000) as f64 / 100.0).collect();
23
24    let mut bytes: Vec<u8> = Vec::with_capacity(n * 10);
25    let mut offsets: Vec<usize> = Vec::with_capacity(n + 1);
26    offsets.push(0);
27    let mut validity = ValidityMask::all_valid(n);
28    let mut eager: Vec<Scalar> = Vec::with_capacity(n);
29    for i in 0..n {
30        if i % 5 == 0 {
31            validity.set(i, false);
32            eager.push(Scalar::Null(fp_types::NullKind::Null));
33        } else {
34            let s = format!("item_{}", i % 5000);
35            bytes.extend_from_slice(s.as_bytes());
36            eager.push(Scalar::Utf8(s));
37        }
38        offsets.push(bytes.len());
39    }
40
41    let mk = |name_col: Column| -> DataFrame {
42        let mut cols: BTreeMap<String, Column> = BTreeMap::new();
43        cols.insert("id".to_string(), Column::from_i64_values_owned(ids.clone()));
44        cols.insert("value".to_string(), Column::from_f64_values(vals.clone()));
45        cols.insert("name".to_string(), name_col);
46        DataFrame::new(Index::new_known_unique_int64_unit_range(0, n), cols).unwrap()
47    };
48    let frame_fast = mk(Column::from_utf8_values_with_validity(
49        bytes.clone(),
50        offsets.clone(),
51        validity,
52    ));
53    let frame_general = mk(Column::from_values(eager).unwrap());
54
55    let ctl = frame_general.column("name").unwrap();
56    assert!(
57        ctl.as_utf8_contiguous().is_none() && ctl.as_nullable_utf8_contiguous().is_none(),
58        "control name column must NOT be on the fast Utf8 path"
59    );
60
61    let mut best_t = u128::MAX;
62    for _ in 0..iters {
63        let t = std::time::Instant::now();
64        let s = fp_io::write_json_string(&frame_fast, JsonOrient::Records).unwrap();
65        best_t = best_t.min(t.elapsed().as_nanos());
66        std::hint::black_box(s.len());
67    }
68    let mut best_c = u128::MAX;
69    for _ in 0..iters {
70        let t = std::time::Instant::now();
71        let s = fp_io::write_json_string(&frame_general, JsonOrient::Records).unwrap();
72        best_c = best_c.min(t.elapsed().as_nanos());
73        std::hint::black_box(s.len());
74    }
75    let sf = fp_io::write_json_string(&frame_fast, JsonOrient::Records).unwrap();
76    let sg = fp_io::write_json_string(&frame_general, JsonOrient::Records).unwrap();
77    assert_eq!(sf, sg, "fast (nullable-Utf8) JSON must byte-match the serde tree");
78    println!(
79        "to_json(records) nullable-utf8 n={n} NEW(fast)={:>7.2}ms CONTROL(serde)={:>7.2}ms speedup={:.3}x",
80        best_t as f64 / 1e6,
81        best_c as f64 / 1e6,
82        best_c as f64 / best_t as f64,
83    );
84}