Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b82617c

Browse files
committedJan 20, 2023
Add cases for simd-json-derive and update to 0.7
Signed-off-by: Heinz N. Gies <[email protected]>
1 parent de23484 commit b82617c

File tree

10 files changed

+470
-64
lines changed

10 files changed

+470
-64
lines changed
 

‎Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ edition = "2021"
66
publish = false
77

88
[dependencies]
9+
910
getopts = "0.2"
1011
jemallocator = "0.5"
1112
rustc-serialize = { version = "0.3", optional = true }
1213
serde = { version = "1.0", features = ["derive"], optional = true }
1314
serde_json = { version = "1.0", optional = true }
14-
simd-json = { version = "0.7", optional = true}
1515
time = "0.3"
16+
simd-json = { version = "0.7", optional = true }
17+
simd-json-derive = { version = "0.7", optional = true }
1618

1719
[features]
1820
default = ["performance", "all-libs", "all-files"]
1921
all-libs = ["lib-serde", "lib-rustc-serialize", "lib-simd-json"]
2022
all-files = ["file-canada", "file-citm-catalog", "file-twitter"]
2123
performance = ["parse-dom", "stringify-dom", "parse-struct", "stringify-struct"]
2224
lib-serde = ["serde", "serde_json"]
23-
lib-simd-json = ["serde", "simd-json"]
25+
lib-simd-json = ["serde", "simd-json", "simd-json-derive"]
2426
lib-rustc-serialize = ["rustc-serialize"]
2527
file-canada = []
2628
file-citm-catalog = []

‎README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ are:
55

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

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

1515
#### `$ cargo run --release`
1616

‎src/canada.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,56 @@ use std::collections::BTreeMap as Map;
66
pub type Canada = FeatureCollection;
77

88
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
9+
#[cfg_attr(
10+
any(feature = "serde", feature = "lib-simd-json"),
11+
serde(deny_unknown_fields)
12+
)]
13+
#[cfg_attr(
14+
feature = "lib-simd-json",
15+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
16+
)]
1017
pub struct FeatureCollection {
11-
#[cfg_attr(feature = "serde", serde(rename = "type"))]
18+
#[cfg_attr(
19+
any(feature = "serde", feature = "lib-simd-json"),
20+
serde(rename = "type")
21+
)]
1222
pub obj_type: ObjType,
1323
pub features: Vec<Feature>,
1424
}
1525

1626
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
27+
#[cfg_attr(
28+
any(feature = "serde", feature = "lib-simd-json"),
29+
serde(deny_unknown_fields)
30+
)]
31+
#[cfg_attr(
32+
feature = "lib-simd-json",
33+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
34+
)]
1835
pub struct Feature {
19-
#[cfg_attr(feature = "serde", serde(rename = "type"))]
36+
#[cfg_attr(
37+
any(feature = "serde", feature = "lib-simd-json"),
38+
serde(rename = "type")
39+
)]
2040
pub obj_type: ObjType,
2141
pub properties: Map<String, String>,
2242
pub geometry: Geometry,
2343
}
2444

2545
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
26-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
46+
#[cfg_attr(
47+
any(feature = "serde", feature = "lib-simd-json"),
48+
serde(deny_unknown_fields)
49+
)]
50+
#[cfg_attr(
51+
feature = "lib-simd-json",
52+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
53+
)]
2754
pub struct Geometry {
28-
#[cfg_attr(feature = "serde", serde(rename = "type"))]
55+
#[cfg_attr(
56+
any(feature = "serde", feature = "lib-simd-json"),
57+
serde(rename = "type")
58+
)]
2959
pub obj_type: ObjType,
3060
pub coordinates: Vec<Vec<(Latitude, Longitude)>>,
3161
}

‎src/color.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use serde::de::{self, Deserialize, Deserializer, Unexpected};
1212
#[cfg(feature = "serde")]
1313
use serde::ser::{Serialize, Serializer};
1414

15-
#[derive(Clone, Copy)]
15+
#[derive(Clone, Copy, Debug)]
1616
pub struct Color(u32);
1717

1818
#[cfg(any(feature = "serde", feature = "lib-rustc-serialize"))]
@@ -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>
@@ -88,6 +99,29 @@ impl<'de> Deserialize<'de> for Color {
8899
}
89100
}
90101

102+
#[cfg(feature = "lib-simd-json")]
103+
impl<'input> ::simd_json_derive::Deserialize<'input> for Color {
104+
#[inline]
105+
fn from_tape(tape: &mut ::simd_json_derive::Tape<'input>) -> simd_json::Result<Self>
106+
where
107+
Self: std::marker::Sized + 'input,
108+
{
109+
if let Some(::simd_json::Node::String(s)) = tape.next() {
110+
if let Ok(hex) = u32::from_str_radix(s, 16) {
111+
Ok(Color(hex))
112+
} else {
113+
dbg!(Err(::simd_json::Error::generic(
114+
simd_json::ErrorType::ExpectedString,
115+
)))
116+
}
117+
} else {
118+
dbg!(Err(::simd_json::Error::generic(
119+
simd_json::ErrorType::ExpectedString,
120+
)))
121+
}
122+
}
123+
}
124+
91125
#[cfg(feature = "lib-rustc-serialize")]
92126
impl Encodable for Color {
93127
fn encode<S>(&self, s: &mut S) -> Result<(), S::Error>

‎src/copy/citm_catalog.rs

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ use std::collections::BTreeMap as Map;
66
use crate::empty;
77
use crate::prim_str::PrimStr;
88

9-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
109
#[cfg_attr(
11-
feature = "serde",
10+
any(feature = "serde", feature = "lib-simd-json"),
11+
derive(Serialize, Deserialize)
12+
)]
13+
#[cfg_attr(
14+
any(feature = "serde", feature = "lib-simd-json"),
1215
serde(deny_unknown_fields, rename_all = "camelCase")
1316
)]
17+
#[cfg_attr(
18+
feature = "lib-simd-json",
19+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
20+
)]
1421
pub struct CitmCatalog {
1522
pub area_names: Map<IdStr, String>,
1623
pub audience_sub_category_names: Map<IdStr, String>,
@@ -28,11 +35,18 @@ pub struct CitmCatalog {
2835
pub type Id = u32;
2936
pub type IdStr = PrimStr<u32>;
3037

31-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3238
#[cfg_attr(
33-
feature = "serde",
39+
any(feature = "serde", feature = "lib-simd-json"),
40+
derive(Serialize, Deserialize)
41+
)]
42+
#[cfg_attr(
43+
any(feature = "serde", feature = "lib-simd-json"),
3444
serde(deny_unknown_fields, rename_all = "camelCase")
3545
)]
46+
#[cfg_attr(
47+
feature = "lib-simd-json",
48+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
49+
)]
3650
pub struct Event {
3751
pub description: (),
3852
pub id: Id,
@@ -44,11 +58,18 @@ pub struct Event {
4458
pub topic_ids: Vec<Id>,
4559
}
4660

47-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4861
#[cfg_attr(
49-
feature = "serde",
62+
any(feature = "serde", feature = "lib-simd-json"),
63+
derive(Serialize, Deserialize)
64+
)]
65+
#[cfg_attr(
66+
any(feature = "serde", feature = "lib-simd-json"),
5067
serde(deny_unknown_fields, rename_all = "camelCase")
5168
)]
69+
#[cfg_attr(
70+
feature = "lib-simd-json",
71+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
72+
)]
5273
pub struct Performance {
5374
pub event_id: Id,
5475
pub id: Id,
@@ -61,32 +82,53 @@ pub struct Performance {
6182
pub venue_code: String,
6283
}
6384

64-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6585
#[cfg_attr(
66-
feature = "serde",
86+
any(feature = "serde", feature = "lib-simd-json"),
87+
derive(Serialize, Deserialize)
88+
)]
89+
#[cfg_attr(
90+
any(feature = "serde", feature = "lib-simd-json"),
6791
serde(deny_unknown_fields, rename_all = "camelCase")
6892
)]
93+
#[cfg_attr(
94+
feature = "lib-simd-json",
95+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
96+
)]
6997
pub struct Price {
7098
pub amount: u32,
7199
pub audience_sub_category_id: Id,
72100
pub seat_category_id: Id,
73101
}
74102

75-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
76103
#[cfg_attr(
77-
feature = "serde",
104+
any(feature = "serde", feature = "lib-simd-json"),
105+
derive(Serialize, Deserialize)
106+
)]
107+
#[cfg_attr(
108+
any(feature = "serde", feature = "lib-simd-json"),
78109
serde(deny_unknown_fields, rename_all = "camelCase")
79110
)]
111+
#[cfg_attr(
112+
feature = "lib-simd-json",
113+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
114+
)]
80115
pub struct SeatCategory {
81116
pub areas: Vec<Area>,
82117
pub seat_category_id: Id,
83118
}
84119

85-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
86120
#[cfg_attr(
87-
feature = "serde",
121+
any(feature = "serde", feature = "lib-simd-json"),
122+
derive(Serialize, Deserialize)
123+
)]
124+
#[cfg_attr(
125+
any(feature = "serde", feature = "lib-simd-json"),
88126
serde(deny_unknown_fields, rename_all = "camelCase")
89127
)]
128+
#[cfg_attr(
129+
feature = "lib-simd-json",
130+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
131+
)]
90132
pub struct Area {
91133
pub area_id: Id,
92134
pub block_ids: empty::Array,

‎src/copy/twitter.rs

Lines changed: 184 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@ use crate::color::Color;
77
use crate::empty;
88
use crate::prim_str::PrimStr;
99

10-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
10+
#[cfg_attr(
11+
any(feature = "serde", feature = "lib-simd-json"),
12+
derive(Serialize, Deserialize)
13+
)]
14+
#[cfg_attr(
15+
any(feature = "serde", feature = "lib-simd-json"),
16+
serde(deny_unknown_fields)
17+
)]
1218
#[cfg_attr(
1319
feature = "lib-rustc-serialize",
1420
derive(RustcEncodable, RustcDecodable)
1521
)]
22+
#[cfg_attr(
23+
feature = "lib-simd-json",
24+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
25+
)]
1626
pub struct Twitter {
1727
pub statuses: Vec<Status>,
1828
pub search_metadata: SearchMetadata,
@@ -23,12 +33,22 @@ pub type ShortId = u32;
2333
pub type LongIdStr = PrimStr<LongId>;
2434
pub type ShortIdStr = PrimStr<ShortId>;
2535

26-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
27-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
36+
#[cfg_attr(
37+
any(feature = "serde", feature = "lib-simd-json"),
38+
derive(Serialize, Deserialize)
39+
)]
40+
#[cfg_attr(
41+
any(feature = "serde", feature = "lib-simd-json"),
42+
serde(deny_unknown_fields)
43+
)]
2844
#[cfg_attr(
2945
feature = "lib-rustc-serialize",
3046
derive(RustcEncodable, RustcDecodable)
3147
)]
48+
#[cfg_attr(
49+
feature = "lib-simd-json",
50+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
51+
)]
3252
pub struct Status {
3353
pub metadata: Metadata,
3454
pub created_at: String,
@@ -57,23 +77,43 @@ pub struct Status {
5777
pub lang: LanguageCode,
5878
}
5979

60-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
61-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
80+
#[cfg_attr(
81+
any(feature = "serde", feature = "lib-simd-json"),
82+
derive(Serialize, Deserialize)
83+
)]
84+
#[cfg_attr(
85+
any(feature = "serde", feature = "lib-simd-json"),
86+
serde(deny_unknown_fields)
87+
)]
6288
#[cfg_attr(
6389
feature = "lib-rustc-serialize",
6490
derive(RustcEncodable, RustcDecodable)
6591
)]
92+
#[cfg_attr(
93+
feature = "lib-simd-json",
94+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
95+
)]
6696
pub struct Metadata {
6797
pub result_type: ResultType,
6898
pub iso_language_code: LanguageCode,
6999
}
70100

71-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
72-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
101+
#[cfg_attr(
102+
any(feature = "serde", feature = "lib-simd-json"),
103+
derive(Serialize, Deserialize)
104+
)]
105+
#[cfg_attr(
106+
any(feature = "serde", feature = "lib-simd-json"),
107+
serde(deny_unknown_fields)
108+
)]
73109
#[cfg_attr(
74110
feature = "lib-rustc-serialize",
75111
derive(RustcEncodable, RustcDecodable)
76112
)]
113+
#[cfg_attr(
114+
feature = "lib-simd-json",
115+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
116+
)]
77117
pub struct User {
78118
pub id: ShortId,
79119
pub id_str: ShortIdStr,
@@ -117,56 +157,106 @@ pub struct User {
117157
pub notifications: bool,
118158
}
119159

120-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
121-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
160+
#[cfg_attr(
161+
any(feature = "serde", feature = "lib-simd-json"),
162+
derive(Serialize, Deserialize)
163+
)]
164+
#[cfg_attr(
165+
any(feature = "serde", feature = "lib-simd-json"),
166+
serde(deny_unknown_fields)
167+
)]
122168
#[cfg_attr(
123169
feature = "lib-rustc-serialize",
124170
derive(RustcEncodable, RustcDecodable)
125171
)]
172+
#[cfg_attr(
173+
feature = "lib-simd-json",
174+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
175+
)]
126176
pub struct UserEntities {
127177
pub url: Option<UserUrl>,
128178
pub description: UserEntitiesDescription,
129179
}
130180

131-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
132-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
181+
#[cfg_attr(
182+
any(feature = "serde", feature = "lib-simd-json"),
183+
derive(Serialize, Deserialize)
184+
)]
185+
#[cfg_attr(
186+
any(feature = "serde", feature = "lib-simd-json"),
187+
serde(deny_unknown_fields)
188+
)]
133189
#[cfg_attr(
134190
feature = "lib-rustc-serialize",
135191
derive(RustcEncodable, RustcDecodable)
136192
)]
193+
#[cfg_attr(
194+
feature = "lib-simd-json",
195+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
196+
)]
137197
pub struct UserUrl {
138198
pub urls: Vec<Url>,
139199
}
140200

141-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
142-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
201+
#[cfg_attr(
202+
any(feature = "serde", feature = "lib-simd-json"),
203+
derive(Serialize, Deserialize)
204+
)]
205+
#[cfg_attr(
206+
any(feature = "serde", feature = "lib-simd-json"),
207+
serde(deny_unknown_fields)
208+
)]
143209
#[cfg_attr(
144210
feature = "lib-rustc-serialize",
145211
derive(RustcEncodable, RustcDecodable)
146212
)]
213+
#[cfg_attr(
214+
feature = "lib-simd-json",
215+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
216+
)]
147217
pub struct Url {
148218
pub url: String,
149219
pub expanded_url: String,
150220
pub display_url: String,
151221
pub indices: Indices,
152222
}
153223

154-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
155-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
224+
#[cfg_attr(
225+
any(feature = "serde", feature = "lib-simd-json"),
226+
derive(Serialize, Deserialize)
227+
)]
228+
#[cfg_attr(
229+
any(feature = "serde", feature = "lib-simd-json"),
230+
serde(deny_unknown_fields)
231+
)]
156232
#[cfg_attr(
157233
feature = "lib-rustc-serialize",
158234
derive(RustcEncodable, RustcDecodable)
159235
)]
236+
#[cfg_attr(
237+
feature = "lib-simd-json",
238+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
239+
)]
160240
pub struct UserEntitiesDescription {
161241
pub urls: Vec<Url>,
162242
}
163243

164-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
165-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
244+
#[cfg_attr(
245+
any(feature = "serde", feature = "lib-simd-json"),
246+
derive(Serialize, Deserialize)
247+
)]
248+
#[cfg_attr(
249+
any(feature = "serde", feature = "lib-simd-json"),
250+
serde(deny_unknown_fields)
251+
)]
166252
#[cfg_attr(
167253
feature = "lib-rustc-serialize",
168254
derive(RustcEncodable, RustcDecodable)
169255
)]
256+
#[cfg_attr(
257+
feature = "lib-simd-json",
258+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
259+
)]
170260
pub struct StatusEntities {
171261
pub hashtags: Vec<Hashtag>,
172262
pub symbols: empty::Array,
@@ -175,23 +265,43 @@ pub struct StatusEntities {
175265
pub media: Option<Vec<Media>>,
176266
}
177267

178-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
179-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
268+
#[cfg_attr(
269+
any(feature = "serde", feature = "lib-simd-json"),
270+
derive(Serialize, Deserialize)
271+
)]
272+
#[cfg_attr(
273+
any(feature = "serde", feature = "lib-simd-json"),
274+
serde(deny_unknown_fields)
275+
)]
180276
#[cfg_attr(
181277
feature = "lib-rustc-serialize",
182278
derive(RustcEncodable, RustcDecodable)
183279
)]
280+
#[cfg_attr(
281+
feature = "lib-simd-json",
282+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
283+
)]
184284
pub struct Hashtag {
185285
pub text: String,
186286
pub indices: Indices,
187287
}
188288

189-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
190-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
289+
#[cfg_attr(
290+
any(feature = "serde", feature = "lib-simd-json"),
291+
derive(Serialize, Deserialize)
292+
)]
293+
#[cfg_attr(
294+
any(feature = "serde", feature = "lib-simd-json"),
295+
serde(deny_unknown_fields)
296+
)]
191297
#[cfg_attr(
192298
feature = "lib-rustc-serialize",
193299
derive(RustcEncodable, RustcDecodable)
194300
)]
301+
#[cfg_attr(
302+
feature = "lib-simd-json",
303+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
304+
)]
195305
pub struct UserMention {
196306
pub screen_name: String,
197307
pub name: String,
@@ -200,8 +310,18 @@ pub struct UserMention {
200310
pub indices: Indices,
201311
}
202312

203-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
204-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
313+
#[cfg_attr(
314+
any(feature = "serde", feature = "lib-simd-json"),
315+
derive(Serialize, Deserialize)
316+
)]
317+
#[cfg_attr(
318+
any(feature = "serde", feature = "lib-simd-json"),
319+
serde(deny_unknown_fields)
320+
)]
321+
#[cfg_attr(
322+
feature = "lib-simd-json",
323+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
324+
)]
205325
pub struct Media {
206326
pub id: LongId,
207327
pub id_str: LongIdStr,
@@ -211,32 +331,55 @@ pub struct Media {
211331
pub url: String,
212332
pub display_url: String,
213333
pub expanded_url: String,
214-
#[cfg_attr(feature = "serde", serde(rename = "type"))]
334+
#[cfg_attr(
335+
any(feature = "serde", feature = "lib-simd-json"),
336+
serde(rename = "type")
337+
)]
215338
pub media_type: String,
216339
pub sizes: Sizes,
217340
pub source_status_id: Option<LongId>,
218341
pub source_status_id_str: Option<LongIdStr>,
219342
}
220343

221-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
222-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
344+
#[cfg_attr(
345+
any(feature = "serde", feature = "lib-simd-json"),
346+
derive(Serialize, Deserialize)
347+
)]
348+
#[cfg_attr(
349+
any(feature = "serde", feature = "lib-simd-json"),
350+
serde(deny_unknown_fields)
351+
)]
223352
#[cfg_attr(
224353
feature = "lib-rustc-serialize",
225354
derive(RustcEncodable, RustcDecodable)
226355
)]
356+
#[cfg_attr(
357+
feature = "lib-simd-json",
358+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
359+
)]
227360
pub struct Sizes {
228361
pub medium: Size,
229362
pub small: Size,
230363
pub thumb: Size,
231364
pub large: Size,
232365
}
233366

234-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
235-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
367+
#[cfg_attr(
368+
any(feature = "serde", feature = "lib-simd-json"),
369+
derive(Serialize, Deserialize)
370+
)]
371+
#[cfg_attr(
372+
any(feature = "serde", feature = "lib-simd-json"),
373+
serde(deny_unknown_fields)
374+
)]
236375
#[cfg_attr(
237376
feature = "lib-rustc-serialize",
238377
derive(RustcEncodable, RustcDecodable)
239378
)]
379+
#[cfg_attr(
380+
feature = "lib-simd-json",
381+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
382+
)]
240383
pub struct Size {
241384
pub w: u16,
242385
pub h: u16,
@@ -245,12 +388,22 @@ pub struct Size {
245388

246389
pub type Indices = (u8, u8);
247390

248-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
249-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
391+
#[cfg_attr(
392+
any(feature = "serde", feature = "lib-simd-json"),
393+
derive(Serialize, Deserialize)
394+
)]
395+
#[cfg_attr(
396+
any(feature = "serde", feature = "lib-simd-json"),
397+
serde(deny_unknown_fields)
398+
)]
250399
#[cfg_attr(
251400
feature = "lib-rustc-serialize",
252401
derive(RustcEncodable, RustcDecodable)
253402
)]
403+
#[cfg_attr(
404+
feature = "lib-simd-json",
405+
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
406+
)]
254407
pub struct SearchMetadata {
255408
pub completed_in: f32,
256409
pub max_id: LongId,

‎src/empty.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,32 @@ 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+
58+
impl<'input> simd_json_derive::Deserialize<'input> for Array {
59+
#[inline]
60+
fn from_tape(tape: &mut simd_json_derive::Tape<'input>) -> simd_json::Result<Self>
61+
where
62+
Self: std::marker::Sized + 'input,
63+
{
64+
if let Some(simd_json::Node::Array(0, _)) = tape.next() {
65+
Ok(Self)
66+
} else {
67+
Err(simd_json::Error::generic(
68+
simd_json::ErrorType::ExpectedArray,
69+
))
70+
}
71+
}
72+
}
73+
4874
#[cfg(feature = "lib-rustc-serialize")]
4975
impl Encodable for Array {
5076
fn encode<S>(&self, s: &mut S) -> Result<(), S::Error>

‎src/enums.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#[macro_export]
22
macro_rules! enum_str {
33
($name:ident { $($variant:ident($str:expr), )* }) => {
4-
#[derive(Clone, Copy)]
4+
#[derive(Clone, Copy, Debug)]
55
pub enum $name {
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,40 @@ macro_rules! enum_str {
5252
}
5353
}
5454

55+
#[cfg(feature = "lib-simd-json")]
56+
impl ::simd_json_derive::Serialize for $name {
57+
#[inline]
58+
fn json_write<W>(&self, writer: &mut W) -> std::io::Result<()>
59+
where W: std::io::Write
60+
{
61+
self.as_str().json_write(writer)
62+
}
63+
}
64+
65+
#[cfg(feature = "lib-simd-json")]
66+
impl<'input> ::simd_json_derive::Deserialize<'input> for $name {
67+
#[inline]
68+
fn from_tape(tape: &mut ::simd_json_derive::Tape<'input>) -> simd_json::Result<Self>
69+
where
70+
Self: std::marker::Sized + 'input,
71+
{
72+
if let Some(::simd_json::Node::String(s)) = tape.next() {
73+
match s {
74+
$( $str => Ok($name::$variant), )*
75+
_ => Err(::simd_json::Error::generic(
76+
simd_json::ErrorType::ExpectedString,
77+
)),
78+
}
79+
80+
} else {
81+
Err(::simd_json::Error::generic(
82+
simd_json::ErrorType::ExpectedString,
83+
))
84+
}
85+
}
86+
}
87+
88+
5589
#[cfg(feature = "lib-rustc-serialize")]
5690
impl ::rustc_serialize::Encodable for $name {
5791
fn encode<S>(&self, s: &mut S) -> Result<(), S::Error>

‎src/main.rs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ macro_rules! bench_file_simd_json {
131131
path: $path:expr,
132132
structure: $structure:ty,
133133
} => {
134+
134135
let num_trials = num_trials().unwrap_or(256);
136+
let mut string_buffer = Vec::with_capacity(4096);
137+
let mut input_buffer = simd_json::AlignedBuf::with_capacity(4096);
135138

136139
print!("{:22}", $path);
137140
io::stdout().flush().unwrap();
@@ -150,7 +153,7 @@ macro_rules! bench_file_simd_json {
150153
for _ in 0..num_trials {
151154
data.as_mut_slice().clone_from_slice(contents.as_slice());
152155
let mut timer = benchmark.start();
153-
let _parsed = simd_json_parse_dom(&mut data).unwrap();
156+
let _parsed = simd_json_parse_dom(&mut data, &mut input_buffer, &mut string_buffer).unwrap();
154157
timer.stop();
155158
}
156159
let dur = benchmark.min_elapsed();
@@ -164,7 +167,7 @@ macro_rules! bench_file_simd_json {
164167
{
165168
let len = contents.len();
166169
let mut data = contents.clone();
167-
let dom = simd_json_parse_dom(&mut data).unwrap();
170+
let dom = simd_json_parse_dom(&mut data, &mut input_buffer, &mut string_buffer).unwrap();
168171
let dur = timer::bench_with_buf(num_trials, len, |out| {
169172
simd_json::Writable::write(&dom, out).unwrap()
170173
});
@@ -184,14 +187,30 @@ macro_rules! bench_file_simd_json {
184187
for _ in 0..num_trials {
185188
data.as_mut_slice().clone_from_slice(contents.as_slice());
186189
let mut timer = benchmark.start();
187-
let _parsed: $structure = simd_json_parse_struct(&mut data).unwrap();
190+
let _parsed: $structure = simd_json_parse_struct(&mut data, &mut input_buffer, &mut string_buffer).unwrap();
188191
timer.stop();
189192
}
190193
let dur = benchmark.min_elapsed();
191194
print!("{:6} MB/s", throughput(dur, contents.len()));
192195
io::stdout().flush().unwrap();
193196
}
194197

198+
#[cfg(feature = "stringify-struct")]
199+
{
200+
use simd_json_derive::Serialize;
201+
let len = contents.len();
202+
let mut data = contents.clone();
203+
let parsed: $structure = simd_json_parse_struct(&mut data, &mut input_buffer, &mut string_buffer).unwrap();
204+
let dur = timer::bench_with_buf(num_trials, len, |out| {
205+
parsed.json_write(out).unwrap();
206+
});
207+
let mut serialized = Vec::new();
208+
parsed.json_write(&mut serialized).unwrap();
209+
210+
print!("{:6} MB/s", throughput(dur, serialized.len()));
211+
io::stdout().flush().unwrap();
212+
}
213+
195214
println!();
196215
}
197216
}
@@ -292,17 +311,36 @@ where
292311
feature = "lib-simd-json",
293312
any(feature = "parse-dom", feature = "stringify-dom")
294313
))]
295-
fn simd_json_parse_dom(bytes: &mut [u8]) -> simd_json::Result<simd_json::BorrowedValue> {
296-
simd_json::to_borrowed_value(bytes)
314+
fn simd_json_parse_dom<'input>(
315+
bytes: &'input mut [u8],
316+
input_buffer: &mut simd_json::AlignedBuf,
317+
string_buffer: &mut [u8],
318+
) -> simd_json::Result<simd_json::BorrowedValue<'input>> {
319+
simd_json::to_borrowed_value_with_buffers(bytes, input_buffer, string_buffer)
297320
}
298321

322+
// #[cfg(all(
323+
// feature = "lib-simd-json",
324+
// any(feature = "parse-struct", feature = "stringify-struct")
325+
// ))]
326+
// fn simd_json_parse_struct<'de, T>(bytes: &'de mut [u8]) -> simd_json::Result<T>
327+
// where
328+
// T: serde::Deserialize<'de>,
329+
// {
330+
// simd_json::serde::from_slice(bytes)
331+
// }
332+
299333
#[cfg(all(
300334
feature = "lib-simd-json",
301335
any(feature = "parse-struct", feature = "stringify-struct")
302336
))]
303-
fn simd_json_parse_struct<'de, T>(bytes: &'de mut [u8]) -> simd_json::Result<T>
337+
fn simd_json_parse_struct<'de, T>(
338+
bytes: &'de mut [u8],
339+
input_buffer: &mut simd_json::AlignedBuf,
340+
string_buffer: &mut [u8],
341+
) -> simd_json::Result<T>
304342
where
305-
T: serde::Deserialize<'de>,
343+
T: simd_json_derive::Deserialize<'de> + 'de,
306344
{
307-
simd_json::serde::from_slice(bytes)
345+
T::from_slice_with_buffers(bytes, input_buffer, string_buffer)
308346
}

‎src/prim_str.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,53 @@ 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, r#""{}""#, self.0)
78+
}
79+
}
80+
81+
#[cfg(feature = "lib-simd-json")]
82+
impl<T> simd_json_derive::SerializeAsKey for PrimStr<T>
83+
where
84+
T: Copy + Ord + Display + FromStr,
85+
{
86+
fn json_write<W>(&self, writer: &mut W) -> std::io::Result<()>
87+
where
88+
W: std::io::Write,
89+
{
90+
write!(writer, r#""{}""#, self.0)
91+
}
92+
}
93+
94+
impl<'input, T> simd_json_derive::Deserialize<'input> for PrimStr<T>
95+
where
96+
T: Copy + Ord + Display + FromStr,
97+
{
98+
#[inline]
99+
fn from_tape(tape: &mut simd_json_derive::Tape<'input>) -> simd_json::Result<Self>
100+
where
101+
Self: std::marker::Sized + 'input,
102+
{
103+
if let Some(simd_json::Node::String(s)) = tape.next() {
104+
Ok(PrimStr(FromStr::from_str(s).map_err(|_e| {
105+
simd_json::Error::generic(simd_json::ErrorType::Serde("not a number".into()))
106+
})?))
107+
} else {
108+
Err(simd_json::Error::generic(
109+
simd_json::ErrorType::ExpectedNull,
110+
))
111+
}
112+
}
113+
}
114+
68115
#[cfg(feature = "lib-rustc-serialize")]
69116
impl<T> Encodable for PrimStr<T>
70117
where

0 commit comments

Comments
 (0)
This repository has been archived.