From c40c1bc0a192567f2167f992f0024caaaa7a1274 Mon Sep 17 00:00:00 2001
From: Jan Ferdinand Sauer <ferdinand@neptune.cash>
Date: Tue, 15 Nov 2022 02:38:16 +0100
Subject: [PATCH] replace timing reporter with profiler

Also, make some debug output only appear when DEBUG=1.
---
 triton-vm/src/fri.rs   |  7 -------
 triton-vm/src/stark.rs | 14 +++++++++-----
 triton-vm/src/vm.rs    | 16 ++++++++++------
 3 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/triton-vm/src/fri.rs b/triton-vm/src/fri.rs
index ddc5c9bea..8fecc7da4 100644
--- a/triton-vm/src/fri.rs
+++ b/triton-vm/src/fri.rs
@@ -17,7 +17,6 @@ use twenty_first::shared_math::rescue_prime_digest::Digest;
 use twenty_first::shared_math::traits::FiniteField;
 use twenty_first::shared_math::traits::{CyclicGroupGenerator, ModPowU32};
 use twenty_first::shared_math::x_field_element::XFieldElement;
-use twenty_first::timing_reporter::TimingReporter;
 use twenty_first::util_types::algebraic_hasher::{AlgebraicHasher, Hashable};
 use twenty_first::util_types::merkle_tree::{MerkleTree, PartialAuthenticationPath};
 
@@ -127,14 +126,11 @@ impl<H: AlgebraicHasher> Fri<H> {
         );
 
         // commit phase
-        let mut timer = TimingReporter::start();
         let (codewords, merkle_trees): (Vec<Vec<XFieldElement>>, Vec<MerkleTree<H>>) =
             self.commit(codeword, proof_stream)?.into_iter().unzip();
-        timer.elapsed("Commit phase");
 
         // Fiat-Shamir to get indices
         let top_level_indices: Vec<usize> = self.sample_indices(&proof_stream.prover_fiat_shamir());
-        timer.elapsed("Sample indices");
 
         // query phase
         // query step 0: enqueue authentication paths for all points `A` into proof stream
@@ -158,11 +154,8 @@ impl<H: AlgebraicHasher> Fri<H> {
                 .collect();
             Self::enqueue_auth_pairs(&b_indices, &codewords[r], &merkle_trees[r], proof_stream);
             current_domain_len /= 2;
-            timer.elapsed(&format!("Query phase {}", r));
         }
 
-        println!("FRI-prover, timing report\n{}", timer.finish());
-
         let merkle_root_of_1st_round: Digest = merkle_trees[0].get_root();
         Ok((top_level_indices, merkle_root_of_1st_round))
     }
diff --git a/triton-vm/src/stark.rs b/triton-vm/src/stark.rs
index 6a9c34203..df9b24acc 100644
--- a/triton-vm/src/stark.rs
+++ b/triton-vm/src/stark.rs
@@ -379,10 +379,12 @@ impl Stark {
 
         prof_stop!(maybe_profiler, "open trace leafs");
 
-        println!(
-            "Created proof containing {} B-field elements",
-            proof_stream.transcript_length()
-        );
+        if std::env::var("DEBUG").is_ok() {
+            println!(
+                "Created proof containing {} B-field elements",
+                proof_stream.transcript_length()
+            );
+        }
 
         proof_stream.to_proof()
     }
@@ -464,7 +466,9 @@ impl Stark {
             (extension_codewords, extension_degree_bounds, "ext"),
             (quotient_codewords, quotient_degree_bounds, "quot"),
         ] {
-            println!("{}", identifier);
+            if std::env::var("DEBUG").is_ok() {
+                println!(" --- next up: {identifier} codewords");
+            }
             // TODO with the DEBUG CODE and enumerate removed, the iterators can be `into_par_iter`
             for (idx, (codeword, degree_bound)) in
                 codewords.into_iter().zip_eq(bounds.iter()).enumerate()
diff --git a/triton-vm/src/vm.rs b/triton-vm/src/vm.rs
index 94decee2a..23187576d 100644
--- a/triton-vm/src/vm.rs
+++ b/triton-vm/src/vm.rs
@@ -207,11 +207,11 @@ pub mod triton_vm_tests {
     use num_traits::{One, Zero};
     use rand::rngs::ThreadRng;
     use rand::{Rng, RngCore};
+    use triton_profiler::triton_profiler::TritonProfiler;
     use twenty_first::shared_math::mpolynomial::MPolynomial;
     use twenty_first::shared_math::other;
     use twenty_first::shared_math::other::roundup_npo2;
     use twenty_first::shared_math::rescue_prime_regular::{RescuePrimeRegular, NUM_ROUNDS};
-    use twenty_first::timing_reporter::TimingReporter;
 
     use crate::instruction::{sample_programs, AnInstruction};
     use crate::shared_tests::SourceCodeAndInput;
@@ -823,7 +823,7 @@ pub mod triton_vm_tests {
     }
 
     fn processor_table_constraints_evaluate_to_zero(all_programs: &[SourceCodeAndInput]) {
-        let mut timer = TimingReporter::start();
+        let mut profiler = TritonProfiler::new("Table Constraints Evaluate to Zero Test");
         for (code_idx, program) in all_programs.iter().enumerate() {
             let (aet, output, err) = program.simulate();
 
@@ -862,6 +862,8 @@ pub mod triton_vm_tests {
             let ext_processor_table =
                 processor_table.extend(&challenges.processor_table_challenges, interpolant_degree);
 
+            let program_idx_string = format!("Program number {code_idx:>2}");
+            profiler.start(&program_idx_string);
             for (row_idx, (current_row, next_row)) in ext_processor_table
                 .data()
                 .iter()
@@ -898,11 +900,13 @@ pub mod triton_vm_tests {
                     }
                 }
             }
-            timer.elapsed(
-                format!("Program number {code_idx:>2} (cycles: {num_cycles:>4})").as_str(),
-            );
+            let num_cycles_string = format!("took {num_cycles:>4} VM cycles");
+            profiler.start(&num_cycles_string);
+            profiler.stop(&num_cycles_string);
+            profiler.stop(&program_idx_string);
         }
-        println!("{}", timer.finish());
+        profiler.finish();
+        println!("{}", profiler.report());
     }
 
     fn _assert_air_constraints_on_matrix(