Skip to content

Commit

Permalink
Small JSON performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
abdolence committed Aug 7, 2022
1 parent 95bebd8 commit 1695985
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
profile: minimal
toolchain: stable
components: rustfmt, clippy
- run: cargo fmt -- --check && cargo clippy -- -Dwarnings && cargo test --all-features
- run: cargo fmt -- --check && cargo clippy --all-features -- -Dwarnings && cargo test --all-features
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license = "Apache-2.0"
name = "axum-streams"
readme = "README.md"
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]
version = "0.5.0"
version = "0.5.1"

[badges]
maintenance = { status = "actively-developed" }
Expand Down
38 changes: 18 additions & 20 deletions src/json_formats.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::stream_format::StreamingFormat;
use bytes::{BufMut, BytesMut};
use futures_util::stream::BoxStream;
use futures_util::StreamExt;
use http::HeaderMap;
use serde::Serialize;
use std::io::Write;

pub struct JsonArrayStreamFormat;

Expand All @@ -22,16 +24,14 @@ where
) -> BoxStream<'b, Result<axum::body::Bytes, axum::Error>> {
let stream_bytes: BoxStream<Result<axum::body::Bytes, axum::Error>> = Box::pin({
stream.enumerate().map(|(index, obj)| {
let mut output = vec![];
serde_json::to_vec::<T>(&obj)
.map(|obj_vec| {
if index != 0 {
output.extend(JSON_ARRAY_SEP_BYTES)
}
output.extend(obj_vec);
axum::body::Bytes::from(output)
})
.map_err(axum::Error::new)
let mut buf = BytesMut::new().writer();
if index != 0 {
buf.write_all(JSON_ARRAY_SEP_BYTES).unwrap();
}
match serde_json::to_writer(&mut buf, &obj) {
Ok(_) => Ok(buf.into_inner().freeze()),
Err(e) => Err(axum::Error::new(e)),
}
})
});

Expand Down Expand Up @@ -76,16 +76,14 @@ where
) -> BoxStream<'b, Result<axum::body::Bytes, axum::Error>> {
let stream_bytes: BoxStream<Result<axum::body::Bytes, axum::Error>> = Box::pin({
stream.enumerate().map(|(index, obj)| {
let mut output = vec![];
serde_json::to_vec::<T>(&obj)
.map(|obj_vec| {
if index != 0 {
output.extend(JSON_NL_SEP_BYTES)
}
output.extend(obj_vec);
axum::body::Bytes::from(output)
})
.map_err(axum::Error::new)
let mut buf = BytesMut::new().writer();
if index != 0 {
buf.write_all(JSON_NL_SEP_BYTES).unwrap();
}
match serde_json::to_writer(&mut buf, &obj) {
Ok(_) => Ok(buf.into_inner().freeze()),
Err(e) => Err(axum::Error::new(e)),
}
})
});

Expand Down

0 comments on commit 1695985

Please sign in to comment.