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

Commit 1d0b864

Browse files
committed
Add cases for simd-json-derive
1 parent ca3efde commit 1d0b864

File tree

10 files changed

+92
-6
lines changed

10 files changed

+92
-6
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
fail-fast: false
1414
matrix:
15-
rust: [nightly, beta, stable, 1.37.0]
15+
rust: [nightly, beta, stable, 1.42.0]
1616
steps:
1717
- uses: actions/checkout@v2
1818
- uses: dtolnay/rust-toolchain@master

Cargo.toml

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ publish = false
77

88
[dependencies]
99
getopts = "0.2"
10-
jemallocator = "0.3"
10+
mimalloc = "0.1"
1111
json = { version = "0.12", optional = true }
1212
rustc-serialize = { version = "0.3", optional = true }
1313
serde = { version = "1.0", features = ["derive"], optional = true }
1414
serde-bench = { version = "0.0.7", optional = true }
1515
serde_json = { version = "1.0", optional = true }
16-
simd-json = { version = "0.1", optional = true}
16+
simd-json = { version = "0.3", optional = true }
17+
simd-json-derive = { version = "0.1", optional = true }
1718
time = "0.1"
1819

1920
[features]
@@ -22,7 +23,7 @@ all-libs = ["lib-serde", "lib-json-rust", "lib-rustc-serialize", "lib-simd-json"
2223
all-files = ["file-canada", "file-citm-catalog", "file-twitter"]
2324
performance = ["parse-dom", "stringify-dom", "parse-struct", "stringify-struct"]
2425
lib-serde = ["serde", "serde_json"]
25-
lib-simd-json = ["serde", "simd-json"]
26+
lib-simd-json = ["serde", "simd-json", "simd-json-derive"]
2627
lib-json-rust = ["json"]
2728
lib-rustc-serialize = ["rustc-serialize"]
2829
file-canada = []

src/canada.rs

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub type Canada = FeatureCollection;
77

88
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
99
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
10+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
1011
pub struct FeatureCollection {
1112
#[cfg_attr(feature = "serde", serde(rename = "type"))]
1213
pub obj_type: ObjType,
@@ -15,6 +16,7 @@ pub struct FeatureCollection {
1516

1617
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1718
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
19+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
1820
pub struct Feature {
1921
#[cfg_attr(feature = "serde", serde(rename = "type"))]
2022
pub obj_type: ObjType,
@@ -24,6 +26,7 @@ pub struct Feature {
2426

2527
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2628
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
29+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
2730
pub struct Geometry {
2831
#[cfg_attr(feature = "serde", serde(rename = "type"))]
2932
pub obj_type: ObjType,

src/color.rs

+11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ impl Serialize for Color {
5858
}
5959
}
6060

61+
#[cfg(feature = "lib-simd-json")]
62+
impl simd_json_derive::Serialize for Color {
63+
fn json_write<W>(&self, writer: &mut W) -> std::io::Result<()>
64+
where
65+
W: std::io::Write,
66+
{
67+
let mut buf = MaybeUninit::uninit();
68+
self.as_str(&mut buf).json_write(writer)
69+
}
70+
}
71+
6172
#[cfg(feature = "serde")]
6273
impl<'de> Deserialize<'de> for Color {
6374
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>

src/copy/citm_catalog.rs

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::prim_str::PrimStr;
1111
feature = "serde",
1212
serde(deny_unknown_fields, rename_all = "camelCase")
1313
)]
14+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
1415
pub struct CitmCatalog {
1516
pub area_names: Map<IdStr, String>,
1617
pub audience_sub_category_names: Map<IdStr, String>,
@@ -33,6 +34,7 @@ pub type IdStr = PrimStr<u32>;
3334
feature = "serde",
3435
serde(deny_unknown_fields, rename_all = "camelCase")
3536
)]
37+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
3638
pub struct Event {
3739
pub description: (),
3840
pub id: Id,
@@ -49,6 +51,7 @@ pub struct Event {
4951
feature = "serde",
5052
serde(deny_unknown_fields, rename_all = "camelCase")
5153
)]
54+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
5255
pub struct Performance {
5356
pub event_id: Id,
5457
pub id: Id,
@@ -66,6 +69,7 @@ pub struct Performance {
6669
feature = "serde",
6770
serde(deny_unknown_fields, rename_all = "camelCase")
6871
)]
72+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
6973
pub struct Price {
7074
pub amount: u32,
7175
pub audience_sub_category_id: Id,
@@ -77,6 +81,7 @@ pub struct Price {
7781
feature = "serde",
7882
serde(deny_unknown_fields, rename_all = "camelCase")
7983
)]
84+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
8085
pub struct SeatCategory {
8186
pub areas: Vec<Area>,
8287
pub seat_category_id: Id,
@@ -87,6 +92,7 @@ pub struct SeatCategory {
8792
feature = "serde",
8893
serde(deny_unknown_fields, rename_all = "camelCase")
8994
)]
95+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
9096
pub struct Area {
9197
pub area_id: Id,
9298
pub block_ids: empty::Array,

src/copy/twitter.rs

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::prim_str::PrimStr;
1111
feature = "lib-rustc-serialize",
1212
derive(RustcEncodable, RustcDecodable)
1313
)]
14+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
1415
pub struct Twitter {
1516
pub statuses: Vec<Status>,
1617
pub search_metadata: SearchMetadata,
@@ -27,6 +28,7 @@ pub type ShortIdStr = PrimStr<ShortId>;
2728
feature = "lib-rustc-serialize",
2829
derive(RustcEncodable, RustcDecodable)
2930
)]
31+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
3032
pub struct Status {
3133
pub metadata: Metadata,
3234
pub created_at: String,
@@ -61,6 +63,7 @@ pub struct Status {
6163
feature = "lib-rustc-serialize",
6264
derive(RustcEncodable, RustcDecodable)
6365
)]
66+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
6467
pub struct Metadata {
6568
pub result_type: ResultType,
6669
pub iso_language_code: LanguageCode,
@@ -72,6 +75,7 @@ pub struct Metadata {
7275
feature = "lib-rustc-serialize",
7376
derive(RustcEncodable, RustcDecodable)
7477
)]
78+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
7579
pub struct User {
7680
pub id: ShortId,
7781
pub id_str: ShortIdStr,
@@ -121,6 +125,7 @@ pub struct User {
121125
feature = "lib-rustc-serialize",
122126
derive(RustcEncodable, RustcDecodable)
123127
)]
128+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
124129
pub struct UserEntities {
125130
pub url: Option<UserUrl>,
126131
pub description: UserEntitiesDescription,
@@ -132,6 +137,7 @@ pub struct UserEntities {
132137
feature = "lib-rustc-serialize",
133138
derive(RustcEncodable, RustcDecodable)
134139
)]
140+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
135141
pub struct UserUrl {
136142
pub urls: Vec<Url>,
137143
}
@@ -142,6 +148,7 @@ pub struct UserUrl {
142148
feature = "lib-rustc-serialize",
143149
derive(RustcEncodable, RustcDecodable)
144150
)]
151+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
145152
pub struct Url {
146153
pub url: String,
147154
pub expanded_url: String,
@@ -155,6 +162,7 @@ pub struct Url {
155162
feature = "lib-rustc-serialize",
156163
derive(RustcEncodable, RustcDecodable)
157164
)]
165+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
158166
pub struct UserEntitiesDescription {
159167
pub urls: Vec<Url>,
160168
}
@@ -165,6 +173,7 @@ pub struct UserEntitiesDescription {
165173
feature = "lib-rustc-serialize",
166174
derive(RustcEncodable, RustcDecodable)
167175
)]
176+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
168177
pub struct StatusEntities {
169178
pub hashtags: Vec<Hashtag>,
170179
pub symbols: empty::Array,
@@ -179,6 +188,7 @@ pub struct StatusEntities {
179188
feature = "lib-rustc-serialize",
180189
derive(RustcEncodable, RustcDecodable)
181190
)]
191+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
182192
pub struct Hashtag {
183193
pub text: String,
184194
pub indices: Indices,
@@ -190,6 +200,7 @@ pub struct Hashtag {
190200
feature = "lib-rustc-serialize",
191201
derive(RustcEncodable, RustcDecodable)
192202
)]
203+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
193204
pub struct UserMention {
194205
pub screen_name: String,
195206
pub name: String,
@@ -200,6 +211,7 @@ pub struct UserMention {
200211

201212
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
202213
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
214+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
203215
pub struct Media {
204216
pub id: LongId,
205217
pub id_str: LongIdStr,
@@ -222,6 +234,7 @@ pub struct Media {
222234
feature = "lib-rustc-serialize",
223235
derive(RustcEncodable, RustcDecodable)
224236
)]
237+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
225238
pub struct Sizes {
226239
pub medium: Size,
227240
pub small: Size,
@@ -235,6 +248,7 @@ pub struct Sizes {
235248
feature = "lib-rustc-serialize",
236249
derive(RustcEncodable, RustcDecodable)
237250
)]
251+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
238252
pub struct Size {
239253
pub w: u16,
240254
pub h: u16,
@@ -249,6 +263,7 @@ pub type Indices = (u8, u8);
249263
feature = "lib-rustc-serialize",
250264
derive(RustcEncodable, RustcDecodable)
251265
)]
266+
#[cfg_attr(feature = "lib-simd-json", derive(simd_json_derive::Serialize))]
252267
pub struct SearchMetadata {
253268
pub completed_in: f32,
254269
pub max_id: LongId,

src/empty.rs

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ impl<'de> Deserialize<'de> for Array {
4545
}
4646
}
4747

48+
#[cfg(feature = "lib-simd-json")]
49+
impl simd_json_derive::Serialize for Array {
50+
fn json_write<W>(&self, writer: &mut W) -> std::io::Result<()>
51+
where
52+
W: std::io::Write,
53+
{
54+
writer.write_all(b"[]")
55+
}
56+
}
57+
4858
#[cfg(feature = "lib-rustc-serialize")]
4959
impl Encodable for Array {
5060
fn encode<S>(&self, s: &mut S) -> Result<(), S::Error>

src/enums.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ macro_rules! enum_str {
66
$($variant,)*
77
}
88

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

55+
#[cfg(feature = "lib-simd-json")]
56+
impl ::simd_json_derive::Serialize for $name {
57+
fn json_write<W>(&self, writer: &mut W) -> std::io::Result<()>
58+
where W: std::io::Write
59+
{
60+
self.as_str().json_write(writer)
61+
}
62+
}
63+
5564
#[cfg(feature = "lib-rustc-serialize")]
5665
impl ::rustc_serialize::Encodable for $name {
5766
fn encode<S>(&self, s: &mut S) -> Result<(), S::Error>

src/main.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[global_allocator]
2-
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
2+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
33

44
use json_benchmark::*;
55

@@ -129,6 +129,7 @@ macro_rules! bench_file_simd_json {
129129
path: $path:expr,
130130
structure: $structure:ty,
131131
} => {
132+
132133
let num_trials = num_trials().unwrap_or(256);
133134

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

161162
#[cfg(feature = "stringify-dom")]
162163
{
164+
use simd_json::prelude::*;
163165
let len = contents.len();
164166
let mut data = contents.clone();
165167
let dom = simd_json_parse_dom(&mut data).unwrap();
@@ -190,6 +192,22 @@ macro_rules! bench_file_simd_json {
190192
io::stdout().flush().unwrap();
191193
}
192194

195+
#[cfg(feature = "stringify-struct")]
196+
{
197+
use simd_json_derive::Serialize;
198+
let len = contents.len();
199+
let mut data = contents.clone();
200+
let parsed: $structure = simd_json_parse_struct(&mut data).unwrap();
201+
let dur = timer::bench_with_buf(num_trials, len, |out| {
202+
parsed.json_write(out).unwrap();
203+
});
204+
let mut serialized = Vec::new();
205+
parsed.json_write(&mut serialized).unwrap();
206+
207+
print!("{:6} MB/s", throughput(dur, serialized.len()));
208+
io::stdout().flush().unwrap();
209+
}
210+
193211
println!();
194212
}
195213
}

src/prim_str.rs

+13
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ where
6565
}
6666
}
6767

68+
#[cfg(feature = "lib-simd-json")]
69+
impl<T> simd_json_derive::Serialize for PrimStr<T>
70+
where
71+
T: Copy + Ord + Display + FromStr,
72+
{
73+
fn json_write<W>(&self, writer: &mut W) -> std::io::Result<()>
74+
where
75+
W: std::io::Write,
76+
{
77+
write!(writer, "{}", self.0)
78+
}
79+
}
80+
6881
#[cfg(feature = "lib-rustc-serialize")]
6982
impl<T> Encodable for PrimStr<T>
7083
where

0 commit comments

Comments
 (0)