simd_json instead of serde_json
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2023-08-23 11:34:33 +03:00
parent 760f6d9415
commit e1ef9cc3c3
9 changed files with 165 additions and 31 deletions

View File

@@ -44,10 +44,9 @@ convert_async = [
[dependencies]
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
bincode = "^2.0.0-rc.2"
zip = { version = "0.6", optional = true }
async_zip = { version = "0.0.15" , features = [
async_zip = { version = "0.0.15", features = [
"zstd",
"tokio",
"tokio-fs",
@@ -70,6 +69,7 @@ async-stream = { version = "0.3", optional = true }
zstd = { version = "^0.12", default-features = false, optional = true }
memmap = { version = "0.7.0", optional = true }
pin-project = { version = "1.1.3", optional = true }
simd-json = "0.10.6"
[dev-dependencies]
insta = { version = "1.31.0", features = ["yaml"] }

View File

@@ -142,7 +142,7 @@ pub mod convert {
impl<T> QuestionsConverter for T
where
T: Iterator<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>,
T: Iterator<Item = (String, Result<SourceQuestionsBatch, simd_json::Error>)>,
{
fn convert<'a>(&'a mut self) -> Box<dyn Iterator<Item = Question> + 'a> {
let iter = self
@@ -169,7 +169,7 @@ pub mod convert {
fn test_convert() {
let mut source = iter::once((
String::from("test.json"),
Ok::<SourceQuestionsBatch, serde_json::Error>(sample_batch()),
Ok::<SourceQuestionsBatch, simd_json::Error>(sample_batch()),
));
let converted: Vec<_> = source.convert().collect();
assert_yaml_snapshot!(converted, @r#"
@@ -207,7 +207,7 @@ pub mod convert_async {
pub struct QuestionsConverterAsync<T>
where
T: Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>
T: Stream<Item = (String, Result<SourceQuestionsBatch, simd_json::Error>)>
+ std::marker::Unpin,
{
inner: T,
@@ -215,7 +215,7 @@ pub mod convert_async {
impl<T> From<T> for QuestionsConverterAsync<T>
where
T: Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>
T: Stream<Item = (String, Result<SourceQuestionsBatch, simd_json::Error>)>
+ std::marker::Unpin,
{
fn from(inner: T) -> Self {
@@ -225,7 +225,7 @@ pub mod convert_async {
pub trait QuestionsConverterAsyncForStream<T>
where
T: Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>
T: Stream<Item = (String, Result<SourceQuestionsBatch, simd_json::Error>)>
+ std::marker::Unpin,
{
fn converter(&mut self) -> QuestionsConverterAsync<&mut T>;
@@ -233,7 +233,7 @@ pub mod convert_async {
impl<T> QuestionsConverterAsyncForStream<T> for T
where
T: Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>
T: Stream<Item = (String, Result<SourceQuestionsBatch, simd_json::Error>)>
+ std::marker::Unpin,
{
fn converter(&mut self) -> QuestionsConverterAsync<&mut T> {
@@ -243,7 +243,7 @@ pub mod convert_async {
impl<T> QuestionsConverterAsync<T>
where
T: Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>
T: Stream<Item = (String, Result<SourceQuestionsBatch, simd_json::Error>)>
+ std::marker::Unpin,
{
pub fn convert(self) -> impl Stream<Item = Question> {
@@ -279,7 +279,7 @@ pub mod convert_async {
let source = futures::stream::once(async {
(
String::from("test.json"),
Ok::<SourceQuestionsBatch, serde_json::Error>(sample_batch()),
Ok::<SourceQuestionsBatch, simd_json::Error>(sample_batch()),
)
});
@@ -315,7 +315,7 @@ pub use convert_async::{QuestionsConverterAsync, QuestionsConverterAsyncForStrea
mod test {
use super::*;
use insta::assert_yaml_snapshot;
use serde_json::json;
use simd_json::json;
#[cfg(any(feature = "convert", feature = "convert_async"))]
pub mod convert_common {
@@ -373,7 +373,7 @@ mod test {
}
#[test]
fn test_question_de() {
let question_from_json: Result<Question, _> = serde_json::from_value(json!({
let question_from_json: Result<Question, _> = simd_json::serde::from_owned_value(json!({
"id": "Вопрос 1",
"description": "Сколько будет (2 * 2 * 2 + 2) * 2 * 2 + 2",
"answer": "42",

View File

@@ -142,7 +142,7 @@ pub mod reader_sync {
where
R: Read + Seek,
{
type Item = (String, Result<SourceQuestionsBatch, serde_json::Error>);
type Item = (String, Result<SourceQuestionsBatch, simd_json::Error>);
fn next(&mut self) -> Option<Self::Item> {
if self.index.is_none() && !self.zipfile.is_empty() {
@@ -169,7 +169,7 @@ pub mod reader_sync {
let name = file.mangled_name();
let name_str = name.to_str().unwrap();
let data: Result<SourceQuestionsBatch, _> = serde_json::from_reader(file);
let data: Result<SourceQuestionsBatch, _> = simd_json::from_reader(file);
Some((String::from(name_str), data))
}
@@ -237,7 +237,7 @@ pub mod reader_sync {
zip_file
.start_file("test.json", options)
.expect("zip start file");
let data = serde_json::to_vec(&batch).unwrap();
let data = simd_json::to_vec(&batch).unwrap();
let amount = zip_file.write(data.as_slice()).expect("write entry");
assert_eq!(amount, data.len());
zip_file.finish().expect("finish zip file");
@@ -331,7 +331,7 @@ pub mod reader_async {
pub async fn get(
&mut self,
index: usize,
) -> Result<(String, Result<SourceQuestionsBatch, serde_json::Error>), String>
) -> Result<(String, Result<SourceQuestionsBatch, simd_json::Error>), String>
where
R: AsyncRead + AsyncSeek + Unpin,
{
@@ -352,12 +352,12 @@ pub mod reader_async {
if let Err(error) = readed {
return Err(format!("read_to_end: {error:?}"));
}
let parsed: Result<SourceQuestionsBatch, _> = serde_json::from_slice(&data);
let parsed: Result<SourceQuestionsBatch, _> = simd_json::from_slice(data.as_mut_slice());
Ok((filename, parsed))
}
pub async fn get_next(
&mut self,
) -> Option<Result<(String, Result<SourceQuestionsBatch, serde_json::Error>), String>>
) -> Option<Result<(String, Result<SourceQuestionsBatch, simd_json::Error>), String>>
where
R: AsyncRead + AsyncSeek + Unpin,
{
@@ -376,7 +376,7 @@ pub mod reader_async {
}
pub fn stream(
&mut self,
) -> impl Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)> + '_
) -> impl Stream<Item = (String, Result<SourceQuestionsBatch, simd_json::Error>)> + '_
{
stream! {
while let Some(Ok(item)) = self.get_next().await {
@@ -425,7 +425,7 @@ pub mod reader_async {
let entry =
ZipEntryBuilder::new("test.json".into(), async_zip::Compression::Zstd).build();
zip_file
.write_entry_whole(entry, serde_json::to_vec(&batch).unwrap().as_slice())
.write_entry_whole(entry, simd_json::to_vec(&batch).unwrap().as_slice())
.await
.expect("write entry");
zip_file.close().await.expect("close zip");
@@ -500,7 +500,7 @@ pub use reader_async::{ReadSourceQuestionsBatchesAsync, SourceQuestionsZipReader
mod test {
use super::*;
use insta::assert_yaml_snapshot;
use serde_json::json;
use simd_json::json;
pub fn sample_batch() -> SourceQuestionsBatch {
SourceQuestionsBatch {
@@ -544,7 +544,7 @@ mod test {
}
#[test]
fn test_batch_de() {
let batch_from_json: Result<SourceQuestionsBatch, _> = serde_json::from_value(json!({
let batch_from_json: Result<SourceQuestionsBatch, _> = simd_json::serde::from_owned_value(json!({
"Чемпионат": "Тестовый",
"Дата": "00-000-2000",
"Вопросы": [