Switch to `memuse` crate for measuring heap allocations

This commit is contained in:
Jack Grigg 2021-09-03 21:22:22 +01:00
parent bd2a7c2e79
commit 7fad21e7d6
3 changed files with 39 additions and 14 deletions

View File

@ -29,6 +29,7 @@ fpe = "0.4"
group = "0.10" group = "0.10"
halo2 = "0.0" halo2 = "0.0"
lazy_static = "1" lazy_static = "1"
memuse = { version = "0.1", features = ["nonempty"] }
pasta_curves = "0.1.2" pasta_curves = "0.1.2"
proptest = { version = "1.0.0", optional = true } proptest = { version = "1.0.0", optional = true }
rand = "0.8" rand = "0.8"

View File

@ -3,9 +3,9 @@
pub mod commitments; pub mod commitments;
use std::io; use std::io;
use std::mem;
use blake2b_simd::Hash as Blake2bHash; use blake2b_simd::Hash as Blake2bHash;
use memuse::{DynamicUsage, NoDynamicUsage};
use nonempty::NonEmpty; use nonempty::NonEmpty;
use zcash_note_encryption::try_note_decryption; use zcash_note_encryption::try_note_decryption;
@ -134,6 +134,8 @@ impl<T> Action<T> {
} }
} }
impl NoDynamicUsage for Action<redpallas::Signature<SpendAuth>> {}
/// Orchard-specific flags. /// Orchard-specific flags.
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct Flags { pub struct Flags {
@ -441,13 +443,6 @@ impl Authorized {
} }
impl<V> Bundle<Authorized, V> { impl<V> Bundle<Authorized, V> {
/// Returns the amount of heap-allocated memory used by this bundle.
pub fn dynamic_usage(&self) -> usize {
// NonEmpty<T> stores its head element separately from its tail Vec<T>.
(self.actions.capacity() - 1) * mem::size_of::<Action<redpallas::Signature<SpendAuth>>>()
+ self.authorization.proof.dynamic_usage()
}
/// Computes a commitment to the authorizing data within for this bundle. /// Computes a commitment to the authorizing data within for this bundle.
/// ///
/// This together with `Bundle::commitment` bind the entire bundle. /// This together with `Bundle::commitment` bind the entire bundle.
@ -463,6 +458,31 @@ impl<V> Bundle<Authorized, V> {
} }
} }
impl<V: DynamicUsage> DynamicUsage for Bundle<Authorized, V> {
fn dynamic_usage(&self) -> usize {
self.actions.dynamic_usage()
+ self.value_balance.dynamic_usage()
+ self.authorization.proof.dynamic_usage()
}
fn dynamic_usage_bounds(&self) -> (usize, Option<usize>) {
let bounds = (
self.actions.dynamic_usage_bounds(),
self.value_balance.dynamic_usage_bounds(),
self.authorization.proof.dynamic_usage_bounds(),
);
(
bounds.0 .0 + bounds.1 .0 + bounds.2 .0,
bounds
.0
.1
.zip(bounds.1 .1)
.zip(bounds.2 .1)
.map(|((a, b), c)| a + b + c),
)
}
}
/// A commitment to a bundle of actions. /// A commitment to a bundle of actions.
/// ///
/// This commitment is non-malleable, in the sense that a bundle's commitment will only /// This commitment is non-malleable, in the sense that a bundle's commitment will only

View File

@ -1,7 +1,5 @@
//! The Orchard Action circuit implementation. //! The Orchard Action circuit implementation.
use std::mem;
use group::{Curve, GroupEncoding}; use group::{Curve, GroupEncoding};
use halo2::{ use halo2::{
circuit::{floor_planner, Layouter}, circuit::{floor_planner, Layouter},
@ -9,6 +7,7 @@ use halo2::{
poly::Rotation, poly::Rotation,
transcript::{Blake2bRead, Blake2bWrite}, transcript::{Blake2bRead, Blake2bWrite},
}; };
use memuse::DynamicUsage;
use pasta_curves::{ use pasta_curves::{
arithmetic::{CurveAffine, FieldExt}, arithmetic::{CurveAffine, FieldExt},
pallas, vesta, pallas, vesta,
@ -849,12 +848,17 @@ impl AsRef<[u8]> for Proof {
} }
} }
impl Proof { impl DynamicUsage for Proof {
/// Returns the amount of heap-allocated memory used by this proof. fn dynamic_usage(&self) -> usize {
pub(crate) fn dynamic_usage(&self) -> usize { self.0.dynamic_usage()
self.0.capacity() * mem::size_of::<u8>()
} }
fn dynamic_usage_bounds(&self) -> (usize, Option<usize>) {
self.0.dynamic_usage_bounds()
}
}
impl Proof {
/// Creates a proof for the given circuits and instances. /// Creates a proof for the given circuits and instances.
pub fn create( pub fn create(
pk: &ProvingKey, pk: &ProvingKey,