Skip to content

Commit

Permalink
perf(RAM): Clear unusable caches
Browse files Browse the repository at this point in the history
It is possible that the quotient domain codewords are cached for the
main table, but not for the auxiliary table. To compute the quotients,
either both or neither are needed.[^1] For peak memory consumption, it
is beneficial to clear any unused cache.

Throwing away any cache here incurs a performance penalty later, when
revealing the rows indicated by FRI. This is deemed an acceptable
tradeoff when peak memory usage is a larger issue than compute time.

[^1]: Code using exactly one cache could exist, but oh! the engineering.
  • Loading branch information
jan-ferdinand committed Oct 15, 2024
1 parent da36614 commit f1f7375
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
15 changes: 15 additions & 0 deletions triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ impl Stark {
profiler!(stop "Fiat-Shamir");
profiler!(stop "aux tables");

// It is possible that the quotient domain codewords are cached for the main
// table, but not for the auxiliary table. To compute the quotients, either both
// or neither are needed.[^1] For peak memory consumption, it is beneficial to
// clear any unused cache.
//
// Throwing away any cache here incurs a performance penalty later, when
// revealing the rows indicated by FRI. This is deemed an acceptable tradeoff
// when peak memory usage is a larger issue than compute time.
//
// [^1]: Code using exactly one cache _could_ exist, but oh! the engineering.
if master_main_table.cache_is_empty() || master_aux_table.cache_is_empty() {
master_main_table.clear_cache();
master_aux_table.clear_cache();
}

let (fri_domain_quotient_segment_codewords, quotient_segment_polynomials) =
Self::compute_quotient_segments(
&master_main_table,
Expand Down
19 changes: 11 additions & 8 deletions triton-vm/src/table/master_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,13 @@ where
low_degree_extended_columns: Array2<Self::Field>,
);

/// Return the cached low-degree-extended table, if any.
fn low_degree_extended_table(&self) -> Option<ArrayView2<Self::Field>>;
#[doc(hidden)]
fn cache_is_empty(&self) -> bool {
self.fri_domain_table().is_none()
}

#[doc(hidden)]
fn clear_cache(&mut self);

/// Return the FRI domain view of the cached low-degree-extended table, if any.
///
Expand Down Expand Up @@ -711,9 +716,8 @@ impl MasterTable for MasterMainTable {
self.low_degree_extended_table = Some(low_degree_extended_columns);
}

fn low_degree_extended_table(&self) -> Option<ArrayView2<BFieldElement>> {
let low_degree_extended_table = self.low_degree_extended_table.as_ref()?;
Some(low_degree_extended_table.view())
fn clear_cache(&mut self) {
drop(self.low_degree_extended_table.take());
}

fn fri_domain_table(&self) -> Option<ArrayView2<BFieldElement>> {
Expand Down Expand Up @@ -784,9 +788,8 @@ impl MasterTable for MasterAuxTable {
self.low_degree_extended_table = Some(low_degree_extended_columns);
}

fn low_degree_extended_table(&self) -> Option<ArrayView2<XFieldElement>> {
let low_degree_extended_table = self.low_degree_extended_table.as_ref()?;
Some(low_degree_extended_table.view())
fn clear_cache(&mut self) {
drop(self.low_degree_extended_table.take());
}

fn fri_domain_table(&self) -> Option<ArrayView2<XFieldElement>> {
Expand Down

0 comments on commit f1f7375

Please sign in to comment.