Skip to content

Commit

Permalink
Improve performance of TrieNode serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
styppo authored and jsdanielh committed Jan 26, 2024
1 parent 49cb6a9 commit 5ce7b54
Show file tree
Hide file tree
Showing 16 changed files with 632 additions and 30 deletions.
168 changes: 167 additions & 1 deletion Cargo.lock

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

6 changes: 6 additions & 0 deletions hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ nimiq-serde = { workspace = true }

[dev-dependencies]
nimiq-test-log = { workspace = true }
criterion = "0.5"
postcard = "1.0"

[[bench]]
name = "hash_serialize"
harness = false
23 changes: 23 additions & 0 deletions hash/benches/hash_serialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use nimiq_hash::Blake2bHash;
use postcard;

fn serialize_hash(c: &mut Criterion) {
let hash = Blake2bHash::default();
let mut buf = [0; 32];
let mut group = c.benchmark_group("serialize_hash");
group.bench_function("serde", |b| {
b.iter(|| {
let _ = black_box(postcard::to_slice(black_box(&hash), &mut buf).unwrap());
})
});
group.bench_function("plain", |b| {
b.iter(|| {
let _ = black_box(hash.serialize(&mut buf.as_mut_slice()).unwrap());
})
});
group.finish();
}

criterion_group!(hash, serialize_hash,);
criterion_main!(hash);
14 changes: 9 additions & 5 deletions hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use blake2_rfc::{blake2b::Blake2b, blake2s::Blake2s};
use byteorder::WriteBytesExt;
use hex::FromHex;
use nimiq_database_value::{AsDatabaseBytes, FromDatabaseValue};
use nimiq_macros::{add_hex_io_fns_typed_arr, add_serialization_fns_typed_arr, create_typed_array};
use nimiq_macros::{
add_hex_io_fns_typed_arr, add_raw_serialization_fns_typed_arr,
add_serde_serialization_fns_typed_arr, create_typed_array,
};
use nimiq_mmr::hash::Merge;
use nimiq_serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256, Sha512};
Expand Down Expand Up @@ -110,7 +113,8 @@ pub trait HashOutput:
const BLAKE2B_LENGTH: usize = 32;
create_typed_array!(Blake2bHash, u8, BLAKE2B_LENGTH);
add_hex_io_fns_typed_arr!(Blake2bHash, BLAKE2B_LENGTH);
add_serialization_fns_typed_arr!(Blake2bHash, BLAKE2B_LENGTH);
add_serde_serialization_fns_typed_arr!(Blake2bHash, BLAKE2B_LENGTH);
add_raw_serialization_fns_typed_arr!(Blake2bHash, BLAKE2B_LENGTH);

pub struct Blake2bHasher(Blake2b);
impl HashOutput for Blake2bHash {
Expand Down Expand Up @@ -206,7 +210,7 @@ impl Merge for Blake2bHash {
const BLAKE2S_LENGTH: usize = 32;
create_typed_array!(Blake2sHash, u8, BLAKE2S_LENGTH);
add_hex_io_fns_typed_arr!(Blake2sHash, BLAKE2S_LENGTH);
add_serialization_fns_typed_arr!(Blake2sHash, BLAKE2S_LENGTH);
add_serde_serialization_fns_typed_arr!(Blake2sHash, BLAKE2S_LENGTH);
pub struct Blake2sHasher(Blake2s);
impl HashOutput for Blake2sHash {
type Builder = Blake2sHasher;
Expand Down Expand Up @@ -270,7 +274,7 @@ const NIMIQ_ARGON2_SALT: &str = "nimiqrocks!";
const DEFAULT_ARGON2_COST: u32 = 512;
create_typed_array!(Argon2dHash, u8, ARGON2D_LENGTH);
add_hex_io_fns_typed_arr!(Argon2dHash, ARGON2D_LENGTH);
add_serialization_fns_typed_arr!(Argon2dHash, ARGON2D_LENGTH);
add_serde_serialization_fns_typed_arr!(Argon2dHash, ARGON2D_LENGTH);
pub struct Argon2dHasher {
buf: Vec<u8>,
config: argon2::Config<'static>,
Expand Down Expand Up @@ -352,7 +356,7 @@ impl Hasher for Argon2dHasher {
const SHA256_LENGTH: usize = 32;
create_typed_array!(Sha256Hash, u8, SHA256_LENGTH);
add_hex_io_fns_typed_arr!(Sha256Hash, SHA256_LENGTH);
add_serialization_fns_typed_arr!(Sha256Hash, SHA256_LENGTH);
add_serde_serialization_fns_typed_arr!(Sha256Hash, SHA256_LENGTH);
pub struct Sha256Hasher(Sha256);
impl HashOutput for Sha256Hash {
type Builder = Sha256Hasher;
Expand Down
4 changes: 2 additions & 2 deletions hash/src/sha512.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use nimiq_macros::add_serialization_fns_typed_arr;
use nimiq_macros::add_serde_serialization_fns_typed_arr;

use super::*;

Expand All @@ -7,7 +7,7 @@ pub(super) const SHA512_LENGTH: usize = 64;

pub struct Sha512Hash([u8; SHA512_LENGTH]);

add_serialization_fns_typed_arr!(Sha512Hash, SHA512_LENGTH);
add_serde_serialization_fns_typed_arr!(Sha512Hash, SHA512_LENGTH);

impl<'a> From<&'a [u8]> for Sha512Hash {
fn from(slice: &'a [u8]) -> Self {
Expand Down
1 change: 1 addition & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ workspace = true
hex = "0.4"
serde = "1.0"
serde-big-array = "0.5"
byteorder = "1.5"

nimiq-serde = { workspace = true }

Expand Down

0 comments on commit 5ce7b54

Please sign in to comment.