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
Browse files Browse the repository at this point in the history
  • Loading branch information
Licenser committed Jul 6, 2020
1 parent ca3efde commit b166f91
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
rust: [nightly, beta, stable, 1.37.0]
rust: [nightly, beta, stable, 1.42.0]
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@master
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ rustc-serialize = { version = "0.3", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
serde-bench = { version = "0.0.7", optional = true }
serde_json = { version = "1.0", optional = true }
simd-json = { version = "0.1", optional = true}
simd-json = { version = "0.3", optional = true }
simd-json-derive = { version = "0.1", optional = true }
time = "0.1"

[features]
Expand All @@ -22,7 +23,7 @@ all-libs = ["lib-serde", "lib-json-rust", "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-json-rust = ["json"]
lib-rustc-serialize = ["rustc-serialize"]
file-canada = []
Expand Down
3 changes: 3 additions & 0 deletions src/canada.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub type Canada = FeatureCollection;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct FeatureCollection {
#[cfg_attr(feature = "serde", serde(rename = "type"))]
pub obj_type: ObjType,
Expand All @@ -15,6 +16,7 @@ pub struct FeatureCollection {

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct Feature {
#[cfg_attr(feature = "serde", serde(rename = "type"))]
pub obj_type: ObjType,
Expand All @@ -24,6 +26,7 @@ pub struct Feature {

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct Geometry {
#[cfg_attr(feature = "serde", serde(rename = "type"))]
pub obj_type: ObjType,
Expand Down
11 changes: 11 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
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
6 changes: 6 additions & 0 deletions src/copy/citm_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::prim_str::PrimStr;
feature = "serde",
serde(deny_unknown_fields, rename_all = "camelCase")
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct CitmCatalog {
pub area_names: Map<IdStr, String>,
pub audience_sub_category_names: Map<IdStr, String>,
Expand All @@ -33,6 +34,7 @@ pub type IdStr = PrimStr<u32>;
feature = "serde",
serde(deny_unknown_fields, rename_all = "camelCase")
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct Event {
pub description: (),
pub id: Id,
Expand All @@ -49,6 +51,7 @@ pub struct Event {
feature = "serde",
serde(deny_unknown_fields, rename_all = "camelCase")
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct Performance {
pub event_id: Id,
pub id: Id,
Expand All @@ -66,6 +69,7 @@ pub struct Performance {
feature = "serde",
serde(deny_unknown_fields, rename_all = "camelCase")
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct Price {
pub amount: u32,
pub audience_sub_category_id: Id,
Expand All @@ -77,6 +81,7 @@ pub struct Price {
feature = "serde",
serde(deny_unknown_fields, rename_all = "camelCase")
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct SeatCategory {
pub areas: Vec<Area>,
pub seat_category_id: Id,
Expand All @@ -87,6 +92,7 @@ pub struct SeatCategory {
feature = "serde",
serde(deny_unknown_fields, rename_all = "camelCase")
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct Area {
pub area_id: Id,
pub block_ids: empty::Array,
Expand Down
15 changes: 15 additions & 0 deletions src/copy/twitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::prim_str::PrimStr;
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct Twitter {
pub statuses: Vec<Status>,
pub search_metadata: SearchMetadata,
Expand All @@ -27,6 +28,7 @@ pub type ShortIdStr = PrimStr<ShortId>;
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct Status {
pub metadata: Metadata,
pub created_at: String,
Expand Down Expand Up @@ -61,6 +63,7 @@ pub struct Status {
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct Metadata {
pub result_type: ResultType,
pub iso_language_code: LanguageCode,
Expand All @@ -72,6 +75,7 @@ pub struct Metadata {
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct User {
pub id: ShortId,
pub id_str: ShortIdStr,
Expand Down Expand Up @@ -121,6 +125,7 @@ pub struct User {
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct UserEntities {
pub url: Option<UserUrl>,
pub description: UserEntitiesDescription,
Expand All @@ -132,6 +137,7 @@ pub struct UserEntities {
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct UserUrl {
pub urls: Vec<Url>,
}
Expand All @@ -142,6 +148,7 @@ pub struct UserUrl {
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct Url {
pub url: String,
pub expanded_url: String,
Expand All @@ -155,6 +162,7 @@ pub struct Url {
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct UserEntitiesDescription {
pub urls: Vec<Url>,
}
Expand All @@ -165,6 +173,7 @@ pub struct UserEntitiesDescription {
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct StatusEntities {
pub hashtags: Vec<Hashtag>,
pub symbols: empty::Array,
Expand All @@ -179,6 +188,7 @@ pub struct StatusEntities {
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct Hashtag {
pub text: String,
pub indices: Indices,
Expand All @@ -190,6 +200,7 @@ pub struct Hashtag {
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct UserMention {
pub screen_name: String,
pub name: String,
Expand All @@ -200,6 +211,7 @@ pub struct UserMention {

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct Media {
pub id: LongId,
pub id_str: LongIdStr,
Expand All @@ -222,6 +234,7 @@ pub struct Media {
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct Sizes {
pub medium: Size,
pub small: Size,
Expand All @@ -235,6 +248,7 @@ pub struct Sizes {
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct Size {
pub w: u16,
pub h: u16,
Expand All @@ -249,6 +263,7 @@ pub type Indices = (u8, u8);
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
pub struct SearchMetadata {
pub completed_in: f32,
pub max_id: LongId,
Expand Down
10 changes: 10 additions & 0 deletions src/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ impl<'de> Deserialize<'de> for Array {
}
}

#[cfg(feature = "lib-simd-json")]
impl simd_json_derive::Serialize for Array {
fn json_write<W>(&self, writer: &mut W) -> std::io::Result<()>
where
W: std::io::Write,
{
writer.write_all(b"[]")
}
}

#[cfg(feature = "lib-rustc-serialize")]
impl Encodable for Array {
fn encode<S>(&self, s: &mut S) -> Result<(), S::Error>
Expand Down
11 changes: 10 additions & 1 deletion src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ macro_rules! enum_str {
$($variant,)*
}

#[cfg(any(feature = "serde", feature = "lib-rustc-serialize"))]
#[cfg(any(feature = "lib-simd-json", feature = "lib-serde", feature = "lib-rustc-serialize"))]
impl $name {
fn as_str(self) -> &'static str {
match self {
Expand Down Expand Up @@ -52,6 +52,15 @@ macro_rules! enum_str {
}
}

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

#[cfg(feature = "lib-rustc-serialize")]
impl ::rustc_serialize::Encodable for $name {
fn encode<S>(&self, s: &mut S) -> Result<(), S::Error>
Expand Down
18 changes: 18 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ macro_rules! bench_file_simd_json {
path: $path:expr,
structure: $structure:ty,
} => {

let num_trials = num_trials().unwrap_or(256);

print!("{:22}", $path);
Expand Down Expand Up @@ -160,6 +161,7 @@ macro_rules! bench_file_simd_json {

#[cfg(feature = "stringify-dom")]
{
use simd_json::prelude::*;
let len = contents.len();
let mut data = contents.clone();
let dom = simd_json_parse_dom(&mut data).unwrap();
Expand Down Expand Up @@ -190,6 +192,22 @@ macro_rules! bench_file_simd_json {
io::stdout().flush().unwrap();
}

#[cfg(feature = "stringify-struct")]
{
use simd_json_derive::Serialize;
let len = contents.len();
let mut data = contents.clone();
let parsed: $structure = simd_json_parse_struct(&mut data).unwrap();
let dur = timer::bench_with_buf(num_trials, len, |out| {
parsed.json_write(out).unwrap();
});
let mut serialized = Vec::new();
parsed.json_write(&mut serialized).unwrap();

print!("{:6} MB/s", throughput(dur, serialized.len()));
io::stdout().flush().unwrap();
}

println!();
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/prim_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ where
}
}

#[cfg(feature = "lib-simd-json")]
impl<T> simd_json_derive::Serialize for PrimStr<T>
where
T: Copy + Ord + Display + FromStr,
{
fn json_write<W>(&self, writer: &mut W) -> std::io::Result<()>
where
W: std::io::Write,
{
write!(writer, "{}", self.0)
}
}

#[cfg(feature = "lib-rustc-serialize")]
impl<T> Encodable for PrimStr<T>
where
Expand Down

0 comments on commit b166f91

Please sign in to comment.