pub fn write_json_string(
frame: &DataFrame,
orient: JsonOrient,
) -> Result<String, IoError>Examples found in repository?
examples/bench_to_json_dt.rs (line 25)
9fn main() {
10 let a: Vec<String> = std::env::args().collect();
11 let n: usize = a.get(1).and_then(|s| s.parse().ok()).unwrap_or(1_000_000);
12 let ints: Vec<i64> = (0..n as i64)
13 .map(|i| (i.wrapping_mul(2_654_435_761)) % 1_000_000)
14 .collect();
15 let base = 946_684_800_000_000_000i64;
16 let step = 37_000_000_000i64;
17 let dts: Vec<i64> = (0..n as i64).map(|i| base + i * step).collect();
18 let mut cols: BTreeMap<String, Column> = BTreeMap::new();
19 cols.insert("a".to_string(), Column::from_i64_values(ints));
20 cols.insert("t".to_string(), Column::from_datetime64_values(dts));
21 let frame = DataFrame::new(Index::new_known_unique_int64_unit_range(0, n), cols).unwrap();
22 let mut best = u128::MAX;
23 for _ in 0..6 {
24 let t = std::time::Instant::now();
25 let s = fp_io::write_json_string(&frame, JsonOrient::Records).unwrap();
26 std::hint::black_box(s.len());
27 let e = t.elapsed().as_nanos();
28 if e < best {
29 best = e;
30 }
31 }
32 println!(
33 "to_json(records) +dt n={n}: best={best}ns ({:.2}ms)",
34 best as f64 / 1e6
35 );
36}More examples
examples/bench_to_json_str.rs (line 29)
9fn main() {
10 let a: Vec<String> = std::env::args().collect();
11 let n: usize = a.get(1).and_then(|s| s.parse().ok()).unwrap_or(1_000_000);
12 let ints: Vec<i64> = (0..n as i64)
13 .map(|i| (i.wrapping_mul(2_654_435_761)) % 1_000_000)
14 .collect();
15 // Contiguous Utf8 backing (what read_csv / string ops produce).
16 let mut sbytes: Vec<u8> = Vec::new();
17 let mut soff: Vec<usize> = vec![0];
18 for i in 0..n {
19 sbytes.extend_from_slice(format!("item_{}", i % 1000).as_bytes());
20 soff.push(sbytes.len());
21 }
22 let mut cols: BTreeMap<String, Column> = BTreeMap::new();
23 cols.insert("a".to_string(), Column::from_i64_values(ints));
24 cols.insert("s".to_string(), Column::from_utf8_contiguous(sbytes, soff));
25 let frame = DataFrame::new(Index::new_known_unique_int64_unit_range(0, n), cols).unwrap();
26 let mut best = u128::MAX;
27 for _ in 0..6 {
28 let t = std::time::Instant::now();
29 let s = fp_io::write_json_string(&frame, JsonOrient::Records).unwrap();
30 std::hint::black_box(s.len());
31 let e = t.elapsed().as_nanos();
32 if e < best {
33 best = e;
34 }
35 }
36 println!(
37 "to_json(records) +str n={n}: best={best}ns ({:.2}ms)",
38 best as f64 / 1e6
39 );
40}examples/bench_to_json.rs (line 30)
9fn main() {
10 let a: Vec<String> = std::env::args().collect();
11 let n: usize = a.get(1).and_then(|s| s.parse().ok()).unwrap_or(1_000_000);
12 let ints: Vec<i64> = (0..n as i64)
13 .map(|i| (i.wrapping_mul(2_654_435_761)) % 1_000_000)
14 .collect();
15 let floats: Vec<f64> = (0..n).map(|i| (i as f64) * 0.5 - 100.0).collect();
16 let orient = match a.get(2).map(String::as_str).unwrap_or("records") {
17 "values" => JsonOrient::Values,
18 "split" => JsonOrient::Split,
19 "columns" => JsonOrient::Columns,
20 "index" => JsonOrient::Index,
21 _ => JsonOrient::Records,
22 };
23 let mut cols: BTreeMap<String, Column> = BTreeMap::new();
24 cols.insert("a".to_string(), Column::from_i64_values(ints));
25 cols.insert("b".to_string(), Column::from_f64_values(floats));
26 let frame = DataFrame::new(Index::new_known_unique_int64_unit_range(0, n), cols).unwrap();
27 let mut best = u128::MAX;
28 for _ in 0..6 {
29 let t = std::time::Instant::now();
30 let s = fp_io::write_json_string(&frame, orient).unwrap();
31 std::hint::black_box(s.len());
32 let e = t.elapsed().as_nanos();
33 if e < best {
34 best = e;
35 }
36 }
37 println!(
38 "to_json({orient:?}) n={n}: best={best}ns ({:.2}ms)",
39 best as f64 / 1e6
40 );
41}examples/bench_to_json_null_utf8.rs (line 64)
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}examples/bench_to_json_null_num.rs (line 77)
15fn main() {
16 let a: Vec<String> = std::env::args().collect();
17 let n: usize = a.get(1).and_then(|s| s.parse().ok()).unwrap_or(1_000_000);
18 let iters: usize = a.get(2).and_then(|s| s.parse().ok()).unwrap_or(6);
19
20 let ids: Vec<i64> = (0..n as i64).collect();
21 let price: Vec<f64> = (0..n).map(|i| (i % 100000) as f64 / 100.0).collect();
22 let mut price_v = ValidityMask::all_valid(n);
23 let qty: Vec<i64> = (0..n as i64).map(|i| i % 1000).collect();
24 let mut qty_v = ValidityMask::all_valid(n);
25 let active: Vec<bool> = (0..n).map(|i| i % 2 == 0).collect();
26 let mut active_v = ValidityMask::all_valid(n);
27 let mut nb: Vec<u8> = Vec::with_capacity(n * 8);
28 let mut no: Vec<usize> = Vec::with_capacity(n + 1);
29 no.push(0);
30 let mut names_eager: Vec<Scalar> = Vec::with_capacity(n);
31 for i in 0..n {
32 if i % 7 == 0 {
33 price_v.set(i, false);
34 }
35 if i % 5 == 0 {
36 qty_v.set(i, false);
37 }
38 if i % 3 == 0 {
39 active_v.set(i, false);
40 }
41 let s = format!("item_{}", i % 5000);
42 nb.extend_from_slice(s.as_bytes());
43 no.push(nb.len());
44 names_eager.push(Scalar::Utf8(s));
45 }
46
47 let mk = |name_col: Column| -> DataFrame {
48 let mut cols: BTreeMap<String, Column> = BTreeMap::new();
49 cols.insert("id".to_string(), Column::from_i64_values_owned(ids.clone()));
50 cols.insert(
51 "price".to_string(),
52 Column::from_f64_values_with_validity(price.clone(), price_v.clone()),
53 );
54 cols.insert(
55 "qty".to_string(),
56 Column::from_i64_values_with_validity(qty.clone(), qty_v.clone()),
57 );
58 cols.insert(
59 "active".to_string(),
60 Column::from_bool_values_with_validity(active.clone(), active_v.clone()),
61 );
62 cols.insert("name".to_string(), name_col);
63 DataFrame::new(Index::new_known_unique_int64_unit_range(0, n), cols).unwrap()
64 };
65 let frame_fast = mk(Column::from_utf8_contiguous(nb.clone(), no.clone()));
66 let frame_general = mk(Column::from_values(names_eager).unwrap());
67
68 let ctl = frame_general.column("name").unwrap();
69 assert!(
70 ctl.as_utf8_contiguous().is_none() && ctl.as_nullable_utf8_contiguous().is_none(),
71 "control name column must force the serde tree"
72 );
73
74 let mut best_t = u128::MAX;
75 for _ in 0..iters {
76 let t = std::time::Instant::now();
77 let s = fp_io::write_json_string(&frame_fast, JsonOrient::Records).unwrap();
78 best_t = best_t.min(t.elapsed().as_nanos());
79 std::hint::black_box(s.len());
80 }
81 let mut best_c = u128::MAX;
82 for _ in 0..iters {
83 let t = std::time::Instant::now();
84 let s = fp_io::write_json_string(&frame_general, JsonOrient::Records).unwrap();
85 best_c = best_c.min(t.elapsed().as_nanos());
86 std::hint::black_box(s.len());
87 }
88 let sf = fp_io::write_json_string(&frame_fast, JsonOrient::Records).unwrap();
89 let sg = fp_io::write_json_string(&frame_general, JsonOrient::Records).unwrap();
90 assert_eq!(sf, sg, "fast (FN/IN) JSON must byte-match the serde tree");
91 println!(
92 "to_json(records) nullable-numeric n={n} NEW(fast)={:>7.2}ms CONTROL(serde)={:>7.2}ms speedup={:.3}x",
93 best_t as f64 / 1e6,
94 best_c as f64 / 1e6,
95 best_c as f64 / best_t as f64,
96 );
97}