Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into kalan/fix-prefixinput
Browse files Browse the repository at this point in the history
  • Loading branch information
bkchr committed Apr 30, 2024
2 parents 570ffb8 + faac8cc commit b4234f2
Show file tree
Hide file tree
Showing 30 changed files with 714 additions and 557 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ jobs:
run: echo "IMAGE=${{ env.IMAGE }}" >> $GITHUB_OUTPUT

# Checks
fmt:
runs-on: ubuntu-latest
needs: [set-image]
container: ${{ needs.set-image.outputs.IMAGE }}
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Show Cargo version
run: cargo +nightly -vV

- name: Cargo fmt
run: cargo +nightly fmt --all -- --check

clippy:
runs-on: ubuntu-latest
needs: [set-image]
Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rust-version = "1.60.0"

[dependencies]
arrayvec = { version = "0.7", default-features = false }
serde = { version = "1.0.198", default-features = false, optional = true }
serde = { version = "1.0.199", default-features = false, optional = true }
parity-scale-codec-derive = { path = "derive", version = ">= 3.6.8", default-features = false, optional = true }
bitvec = { version = "1", default-features = false, features = [ "alloc" ], optional = true }
bytes = { version = "1", default-features = false, optional = true }
Expand Down
160 changes: 85 additions & 75 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{time::Duration, any::type_name, convert::{TryFrom, TryInto}};
use std::{
any::type_name,
convert::{TryFrom, TryInto},
time::Duration,
};

#[cfg(feature = "bit-vec")]
use bitvec::{vec::BitVec, order::Lsb0};
use criterion::{Criterion, black_box, Bencher, criterion_group, criterion_main};
use bitvec::{order::Lsb0, vec::BitVec};
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use parity_scale_codec::*;
use parity_scale_codec_derive::{Encode, Decode};
use parity_scale_codec_derive::{Decode, Encode};

fn array_vec_write_u128(b: &mut Bencher) {
b.iter(|| {
for b in 0..black_box(1_000_000) {
let a = 0xffff_ffff_ffff_ffff_ffff_u128;
Compact(a ^ b).using_encoded(|x| {
black_box(x).len()
});
Compact(a ^ b).using_encoded(|x| black_box(x).len());
}
});
}
Expand Down Expand Up @@ -84,8 +86,7 @@ fn vec_append_with_decode_and_encode(b: &mut Bencher) {
b.iter(|| {
let mut encoded_events_vec = Vec::new();
for _ in 0..1000 {
let mut events = Vec::<Event>::decode(&mut &encoded_events_vec[..])
.unwrap_or_default();
let mut events = Vec::<Event>::decode(&mut &encoded_events_vec[..]).unwrap_or_default();

events.push(Event::ComplexEvent(data.to_vec(), 4, 5, 6, 9));

Expand All @@ -107,62 +108,69 @@ fn vec_append_with_encode_append(b: &mut Bencher) {
encoded_events_vec = <Vec<Event> as EncodeAppend>::append_or_new(
encoded_events_vec,
&[Event::ComplexEvent(data.to_vec(), 4, 5, 6, 9)],
).unwrap();
)
.unwrap();
}
});
}

fn encode_decode_vec<T: TryFrom<u8> + Codec>(c: &mut Criterion) where T::Error: std::fmt::Debug {
fn encode_decode_vec<T: TryFrom<u8> + Codec>(c: &mut Criterion)
where
T::Error: std::fmt::Debug,
{
let mut g = c.benchmark_group("vec_encode");
for vec_size in [1, 2, 5, 32, 1024, 2048, 16384] {
g.bench_with_input(&format!("{}/{}", type_name::<T>(), vec_size), &vec_size, |b, &vec_size| {
let vec: Vec<T> = (0..=127u8)
.cycle()
.take(vec_size)
.map(|v| v.try_into().unwrap())
.collect();

let vec = black_box(vec);
b.iter(|| vec.encode())
});
g.bench_with_input(
&format!("{}/{}", type_name::<T>(), vec_size),
&vec_size,
|b, &vec_size| {
let vec: Vec<T> =
(0..=127u8).cycle().take(vec_size).map(|v| v.try_into().unwrap()).collect();

let vec = black_box(vec);
b.iter(|| vec.encode())
},
);
}

drop(g);
let mut g = c.benchmark_group("vec_decode");
for vec_size in [1, 2, 5, 32, 1024, 2048, 16384] {
g.bench_with_input(&format!("{}/{}", type_name::<T>(), vec_size), &vec_size, |b, &vec_size| {
let vec: Vec<T> = (0..=127u8)
.cycle()
.take(vec_size)
.map(|v| v.try_into().unwrap())
.collect();

let vec = vec.encode();

let vec = black_box(vec);
b.iter(|| {
let _: Vec<T> = Decode::decode(&mut &vec[..]).unwrap();
})
});
g.bench_with_input(
&format!("{}/{}", type_name::<T>(), vec_size),
&vec_size,
|b, &vec_size| {
let vec: Vec<T> =
(0..=127u8).cycle().take(vec_size).map(|v| v.try_into().unwrap()).collect();

let vec = vec.encode();

let vec = black_box(vec);
b.iter(|| {
let _: Vec<T> = Decode::decode(&mut &vec[..]).unwrap();
})
},
);
}

drop(g);
let mut g = c.benchmark_group("vec_decode_no_limit");
for vec_size in [16384, 131072] {
g.bench_with_input(&format!("vec_decode_no_limit_{}/{}", type_name::<T>(), vec_size), &vec_size, |b, &vec_size| {
let vec: Vec<T> = (0..=127u8)
.cycle()
.take(vec_size)
.map(|v| v.try_into().unwrap())
.collect();

let vec = vec.encode();

let vec = black_box(vec);
b.iter(|| {
let _: Vec<T> = Decode::decode(&mut NoLimitInput(&vec[..])).unwrap();
})
});
g.bench_with_input(
&format!("vec_decode_no_limit_{}/{}", type_name::<T>(), vec_size),
&vec_size,
|b, &vec_size| {
let vec: Vec<T> =
(0..=127u8).cycle().take(vec_size).map(|v| v.try_into().unwrap()).collect();

let vec = vec.encode();

let vec = black_box(vec);
b.iter(|| {
let _: Vec<T> = Decode::decode(&mut NoLimitInput(&vec[..])).unwrap();
})
},
);
}
}

Expand All @@ -183,28 +191,38 @@ fn encode_decode_complex_type(c: &mut Criterion) {
let mut g = c.benchmark_group("vec_encode_complex_type");
for vec_size in [1, 2, 5, 32, 1024, 2048, 16384] {
let complex_types = complex_types.clone();
g.bench_with_input(format!("vec_encode_complex_type/{}", vec_size), &vec_size, move |b, &vec_size| {
let vec: Vec<ComplexType> = complex_types.clone().into_iter().cycle().take(vec_size).collect();
g.bench_with_input(
format!("vec_encode_complex_type/{}", vec_size),
&vec_size,
move |b, &vec_size| {
let vec: Vec<ComplexType> =
complex_types.clone().into_iter().cycle().take(vec_size).collect();

let vec = black_box(vec);
b.iter(|| vec.encode())
});
let vec = black_box(vec);
b.iter(|| vec.encode())
},
);
}

drop(g);
let mut g = c.benchmark_group("vec_decode_complex_type");
for vec_size in [1, 2, 5, 32, 1024, 2048, 16384] {
let complex_types = complex_types.clone();
g.bench_with_input(format!("vec_decode_complex_type/{}", vec_size), &vec_size, move |b, &vec_size| {
let vec: Vec<ComplexType> = complex_types.clone().into_iter().cycle().take(vec_size).collect();
g.bench_with_input(
format!("vec_decode_complex_type/{}", vec_size),
&vec_size,
move |b, &vec_size| {
let vec: Vec<ComplexType> =
complex_types.clone().into_iter().cycle().take(vec_size).collect();

let vec = vec.encode();
let vec = vec.encode();

let vec = black_box(vec);
b.iter(|| {
let _: Vec<ComplexType> = Decode::decode(&mut &vec[..]).unwrap();
})
});
let vec = black_box(vec);
b.iter(|| {
let _: Vec<ComplexType> = Decode::decode(&mut &vec[..]).unwrap();
})
},
);
}
}

Expand All @@ -225,12 +243,8 @@ fn encode_decode_bitvec_u8(c: &mut Criterion) {
let mut g = c.benchmark_group("bitvec_u8_encode");
for size in [1, 2, 5, 32, 1024] {
g.bench_with_input(size.to_string(), &size, |b, &size| {
let vec: BitVec<u8, Lsb0> = [true, false]
.iter()
.cloned()
.cycle()
.take(size)
.collect();
let vec: BitVec<u8, Lsb0> =
[true, false].iter().cloned().cycle().take(size).collect();

let vec = black_box(vec);
b.iter(|| vec.encode())
Expand All @@ -243,12 +257,8 @@ fn encode_decode_bitvec_u8(c: &mut Criterion) {
let mut g = c.benchmark_group("bitvec_u8_decode");
for size in [1, 2, 5, 32, 1024] {
g.bench_with_input(size.to_string(), &size, |b, &size| {
let vec: BitVec<u8, Lsb0> = [true, false]
.iter()
.cloned()
.cycle()
.take(size)
.collect();
let vec: BitVec<u8, Lsb0> =
[true, false].iter().cloned().cycle().take(size).collect();

let vec = vec.encode();

Expand All @@ -261,7 +271,7 @@ fn encode_decode_bitvec_u8(c: &mut Criterion) {
}
}

criterion_group!{
criterion_group! {
name = benches;
config = Criterion::default().warm_up_time(Duration::from_millis(500)).without_plots();
targets = encode_decode_vec::<u8>, encode_decode_vec::<u16>, encode_decode_vec::<u32>, encode_decode_vec::<u64>,
Expand Down
Loading

0 comments on commit b4234f2

Please sign in to comment.