Skip to content

Commit

Permalink
Merge pull request #198 from toad-lib/toad-msg/msg-get-options
Browse files Browse the repository at this point in the history
feat: add trait for getting & setting options
  • Loading branch information
cakekindel authored Jan 31, 2023
2 parents 77019c4 + dc37d36 commit ace3df0
Show file tree
Hide file tree
Showing 12 changed files with 687 additions and 359 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 39 additions & 31 deletions toad-msg/benches/bench_input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use std::collections::BTreeMap;

use tinyvec::ArrayVec;
use toad_msg::*;

#[path = "common.rs"]
mod common;
use common::*;

#[derive(Debug, PartialEq, PartialOrd, Ord, Eq)]
pub struct TestInput {
pub tkl: u8,
Expand All @@ -13,62 +19,64 @@ impl TestInput {
pub fn get_bytes(&self) -> Vec<u8> {
self.get_alloc_message().try_into_bytes::<Vec<_>>().unwrap()
}
pub fn get_alloc_message(&self) -> VecMessage {
pub fn get_alloc_message(&self) -> alloc::Message {
self.into()
}
pub fn get_no_alloc_message<const P: usize, const N: usize, const O: usize>(
&self)
-> ArrayVecMessage<P, N, O> {
-> StackMessage<P, N, O> {
self.into()
}
pub fn get_coap_lite_packet(&self) -> coap_lite::Packet {
coap_lite::Packet::from_bytes(&self.get_bytes()).unwrap()
}
}

impl<'a> From<&'a TestInput> for VecMessage {
fn from(inp: &'a TestInput) -> VecMessage {
let opts: Vec<_> =
(0..inp.n_opts).map(|n| Opt { delta: OptDelta(n as _),
value: OptValue(core::iter::repeat(1).take(inp.opt_size)
.collect()) })
.collect();
impl<'a> From<&'a TestInput> for alloc::Message {
fn from(inp: &'a TestInput) -> alloc::Message {
let opts: BTreeMap<_, _> = (0..inp.n_opts).map(|n| {
(OptNumber(n as u32),
vec![OptValue(core::iter::repeat(1).take(inp.opt_size)
.collect())])
})
.collect();

let token = core::iter::repeat(1u8).take(inp.tkl as _)
.collect::<tinyvec::ArrayVec<[_; 8]>>();

VecMessage { id: Id(1),
ty: Type::Non,
ver: Default::default(),
token: Token(token),
code: Code { class: 2,
detail: 5 },
opts,
payload: Payload(core::iter::repeat(1u8).take(inp.payload_size).collect()) }
alloc::Message { id: Id(1),
ty: Type::Non,
ver: Default::default(),
token: Token(token),
code: Code { class: 2,
detail: 5 },
opts,
payload: Payload(core::iter::repeat(1u8).take(inp.payload_size).collect()) }
}
}

impl<'a, const P: usize, const N: usize, const O: usize> From<&'a TestInput>
for ArrayVecMessage<P, N, O>
for StackMessage<P, N, O>
{
fn from(inp: &'a TestInput) -> ArrayVecMessage<P, N, O> {
fn from(inp: &'a TestInput) -> StackMessage<P, N, O> {
let opts: ArrayVec<[_; N]> =
(0..inp.n_opts).map(|n| Opt::<_> { delta: OptDelta(n as _),
value:
OptValue::<_>(core::iter::repeat(1).take(inp.opt_size)
.collect()) })
(0..inp.n_opts).map(|n| {
(OptNumber(n as u32),
tinyvec::array_vec![_ => OptValue(core::iter::repeat(1).take(inp.opt_size)
.collect())])
})
.collect();

let token = core::iter::repeat(1u8).take(inp.tkl as _)
.collect::<tinyvec::ArrayVec<[_; 8]>>();

ArrayVecMessage { id: Id(1),
ty: Type::Non,
ver: Default::default(),
token: Token(token),
code: Code { class: 2,
detail: 5 },
opts,
payload: Payload(core::iter::repeat(1u8).take(inp.payload_size).collect()) }
StackMessage { id: Id(1),
ty: Type::Non,
ver: Default::default(),
token: Token(token),
code: Code { class: 2,
detail: 5 },
opts,
payload: Payload(core::iter::repeat(1u8).take(inp.payload_size).collect()) }
}
}
6 changes: 6 additions & 0 deletions toad-msg/benches/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use tinyvec::ArrayVec;
use toad_msg::*;

pub type StackMessage<const PAYLOAD_BYTES: usize, const OPTS_MAX: usize, const OPT_BYTES: usize> =
Message<ArrayVec<[u8; PAYLOAD_BYTES]>,
ArrayVec<[(OptNumber, ArrayVec<[OptValue<ArrayVec<[u8; OPT_BYTES]>>; 1]>); OPTS_MAX]>>;
8 changes: 6 additions & 2 deletions toad-msg/benches/from_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ use toad_msg::*;
mod bench_input;
use bench_input::TestInput;

#[path = "common.rs"]
mod common;
use common::*;

fn message_from_bytes(c: &mut Criterion) {
let mut group = c.benchmark_group("msg/from_bytes");
group.measurement_time(std::time::Duration::from_secs(5));
Expand Down Expand Up @@ -66,14 +70,14 @@ fn message_from_bytes(c: &mut Criterion) {
opt_size: 512,
payload_size: 4096 },];

type ArrayMessage = ArrayVecMessage<4096, 32, 512>;
type ArrayMessage = StackMessage<4096, 32, 512>;

for inp in inputs.iter() {
let bytes = inp.get_bytes();

group.bench_with_input(BenchmarkId::new("toad_msg/alloc/size", bytes.len()),
&bytes,
|b, bytes| b.iter(|| VecMessage::try_from_bytes(bytes)));
|b, bytes| b.iter(|| alloc::Message::try_from_bytes(bytes)));

group.bench_with_input(BenchmarkId::new("toad_msg/no_alloc/size", bytes.len()),
&bytes,
Expand Down
10 changes: 7 additions & 3 deletions toad-msg/benches/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ use toad_msg::*;
mod bench_input;
use bench_input::TestInput;

#[path = "common.rs"]
mod common;
use common::*;

fn profile(c: &mut Criterion) {
type ArrayMessage = ArrayVecMessage<1024, 16, 128>;
type ArrayMessage = StackMessage<1024, 16, 128>;
let inp = TestInput { tkl: 8,
n_opts: 16,
opt_size: 128,
Expand All @@ -19,7 +23,7 @@ fn profile(c: &mut Criterion) {
c.bench_with_input(BenchmarkId::new("msg/profile/to_bytes", "toad_msg/alloc"),
&inp,
|b, inp| {
b.iter_batched(|| VecMessage::from(inp),
b.iter_batched(|| alloc::Message::from(inp),
|m| m.try_into_bytes::<Vec<u8>>().unwrap(),
BatchSize::SmallInput)
});
Expand All @@ -36,7 +40,7 @@ fn profile(c: &mut Criterion) {

c.bench_with_input(BenchmarkId::new("msg/profile/from_bytes", "toad_msg/alloc"),
&bytes,
|b, bytes| b.iter(|| VecMessage::try_from_bytes(bytes)));
|b, bytes| b.iter(|| alloc::Message::try_from_bytes(bytes)));
c.bench_with_input(BenchmarkId::new("msg/profile/from_bytes", "toad_msg/no_alloc"),
&bytes,
|b, bytes| b.iter(|| ArrayMessage::try_from_bytes(bytes)));
Expand Down
2 changes: 1 addition & 1 deletion toad-msg/benches/to_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
use toad_msg::TryIntoBytes;

#[path = "bench_input.rs"]
mod bench_input;
use bench_input::TestInput;
use tinyvec::ArrayVec;
use toad_msg::TryIntoBytes;

fn message_to_bytes(c: &mut Criterion) {
let mut group = c.benchmark_group("msg/to_bytes");
Expand Down
Loading

0 comments on commit ace3df0

Please sign in to comment.