Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions pallas-codec/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,16 @@ impl<A> MaybeIndefArray<A> {
pub fn to_vec(self) -> Vec<A> {
self.into()
}

pub fn map_into<B, F>(self, f: F) -> MaybeIndefArray<B>
where
F: FnMut(A) -> B,
{
match self {
MaybeIndefArray::Def(x) => MaybeIndefArray::Def(x.into_iter().map(f).collect()),
MaybeIndefArray::Indef(x) => MaybeIndefArray::Indef(x.into_iter().map(f).collect()),
}
}
}

impl<A> Deref for MaybeIndefArray<A> {
Expand Down Expand Up @@ -1157,6 +1167,71 @@ impl<C, T> minicbor::Encode<C> for KeepRaw<'_, T> {
}
}

/// Decodes just a raw bytes with skipping actual decoding of the CBOR object.
/// Stores the original CBOR bytes for further decoding.
///
/// # Examples
///
/// ```
/// use pallas_codec::utils::OnlyRaw;
///
/// let a = (123u16, (456u16, 789u16), 123u16);
/// let data = minicbor::to_vec(a).unwrap();
///
/// let (_, keeper, _): (u16, OnlyRaw<(u16, u16)>, u16) = minicbor::decode(&data).unwrap();
/// let confirm: (u16, u16) = keeper.decode().unwrap();
/// assert_eq!(confirm, (456u16, 789u16));
/// ```
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct OnlyRaw<'b, T> {
raw: &'b [u8],
_p: std::marker::PhantomData<T>,
}

impl<'b, T> OnlyRaw<'b, T> {
pub fn raw_cbor(&self) -> &'b [u8] {
self.raw
}
}

impl<'b, T> OnlyRaw<'b, T>
where
T: minicbor::Decode<'b, ()>,
{
pub fn decode(&self) -> Result<T, minicbor::decode::Error> {
minicbor::decode(self.raw)
}
}

impl<'b, T, C> minicbor::Decode<'b, C> for OnlyRaw<'b, T>
where
T: minicbor::Decode<'b, C>,
{
fn decode(d: &mut minicbor::Decoder<'b>, _: &mut C) -> Result<Self, minicbor::decode::Error> {
let all = d.input();
let start = d.position();
d.skip()?;
let end = d.position();

Ok(Self {
raw: &all[start..end],
_p: std::marker::PhantomData,
})
}
}

impl<C, T> minicbor::Encode<C> for OnlyRaw<'_, T> {
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
_ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
e.writer_mut()
.write_all(self.raw_cbor())
.map_err(minicbor::encode::Error::write)
}
}

/// Struct to hold arbitrary CBOR to be processed independently
///
/// # Examples
Expand Down
7 changes: 7 additions & 0 deletions pallas-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ authors = [
"Lucas Rosa <[email protected]>",
]

[[bench]]
name = "alonzo_decoding"
harness = false

[dependencies]
hex = "0.4.3"
log = "0.4.14"
Expand All @@ -23,6 +27,9 @@ bech32 = "0.9.0"
serde = { version = "1.0.136", optional = true, features = ["derive"] }
serde_json = { version = "1.0.79", optional = true }

[dev-dependencies]
criterion = { version = "0.5.1" }

[features]
json = ["serde", "serde_json"]
default = ["json"]
30 changes: 30 additions & 0 deletions pallas-primitives/benches/alonzo_decoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use criterion::{criterion_group, criterion_main, Criterion};
use pallas_codec::utils::{KeepRaw, OnlyRaw};
use pallas_primitives::alonzo::AuxiliaryData;

const AUXILARY_HEX: &'static str = "d90103a100a11902d1a278386238663665376634326563336264303239363662343034313131616530653233636238356539303236343462666237636539343036303766a1781c63756d44796e616d6963496e746567726174696f6e456e67696e6565a76538316e6f6e6868617264776172656b383973696d696c697175656d41646d696e6973747261746f726b6465736372697074696f6e783e4d79206e65696768626f7220416c69646120686173206f6e65206f662074686573652e2053686520776f726b7320617320612067616d626c657220616e646566696c657383a3696d656469615479706569696d6167652f706e67646e616d65781c63756d44796e616d6963496e746567726174696f6e456e67696e6565637372637835697066733a2f2f516d5a46373437504659565a6d5161363163777351555268586131783634344b415631377778766b454238756577a3696d656469615479706569696d6167652f706e67646e616d65781c63756d44796e616d6963496e746567726174696f6e456e67696e6565637372637835697066733a2f2f516d644633466b59395277686d74783636534e773373637555756f444852745439684e4d36646f63553767326544a3696d656469615479706569696d6167652f706e67646e616d65781c63756d44796e616d6963496e746567726174696f6e456e67696e6565637372637835697066733a2f2f516d62614a75504e336a463857535544667659687a75324b5a716d6a517868506135736d666245445363366a695765696d6167657835697066733a2f2f516d6156524a7a343652384e72476b4a58684c6170316145323642676a365367746564393442734a487576437556696d656469615479706569696d6167652f706e67646e616d65781c63756d44796e616d6963496e746567726174696f6e456e67696e65656776657273696f6e63312e30";

fn auxilary_data_benches(c: &mut Criterion) {
let bytes = hex::decode(AUXILARY_HEX).unwrap();

let mut group = c.benchmark_group("Alonzo Auxilary Data Decoding");
group.bench_function("KeepRaw<AuxiliaryData>", |b| {
b.iter(|| {
let _aux: KeepRaw<AuxiliaryData> =
pallas_codec::minicbor::decode(bytes.as_slice()).unwrap();
});
});

group.bench_function("OnlyRaw<AuxiliaryData>", |b| {
b.iter(|| {
let _aux: OnlyRaw<AuxiliaryData> =
pallas_codec::minicbor::decode(bytes.as_slice()).unwrap();
});
});

group.finish();
}

criterion_group!(benches, auxilary_data_benches);

criterion_main!(benches);
Loading