Skip to content

Commit 89e47b5

Browse files
Update JSON example (#93)
1 parent 389dd8d commit 89e47b5

File tree

1 file changed

+16
-51
lines changed

1 file changed

+16
-51
lines changed

examples/json.rs

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,6 @@ impl std::ops::Deref for Number {
5454
}
5555
}
5656

57-
/// Stand-in for `Vec<Json>`.
58-
///
59-
/// This approach uses `indexes` which contains discriminants, which should allow
60-
/// an efficient representation of offset information. Unfortunately, both `arrays`
61-
/// and `objects` just list their intended offsets directly, rather than encode the
62-
/// offsets using unary degree sequences, which seemed hard to thread through the
63-
/// other abstractions. Their `Vec<usize>` container can probably be made smarter,
64-
/// in particular by an `Option<usize>` container where `None` indicates increment.
65-
// struct Jsons {
66-
// pub indexes: Vec<JsonDiscriminant>, // Container for `JsonDiscriminant`.
67-
// pub numbers: Vec<serde_json::Number>, // Any `Number` container.
68-
// pub strings: Strings, // Any `String` container.
69-
// pub arrays: Vecs<Vec<usize>>, // Any `Vec<usize>` container.
70-
// pub objects Vecs<(Lookbacks<Strings>, Vec<usize>)>,
71-
// }
72-
7357
/// Stand-in for `Vec<Json>`.
7458
///
7559
/// The `roots` vector indicates the root of each stored `Json`.
@@ -92,7 +76,7 @@ pub enum JsonsRef<'a> {
9276
Null,
9377
Bool(bool),
9478
Number(&'a Number),
95-
String(&'a str),
79+
String(&'a [u8]),
9680
Array(ArrRef<'a>),
9781
Object(ObjRef<'a>),
9882
}
@@ -119,14 +103,14 @@ impl<'a> PartialEq<Json> for JsonsRef<'a> {
119103
(JsonsRef::Null, Json::Null) => { true },
120104
(JsonsRef::Bool(b0), Json::Bool(b1)) => { b0 == b1 },
121105
(JsonsRef::Number(n0), Json::Number(n1)) => { *n0 == n1 },
122-
(JsonsRef::String(s0), Json::String(s1)) => { *s0 == s1 },
106+
(JsonsRef::String(s0), Json::String(s1)) => { *s0 == s1.as_bytes() },
123107
(JsonsRef::Array(a0), Json::Array(a1)) => {
124108
let slice: columnar::Slice<&Vec<JsonIdx>> = (&a0.store.arrays).get(a0.index);
125109
slice.len() == a1.len() && slice.into_iter().zip(a1).all(|(a,b)| a0.store.dereference(*a).eq(b))
126110
},
127111
(JsonsRef::Object(o0), Json::Object(o1)) => {
128112
let slice: columnar::Slice<&(_, _)> = (&o0.store.objects).get(o0.index);
129-
slice.len() == o1.len() && slice.into_iter().zip(o1).all(|((xs, xv),(ys, yv))| xs == ys && o0.store.dereference(*xv).eq(yv))
113+
slice.len() == o1.len() && slice.into_iter().zip(o1).all(|((xs, xv),(ys, yv))| xs == ys.as_bytes() && o0.store.dereference(*xv).eq(yv))
130114
},
131115
_ => { false }
132116
}
@@ -167,12 +151,6 @@ impl Len for Jsons {
167151
}
168152
}
169153

170-
// impl IndexGat for Jsons {
171-
// type Ref<'a> = JsonsRef<'a>;
172-
// fn get(&self, index: usize) -> Self::Ref<'_> {
173-
// self.dereference(self.roots[index])
174-
// }
175-
// }
176154
impl<'a> Index for &'a Jsons {
177155
type Ref = JsonsRef<'a>;
178156
#[inline(always)] fn get(&self, index: usize) -> Self::Ref {
@@ -229,7 +207,7 @@ impl<'a> JsonQueues<'a> {
229207
JsonIdx::Number(self.store.numbers.len() - 1)
230208
},
231209
Json::String(s) => {
232-
self.store.strings.push(s);
210+
self.store.strings.push(s.as_bytes());
233211
JsonIdx::String(self.store.strings.len() - 1)
234212
},
235213
Json::Array(a) => {
@@ -258,7 +236,7 @@ impl<'a> JsonQueues<'a> {
258236
while let Some(pairs) = self.obj_todo.front().cloned() {
259237
Extend::extend(&mut temp, pairs.iter().map(|(_,v)| self.copy(v)));
260238
self.obj_todo.pop_front();
261-
self.store.objects.push_iter(temp.drain(..).zip(pairs).map(|(v,(s,_))| (s, v)));
239+
self.store.objects.push_iter(temp.drain(..).zip(pairs).map(|(v,(s,_))| (s.as_bytes(), v)));
262240
}
263241
}
264242
}
@@ -307,35 +285,22 @@ fn main() {
307285
let time = timer.elapsed();
308286
println!("{:?}\tjson_cols cloned", time);
309287

288+
// Bincode round-trip (serde-based).
310289
let timer = std::time::Instant::now();
311-
use serde::ser::Serialize;
312-
let mut encoded0 = Vec::new();
313-
let mut serializer = rmp_serde::Serializer::new(&mut encoded0).with_bytes(rmp_serde::config::BytesMode::ForceAll);
314-
values.serialize(&mut serializer).unwrap();
315-
let time = timer.elapsed();
316-
println!("{:?}\tjson_vals encode ({} bytes; msgpack)", time, encoded0.len());
317-
let timer = std::time::Instant::now();
318-
let decoded0: Vec<Json> = rmp_serde::from_slice(&encoded0[..]).unwrap();
319-
let time = timer.elapsed();
320-
println!("{:?}\tjson_vals decode", time);
321-
322-
let timer = std::time::Instant::now();
323-
let mut encoded1 = Vec::new();
324-
let mut serializer = rmp_serde::Serializer::new(&mut encoded1).with_bytes(rmp_serde::config::BytesMode::ForceAll);
325-
json_cols.serialize(&mut serializer).unwrap();
326-
let time = timer.elapsed();
327-
println!("{:?}\tjson_cols encode ({} bytes; msgpack)", time, encoded1.len());
328-
let timer = std::time::Instant::now();
329-
let decoded1: Jsons = rmp_serde::from_slice(&encoded1[..]).unwrap();
290+
let encoded: Vec<u8> = bincode::serialize(&json_cols).unwrap();
330291
let time = timer.elapsed();
331-
println!("{:?}\tjson_cols decode", time);
292+
println!("{:?}\tjson_cols encode ({} bytes; bincode)", time, encoded.len());
332293

333294
let timer = std::time::Instant::now();
334-
let encoded2: Vec<u8> = bincode::serialize(&json_cols).unwrap();
295+
let decoded: Jsons = bincode::deserialize(&encoded[..]).unwrap();
335296
let time = timer.elapsed();
336-
println!("{:?}\tjson_cols encode ({} bytes; bincode)", time, encoded2.len());
297+
println!("{:?}\tjson_cols decode (bincode)", time);
337298

338-
assert_eq!(values, decoded0);
339-
assert_eq!(json_cols, decoded1);
299+
assert_eq!(json_cols, decoded);
340300

301+
// TODO: Columnar binary round-trip via AsBytes/FromBytes.
302+
// This requires implementing Borrow for Jsons, which is blocked on
303+
// Vec<Number> (serde_json::Number is not columnar-friendly).
304+
// Converting numbers to a columnar representation (e.g. f64 + tag)
305+
// would unblock this path.
341306
}

0 commit comments

Comments
 (0)