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

Add cases for simd-json-derive #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -6,20 +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.12", optional = true }
time = "0.3"
simd-json = { version = "0.13", optional = true }
simd-json-derive = { version = "0.13", 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 = []
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`

42 changes: 36 additions & 6 deletions src/canada.rs
Original file line number Diff line number Diff line change
@@ -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)>>,
}
36 changes: 35 additions & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
@@ -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"))]
@@ -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>
@@ -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>
66 changes: 54 additions & 12 deletions src/copy/citm_catalog.rs
Original file line number Diff line number Diff line change
@@ -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>,
@@ -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,
@@ -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,
@@ -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,
215 changes: 184 additions & 31 deletions src/copy/twitter.rs
Original file line number Diff line number Diff line change
@@ -7,12 +7,22 @@ use crate::color::Color;
use crate::empty;
use crate::prim_str::PrimStr;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct Twitter {
pub statuses: Vec<Status>,
pub search_metadata: SearchMetadata,
@@ -23,12 +33,22 @@ pub type ShortId = u32;
pub type LongIdStr = PrimStr<LongId>;
pub type ShortIdStr = PrimStr<ShortId>;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct Status {
pub metadata: Metadata,
pub created_at: String,
@@ -57,23 +77,43 @@ pub struct Status {
pub lang: LanguageCode,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct Metadata {
pub result_type: ResultType,
pub iso_language_code: LanguageCode,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct User {
pub id: ShortId,
pub id_str: ShortIdStr,
@@ -117,56 +157,106 @@ pub struct User {
pub notifications: bool,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct UserEntities {
pub url: Option<UserUrl>,
pub description: UserEntitiesDescription,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct UserUrl {
pub urls: Vec<Url>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct Url {
pub url: String,
pub expanded_url: String,
pub display_url: String,
pub indices: Indices,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct UserEntitiesDescription {
pub urls: Vec<Url>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct StatusEntities {
pub hashtags: Vec<Hashtag>,
pub symbols: empty::Array,
@@ -175,23 +265,43 @@ pub struct StatusEntities {
pub media: Option<Vec<Media>>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct Hashtag {
pub text: String,
pub indices: Indices,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct UserMention {
pub screen_name: String,
pub name: String,
@@ -200,8 +310,18 @@ pub struct UserMention {
pub indices: Indices,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[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 Media {
pub id: LongId,
pub id_str: LongIdStr,
@@ -211,32 +331,55 @@ pub struct Media {
pub url: String,
pub display_url: String,
pub expanded_url: String,
#[cfg_attr(feature = "serde", serde(rename = "type"))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(rename = "type")
)]
pub media_type: String,
pub sizes: Sizes,
pub source_status_id: Option<LongId>,
pub source_status_id_str: Option<LongIdStr>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct Sizes {
pub medium: Size,
pub small: Size,
pub thumb: Size,
pub large: Size,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct Size {
pub w: u16,
pub h: u16,
@@ -245,12 +388,22 @@ pub struct Size {

pub type Indices = (u8, u8);

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
derive(Serialize, Deserialize)
)]
#[cfg_attr(
any(feature = "serde", feature = "lib-simd-json"),
serde(deny_unknown_fields)
)]
#[cfg_attr(
feature = "lib-rustc-serialize",
derive(RustcEncodable, RustcDecodable)
)]
#[cfg_attr(
feature = "lib-simd-json",
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
pub struct SearchMetadata {
pub completed_in: f32,
pub max_id: LongId,
26 changes: 26 additions & 0 deletions src/empty.rs
Original file line number Diff line number Diff line change
@@ -45,6 +45,32 @@ 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"[]")
}
}

impl<'input> simd_json_derive::Deserialize<'input> for Array {
#[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::Array { len: 0, .. }) = tape.next() {
Ok(Self)
} else {
Err(simd_json::Error::generic(
simd_json::ErrorType::ExpectedArray,
))
}
}
}

#[cfg(feature = "lib-rustc-serialize")]
impl Encodable for Array {
fn encode<S>(&self, s: &mut S) -> Result<(), S::Error>
38 changes: 36 additions & 2 deletions src/enums.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#[macro_export]
macro_rules! enum_str {
($name:ident { $($variant:ident($str:expr), )* }) => {
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
pub enum $name {
$($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 {
@@ -52,6 +52,40 @@ macro_rules! enum_str {
}
}

#[cfg(feature = "lib-simd-json")]
impl ::simd_json_derive::Serialize for $name {
#[inline]
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-simd-json")]
impl<'input> ::simd_json_derive::Deserialize<'input> for $name {
#[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() {
match s {
$( $str => Ok($name::$variant), )*
_ => Err(::simd_json::Error::generic(
simd_json::ErrorType::ExpectedString,
)),
}

} else {
Err(::simd_json::Error::generic(
simd_json::ErrorType::ExpectedString,
))
}
}
}


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

let num_trials = num_trials().unwrap_or(256);
let mut buffers = simd_json::Buffers::new(4096);

print!("{:22}", $path);
io::stdout().flush().unwrap();
@@ -150,7 +152,7 @@ macro_rules! bench_file_simd_json {
for _ in 0..num_trials {
data.as_mut_slice().clone_from_slice(contents.as_slice());
let mut timer = benchmark.start();
let _parsed = simd_json_parse_dom(&mut data).unwrap();
let _parsed = simd_json_parse_dom(&mut data, &mut buffers).unwrap();
timer.stop();
}
let dur = benchmark.min_elapsed();
@@ -164,12 +166,12 @@ macro_rules! bench_file_simd_json {
{
let len = contents.len();
let mut data = contents.clone();
let dom = simd_json_parse_dom(&mut data).unwrap();
let dom = simd_json_parse_dom(&mut data, &mut buffers).unwrap();
let dur = timer::bench_with_buf(num_trials, len, |out| {
simd_json::Writable::write(&dom, out).unwrap()
simd_json::prelude::Writable::write(&dom, out).unwrap()
});
let mut serialized = Vec::new();
simd_json::Writable::write(&dom, &mut serialized).unwrap();
simd_json::prelude::Writable::write(&dom, &mut serialized).unwrap();
print!("{:6} MB/s", throughput(dur, serialized.len()));
io::stdout().flush().unwrap();
}
@@ -184,14 +186,30 @@ macro_rules! bench_file_simd_json {
for _ in 0..num_trials {
data.as_mut_slice().clone_from_slice(contents.as_slice());
let mut timer = benchmark.start();
let _parsed: $structure = simd_json_parse_struct(&mut data).unwrap();
let _parsed: $structure = simd_json_parse_struct(&mut data, &mut buffers).unwrap();
timer.stop();
}
let dur = benchmark.min_elapsed();
print!("{:6} MB/s", throughput(dur, contents.len()));
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, &mut buffers).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!();
}
}
@@ -292,17 +310,34 @@ where
feature = "lib-simd-json",
any(feature = "parse-dom", feature = "stringify-dom")
))]
fn simd_json_parse_dom(bytes: &mut [u8]) -> simd_json::Result<simd_json::BorrowedValue> {
simd_json::to_borrowed_value(bytes)
fn simd_json_parse_dom<'input>(
bytes: &'input mut [u8],
buffers: &mut simd_json::Buffers,
) -> simd_json::Result<simd_json::BorrowedValue<'input>> {
simd_json::to_borrowed_value_with_buffers(bytes, buffers)
}

// #[cfg(all(
// feature = "lib-simd-json",
// any(feature = "parse-struct", feature = "stringify-struct")
// ))]
// fn simd_json_parse_struct<'de, T>(bytes: &'de mut [u8]) -> simd_json::Result<T>
// where
// T: serde::Deserialize<'de>,
// {
// simd_json::serde::from_slice(bytes)
// }

#[cfg(all(
feature = "lib-simd-json",
any(feature = "parse-struct", feature = "stringify-struct")
))]
fn simd_json_parse_struct<'de, T>(bytes: &'de mut [u8]) -> simd_json::Result<T>
fn simd_json_parse_struct<'de, T>(
bytes: &'de mut [u8],
buffers: &mut simd_json::Buffers,
) -> simd_json::Result<T>
where
T: serde::Deserialize<'de>,
T: simd_json_derive::Deserialize<'de> + 'de,
{
simd_json::serde::from_slice(bytes)
T::from_slice_with_buffers(bytes, buffers)
}
47 changes: 47 additions & 0 deletions src/prim_str.rs
Original file line number Diff line number Diff line change
@@ -65,6 +65,53 @@ 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, r#""{}""#, self.0)
}
}

#[cfg(feature = "lib-simd-json")]
impl<T> simd_json_derive::SerializeAsKey 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, r#""{}""#, self.0)
}
}

impl<'input, T> simd_json_derive::Deserialize<'input> for PrimStr<T>
where
T: Copy + Ord + Display + FromStr,
{
#[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() {
Ok(PrimStr(FromStr::from_str(s).map_err(|_e| {
simd_json::Error::generic(simd_json::ErrorType::Serde("not a number".into()))
})?))
} else {
Err(simd_json::Error::generic(
simd_json::ErrorType::ExpectedNull,
))
}
}
}

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