Open AliBasicCoder opened 1 month ago
Hi Ali, thank you for this contribution! I suspect the discrepancy may be due to the memory allocation time for the chunks. Could you try allocating a single Vec
at initialization time and reusing it for each chunk, or using borrowed slices as chunks instead? You could do this by calling futures::stream::iter(data.chunks(chunk_size))
.
cf. https://doc.rust-lang.org/std/primitive.slice.html#method.chunks https://docs.rs/futures/latest/futures/stream/fn.iter.html
tried this
let source = stream::iter(DATA.chunks(chunk_size))
.map(Bytes::from_static)
.map(Result::<Bytes, destream_json::de::Error>::Ok);
no effect
tried this
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use futures::stream;
use std::hint::black_box;
const DATA: &'static [u8] = include_bytes!("generated.json");
fn destream_json(c: &mut Criterion) {
use bytes::Bytes;
use criterion::async_executor::FuturesExecutor;
use futures::StreamExt;
static KB: usize = 1_000;
let mut group = c.benchmark_group("destream_json");
async fn test(chunks: Vec<Result<Bytes, destream_json::de::Error>>, chunk_size: usize) {
let source = stream::iter(chunks);
#[allow(unused_variables)]
let result: destream_json::Value =
black_box(destream_json::try_decode((), source).await.unwrap());
}
group.sample_size(10);
for size in [10, 1 * KB, 10 * KB, 30 * KB, 50 * KB, 100 * KB].iter() {
let mut chunks: Vec<Result<Bytes, destream_json::de::Error>> = DATA
.chunks(*size)
.map(Bytes::from_static)
.map(Result::<Bytes, destream_json::de::Error>::Ok)
.collect();
group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
b.to_async(FuturesExecutor)
.iter(|| test(std::mem::take(&mut chunks), size))
});
}
group.finish();
}
panics unexpected end of stream
Ok in that case it does seem like there's an issue. Could you submit a PR with your code for debugging purposes? Please don't include the "generated.json" file itself but consider including the code used to generate it.
hi, i'm interested in this repo so i was was curios about performance so i wrote the following benchmark code
result:
as you can see performance is greatly reduced the bigger the chunks are
my machine is i3-8100 cpu, no external gpu runs ubuntu