rkyv_codec
Simple async codec for rkyv. Reuses streaming buffer for maximum speed!
This crate provides a makeshift adaptor for streaming &Archived<Object>s from an AsyncRead using a reusable external buffer, as well as a futures::Sink implementation to serialize Objects to an AsyncWrite.
It uses multiformat's unsigned_varint for variable-length length encoding by default, but allows for other kinds of length encoding through the LengthEncoding trait.
It also supports directly using bytes::BytesMut and #[no_std] when default features are disabled.
Examples
This crate has three examples: chat_client, chat_server & no-std. Run the first two at the same time to see a proof-of-concept Archive tcp echo server in action.
To run:
cargo run --example chat_client
cargo run --example chat_server
Simple usage example (RkyvCodec):
use ;
use ;
use ;
use BytesMut;
let value = Test ;
let mut codec = default;
let mut buf = new;
// Encoding
codec.encode.unwrap;
// Decoding
let decoded_value = codec.decode.unwrap.unwrap;
assert_eq!;
Zero-copy archive usage example (RkyvWriter/archive_stream):
use ;
use ;
use SinkExt;
let value = Test ;
// Writing
let writer = Vecnew;
let mut codec = new;
codec.send.await.unwrap;
// Reading
let mut reader = &codec.inner;
let mut buffer = new; // Aligned streaming buffer for re-use
let value_archived: & = .await.unwrap; // This returns a reference into the passed buffer
// can deserialize as normal as well (or do *partial* deserialization for blazingly fast speeds!)
let value_deserialized: Test = .unwrap;
assert_eq!; // compare to archived version
assert_eq!; // compare to deserialized version
See examples/no-std for an example with no-std support.
Benchmarks
These are a set of benchmarks, each benchmark represents 50 test objects being either sent or received. (requires nightly)
test tests::bench_archive_sink_50 ... bench: 6,276.07 ns/iter (+/- 250.20)
test tests::bench_archive_sink_prearchived_50 ... bench: 2,009.84 ns/iter (+/- 175.23)
test tests::bench_archive_stream_50 ... bench: 2,110.66 ns/iter (+/- 150.51)
test tests::bench_futures_cbor_sink_50 ... bench: 8,178.37 ns/iter (+/- 123.13)
test tests::bench_futures_cbor_stream_50 ... bench: 6,606.01 ns/iter (+/- 129.89)
test tests::bench_rkyv_asynchronous_codec_sink_50 ... bench: 4,328.91 ns/iter (+/- 415.17)
test tests::bench_rkyv_asynchronous_codec_stream_50 ... bench: 4,059.72 ns/iter (+/- 243.54)
test tests::bench_rkyv_writer_50 ... bench: 2,228.49 ns/iter (+/- 185.92)
test tests::bench_u64_length_encoding ... bench: 2,494.63 ns/iter (+/- 143.85)
test tests::bench_varint_length_encoding ... bench: 4,036.83 ns/iter (+/- 471.98)
The fastest real benchmark (full serialization and bytecheck) is using RkyvWriter for writing and archive_stream for reading.
This is compared to the slowest benchmark: the asynchronous-codec library's CborCodec.
Feel free to contribute your own benchmarks!