Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
Add cases for simd-json-derive and update to 0.7
Browse files Browse the repository at this point in the history
Signed-off-by: Heinz N. Gies <[email protected]>
  • Loading branch information
Licenser committed Jan 20, 2023
1 parent de23484 commit b82617c
Show file tree
Hide file tree
Showing 10 changed files with 470 additions and 64 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ edition = "2021"
publish = false

[dependencies]

getopts = "0.2"
jemallocator = "0.5"
rustc-serialize = { version = "0.3", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
serde_json = { version = "1.0", optional = true }
simd-json = { version = "0.7", optional = true}
time = "0.3"
simd-json = { version = "0.7", optional = true }
simd-json-derive = { version = "0.7", optional = true }

[features]
default = ["performance", "all-libs", "all-files"]
all-libs = ["lib-serde", "lib-rustc-serialize", "lib-simd-json"]
all-files = ["file-canada", "file-citm-catalog", "file-twitter"]
performance = ["parse-dom", "stringify-dom", "parse-struct", "stringify-struct"]
lib-serde = ["serde", "serde_json"]
lib-simd-json = ["serde", "simd-json"]
lib-simd-json = ["serde", "simd-json", "simd-json-derive"]
lib-rustc-serialize = ["rustc-serialize"]
file-canada = []
file-citm-catalog = []
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ are:

- [serde\_json] 1.0.72
- [rustc-serialize] 0.3.24
- [simd-json] 0.4.11 (this requires a modern x86 CPU for good results)
- [simd-json] 0.7.0 (this requires a modern x86 CPU for good results)

[nativejson-benchmark]: https://github.com/miloyip/nativejson-benchmark
[serde\_json]: https://github.com/serde-rs/json
[rustc-serialize]: https://github.com/rust-lang-nursery/rustc-serialize
[simd-json]: https://github.com/Licenser/simdjson-rs
[simd-json]: https://github.com/simd-lite/simd-json

#### `$ cargo run --release`

Expand Down
42 changes: 36 additions & 6 deletions src/canada.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,56 @@ use std::collections::BTreeMap as Map;
pub type Canada = FeatureCollection;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct FeatureCollection {
#[cfg_attr(feature = "serde", serde(rename = "type"))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(rename = "type")
)]
pub obj_type: ObjType,
pub features: Vec<Feature>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct Feature {
#[cfg_attr(feature = "serde", serde(rename = "type"))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(rename = "type")
)]
pub obj_type: ObjType,
pub properties: Map<String, String>,
pub geometry: Geometry,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct Geometry {
#[cfg_attr(feature = "serde", serde(rename = "type"))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(rename = "type")
)]
pub obj_type: ObjType,
pub coordinates: Vec<Vec<(Latitude, Longitude)>>,
}
Expand Down
36 changes: 35 additions & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use serde::de::{self, Deserialize, Deserializer, Unexpected};
#[cfg(feature = "serde")]
use serde::ser::{Serialize, Serializer};

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
pub struct Color(u32);

#[cfg(any(feature = "serde", feature = "lib-rustc-serialize"))]
Expand Down Expand Up @@ -58,6 +58,17 @@ impl Serialize for Color {
}
}

#[cfg(feature = "lib-simd-json")]
impl simd_json_derive::Serialize for Color {
fn json_write<W>(&self, writer: &mut W) -> std::io::Result<()>
where
W: std::io::Write,
{
let mut buf = MaybeUninit::uninit();
self.as_str(&mut buf).json_write(writer)
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Color {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
Expand Down Expand Up @@ -88,6 +99,29 @@ impl<'de> Deserialize<'de> for Color {
}
}

#[cfg(feature = "lib-simd-json")]
impl<'input> ::simd_json_derive::Deserialize<'input> for Color {
#[inline]
fn from_tape(tape: &mut ::simd_json_derive::Tape<'input>) -> simd_json::Result<Self>
where
Self: std::marker::Sized + 'input,
{
if let Some(::simd_json::Node::String(s)) = tape.next() {
if let Ok(hex) = u32::from_str_radix(s, 16) {
Ok(Color(hex))
} else {
dbg!(Err(::simd_json::Error::generic(
simd_json::ErrorType::ExpectedString,
)))
}
} else {
dbg!(Err(::simd_json::Error::generic(
simd_json::ErrorType::ExpectedString,
)))
}
}
}

#[cfg(feature = "lib-rustc-serialize")]
impl Encodable for Color {
fn encode<S>(&self, s: &mut S) -> Result<(), S::Error>
Expand Down
66 changes: 54 additions & 12 deletions src/copy/citm_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ use std::collections::BTreeMap as Map;
use crate::empty;
use crate::prim_str::PrimStr;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields, rename_all = "camelCase")
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct CitmCatalog {
pub area_names: Map<IdStr, String>,
pub audience_sub_category_names: Map<IdStr, String>,
Expand All @@ -28,11 +35,18 @@ pub struct CitmCatalog {
pub type Id = u32;
pub type IdStr = PrimStr<u32>;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields, rename_all = "camelCase")
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct Event {
pub description: (),
pub id: Id,
Expand All @@ -44,11 +58,18 @@ pub struct Event {
pub topic_ids: Vec<Id>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields, rename_all = "camelCase")
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct Performance {
pub event_id: Id,
pub id: Id,
Expand All @@ -61,32 +82,53 @@ pub struct Performance {
pub venue_code: String,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields, rename_all = "camelCase")
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct Price {
pub amount: u32,
pub audience_sub_category_id: Id,
pub seat_category_id: Id,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields, rename_all = "camelCase")
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct SeatCategory {
pub areas: Vec<Area>,
pub seat_category_id: Id,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields, rename_all = "camelCase")
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct Area {
pub area_id: Id,
pub block_ids: empty::Array,
Expand Down
Loading

0 comments on commit b82617c

Please sign in to comment.