From 585495d314defc65914d57ae5c1f950576564bd8 Mon Sep 17 00:00:00 2001 From: Sebastien La Duca Date: Wed, 27 Jul 2022 23:53:26 -0400 Subject: [PATCH] feature-gate stub TimingTree --- plonky2/Cargo.toml | 4 ++++ plonky2/src/fri/oracle.rs | 6 +++--- plonky2/src/fri/prover.rs | 6 +++--- plonky2/src/util/timing.rs | 43 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/plonky2/Cargo.toml b/plonky2/Cargo.toml index 6f491e1d..8542f307 100644 --- a/plonky2/Cargo.toml +++ b/plonky2/Cargo.toml @@ -10,6 +10,10 @@ categories = ["cryptography"] edition = "2021" default-run = "generate_constants" +[features] +default = ["timing"] +timing = [] + [dependencies] plonky2_field = { path = "../field" } plonky2_util = { path = "../util" } diff --git a/plonky2/src/fri/oracle.rs b/plonky2/src/fri/oracle.rs index 312b458b..7499e696 100644 --- a/plonky2/src/fri/oracle.rs +++ b/plonky2/src/fri/oracle.rs @@ -71,7 +71,7 @@ impl, C: GenericConfig, const D: usize> rate_bits: usize, blinding: bool, cap_height: usize, - timing: &mut TimingTree, + _timing: &mut TimingTree, fft_root_table: Option<&FftRootTable>, ) -> Self where @@ -79,7 +79,7 @@ impl, C: GenericConfig, const D: usize> { let degree = polynomials[0].len(); let lde_values = timed!( - timing, + _timing, "FFT + blinding", Self::lde_values(&polynomials, rate_bits, blinding, fft_root_table) ); @@ -87,7 +87,7 @@ impl, C: GenericConfig, const D: usize> let mut leaves = timed!(timing, "transpose LDEs", transpose(&lde_values)); reverse_index_bits_in_place(&mut leaves); let merkle_tree = timed!( - timing, + _timing, "build Merkle tree", MerkleTree::new(leaves, cap_height) ); diff --git a/plonky2/src/fri/prover.rs b/plonky2/src/fri/prover.rs index 6136a9a1..f8467d2b 100644 --- a/plonky2/src/fri/prover.rs +++ b/plonky2/src/fri/prover.rs @@ -23,7 +23,7 @@ pub fn fri_proof, C: GenericConfig, const lde_polynomial_values: PolynomialValues, challenger: &mut Challenger, fri_params: &FriParams, - timing: &mut TimingTree, + _timing: &mut TimingTree, ) -> FriProof where [(); C::Hasher::HASH_SIZE]:, @@ -33,7 +33,7 @@ where // Commit phase let (trees, final_coeffs) = timed!( - timing, + _timing, "fold codewords in the commitment phase", fri_committed_trees::( lde_polynomial_coeffs, @@ -46,7 +46,7 @@ where // PoW phase let current_hash = challenger.get_hash(); let pow_witness = timed!( - timing, + _timing, "find proof-of-work witness", fri_proof_of_work::(current_hash, &fri_params.config) ); diff --git a/plonky2/src/util/timing.rs b/plonky2/src/util/timing.rs index 4250d688..70f34306 100644 --- a/plonky2/src/util/timing.rs +++ b/plonky2/src/util/timing.rs @@ -1,8 +1,10 @@ +#[cfg(feature = "timing")] use std::time::{Duration, Instant}; use log::{log, Level}; /// The hierarchy of scopes, and the time consumed by each one. Useful for profiling. +#[cfg(feature = "timing")] pub struct TimingTree { /// The name of this scope. name: String, @@ -16,13 +18,25 @@ pub struct TimingTree { children: Vec, } +#[cfg(not(feature = "timing"))] +pub struct TimingTree(Level); + +#[cfg(feature = "timing")] impl Default for TimingTree { fn default() -> Self { TimingTree::new("root", Level::Debug) } } +#[cfg(not(feature = "timing"))] +impl Default for TimingTree { + fn default() -> Self { + TimingTree::new("", Level::Debug) + } +} + impl TimingTree { + #[cfg(feature = "timing")] pub fn new(root_name: &str, level: Level) -> Self { Self { name: root_name.to_string(), @@ -33,18 +47,26 @@ impl TimingTree { } } + #[cfg(not(feature = "timing"))] + pub fn new(_root_name: &str, level: Level) -> Self { + Self(level) + } + /// Whether this scope is still in scope. + #[cfg(feature = "timing")] fn is_open(&self) -> bool { self.exit_time.is_none() } /// A description of the stack of currently-open scopes. + #[cfg(feature = "timing")] pub fn open_stack(&self) -> String { let mut stack = Vec::new(); self.open_stack_helper(&mut stack); stack.join(" > ") } + #[cfg(feature = "timing")] fn open_stack_helper(&self, stack: &mut Vec) { if self.is_open() { stack.push(self.name.clone()); @@ -54,6 +76,7 @@ impl TimingTree { } } + #[cfg(feature = "timing")] pub fn push(&mut self, ctx: &str, mut level: log::Level) { assert!(self.is_open()); @@ -77,6 +100,7 @@ impl TimingTree { } /// Close the deepest open scope from this tree. + #[cfg(feature = "timing")] pub fn pop(&mut self) { assert!(self.is_open()); @@ -90,6 +114,7 @@ impl TimingTree { self.exit_time = Some(Instant::now()); } + #[cfg(feature = "timing")] fn duration(&self) -> Duration { self.exit_time .unwrap_or_else(Instant::now) @@ -97,6 +122,7 @@ impl TimingTree { } /// Filter out children with a low duration. + #[cfg(feature = "timing")] pub fn filter(&self, min_delta: Duration) -> Self { Self { name: self.name.clone(), @@ -112,10 +138,17 @@ impl TimingTree { } } + #[cfg(feature = "timing")] pub fn print(&self) { self.print_helper(0); } + #[cfg(not(feature = "timing"))] + pub fn print(&self) { + log!(self.0, "TimingTree is not supported without the 'timing' feature enabled"); + } + + #[cfg(feature = "timing")] fn print_helper(&self, depth: usize) { let prefix = "| ".repeat(depth); log!( @@ -135,16 +168,26 @@ impl TimingTree { #[macro_export] macro_rules! timed { ($timing_tree:expr, $level:expr, $ctx:expr, $exp:expr) => {{ + #[cfg(feature = "timing")] $timing_tree.push($ctx, $level); + let res = $exp; + + #[cfg(feature = "timing")] $timing_tree.pop(); + res }}; // If no context is specified, default to Debug. ($timing_tree:expr, $ctx:expr, $exp:expr) => {{ + #[cfg(feature = "timing")] $timing_tree.push($ctx, log::Level::Debug); + let res = $exp; + + #[cfg(feature = "timing")] $timing_tree.pop(); + res }}; }