Skip to content

Commit

Permalink
Optimize RAM usage for large programs
Browse files Browse the repository at this point in the history
  • Loading branch information
georgwiese committed Jan 2, 2024
1 parent b705e66 commit f67134e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 27 deletions.
1 change: 1 addition & 0 deletions airgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ impl<'a, T: FieldElement> ASMPILConverter<'a, T> {
}

fn convert_machine_inner(mut self, ty: &AbsoluteSymbolPath) -> Object<T> {
// TODO: This clone doubles the current memory usage
let input = self.machines.get(ty).unwrap().clone();

let degree = input.degree.map(|s| T::from(s.degree).to_degree());
Expand Down
16 changes: 11 additions & 5 deletions asm_to_pil/src/vm_to_constrained.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,14 +841,20 @@ impl<T: FieldElement> ASMPILConverter<T> {
.collect::<Vec<_>>();
self.pil.extend(free_value_pil);
for (name, values) in rom_constants {
let array_expression = if values.iter().all(|v| v == &values[0]) {
// Performance optimization: The block below converts every T to an Expression<T>,
// which has a 7x larger memory footprint. This is wasteful for constant columns,
// of which there are a lot because this code has not been optimized yet.
ArrayExpression::RepeatedValue(vec![values[0].into()])
} else {
ArrayExpression::value(values.into_iter().map(Expression::from).collect())
.pad_with_last()
.unwrap_or_else(|| ArrayExpression::RepeatedValue(vec![T::zero().into()]))
};
self.pil.push(PilStatement::PolynomialConstantDefinition(
0,
name.clone(),
FunctionDefinition::Array(
ArrayExpression::value(values.into_iter().map(Expression::from).collect())
.pad_with_last()
.unwrap_or_else(|| ArrayExpression::RepeatedValue(vec![T::zero().into()])),
),
FunctionDefinition::Array(array_expression),
));
}
}
Expand Down
5 changes: 2 additions & 3 deletions ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ pub struct DiffMonitor {
impl DiffMonitor {
/// push a new program and log::trace! how it differs from the previous one, if any
pub fn push<S: ToString>(&mut self, s: S) {
std::mem::swap(&mut self.previous, &mut self.current);
self.current = Some(s.to_string());

if log_enabled!(log::Level::Trace) {
std::mem::swap(&mut self.previous, &mut self.current);
self.current = Some(s.to_string());
if let (Some(current), Some(previous)) = (&self.current, &self.previous) {
for diff in diff::lines(previous, current) {
match diff {
Expand Down
13 changes: 10 additions & 3 deletions compiler/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,18 +405,25 @@ impl<T: FieldElement> Pipeline<T> {
}
Artifact::Graph(graph) => {
self.log("Run linker");
self.log("Linker done");
let linked = linker::link(graph)?;
self.diff_monitor.push(&linked);
log::trace!("{linked}");
self.maybe_write_pil(&linked, "")?;
Artifact::Linked(linked)
}
Artifact::Linked(linked) => {
Artifact::AnaylyzedPil(pil_analyzer::analyze_string(&format!("{linked}")))
// TODO: We should probably offer a way to analyze a PILFile directly
self.log("Materialize linked file");
let linked = format!("{linked}");
self.log("Anylyzing pil...");
Artifact::AnaylyzedPil(pil_analyzer::analyze_string(&linked))
}
Artifact::PilFile(pil_file) => {
self.log("Anylyzing pil...");
Artifact::AnaylyzedPil(pil_analyzer::analyze(&pil_file))
}
Artifact::PilFile(pil_file) => Artifact::AnaylyzedPil(pil_analyzer::analyze(&pil_file)),
Artifact::PilString(pil_string) => {
self.log("Anylyzing pil...");
Artifact::AnaylyzedPil(pil_analyzer::analyze_string(&pil_string))
}
Artifact::AnaylyzedPil(analyzed_pil) => {
Expand Down
32 changes: 16 additions & 16 deletions linker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pol constant p_line = [0, 1, 2] + [2]*;
pol constant p_instr__jump_to_operation = [0, 1, 0] + [0]*;
pol constant p_instr__loop = [0, 0, 1] + [1]*;
pol constant p_instr__reset = [1, 0, 0] + [0]*;
pol constant p_instr_return = [0, 0, 0] + [0]*;
pol constant p_instr_return = [0]*;
{ pc, instr__jump_to_operation, instr__reset, instr__loop, instr_return } in { p_line, p_instr__jump_to_operation, p_instr__reset, p_instr__loop, p_instr_return };
pol constant _block_enforcer_last_step = [0]* + [1];
pol commit _operation_id_no_change;
Expand Down Expand Up @@ -283,22 +283,22 @@ pc' = ((1 - first_step') * pc_update);
pol constant p_line = [0, 1, 2, 3, 4] + [4]*;
pol commit X_free_value;
pol commit Y_free_value;
pol constant p_X_const = [0, 0, 0, 0, 0] + [0]*;
pol constant p_X_read_free = [0, 0, 0, 0, 0] + [0]*;
pol constant p_Y_const = [0, 0, 0, 0, 0] + [0]*;
pol constant p_X_const = [0]*;
pol constant p_X_read_free = [0]*;
pol constant p_Y_const = [0]*;
pol constant p_Y_read_free = [0, 0, 1, 0, 0] + [0]*;
pol constant p_instr__jump_to_operation = [0, 1, 0, 0, 0] + [0]*;
pol constant p_instr__loop = [0, 0, 0, 0, 1] + [1]*;
pol constant p_instr__reset = [1, 0, 0, 0, 0] + [0]*;
pol constant p_instr_identity = [0, 0, 0, 0, 0] + [0]*;
pol constant p_instr_nothing = [0, 0, 0, 0, 0] + [0]*;
pol constant p_instr_identity = [0]*;
pol constant p_instr_nothing = [0]*;
pol constant p_instr_one = [0, 0, 1, 0, 0] + [0]*;
pol constant p_instr_return = [0, 0, 0, 1, 0] + [0]*;
pol constant p_read_X_A = [0, 0, 0, 0, 0] + [0]*;
pol constant p_read_X_pc = [0, 0, 0, 0, 0] + [0]*;
pol constant p_read_Y_A = [0, 0, 0, 0, 0] + [0]*;
pol constant p_read_Y_pc = [0, 0, 0, 0, 0] + [0]*;
pol constant p_reg_write_X_A = [0, 0, 0, 0, 0] + [0]*;
pol constant p_read_X_A = [0]*;
pol constant p_read_X_pc = [0]*;
pol constant p_read_Y_A = [0]*;
pol constant p_read_Y_pc = [0]*;
pol constant p_reg_write_X_A = [0]*;
pol constant p_reg_write_Y_A = [0, 0, 1, 0, 0] + [0]*;
{ pc, reg_write_X_A, reg_write_Y_A, instr_identity, instr_one, instr_nothing, instr__jump_to_operation, instr__reset, instr__loop, instr_return, X_const, X_read_free, read_X_A, read_X_pc, Y_const, Y_read_free, read_Y_A, read_Y_pc } in { p_line, p_reg_write_X_A, p_reg_write_Y_A, p_instr_identity, p_instr_one, p_instr_nothing, p_instr__jump_to_operation, p_instr__reset, p_instr__loop, p_instr_return, p_X_const, p_X_read_free, p_read_X_A, p_read_X_pc, p_Y_const, p_Y_read_free, p_read_Y_A, p_read_Y_pc };
pol constant _block_enforcer_last_step = [0]* + [1];
Expand Down Expand Up @@ -331,13 +331,13 @@ pc' = ((1 - first_step') * pc_update);
pol constant p_line = [0, 1, 2, 3, 4, 5] + [5]*;
pol commit _output_0_free_value;
pol constant p__output_0_const = [0, 0, 0, 0, 1, 0] + [0]*;
pol constant p__output_0_read_free = [0, 0, 0, 0, 0, 0] + [0]*;
pol constant p__output_0_read_free = [0]*;
pol constant p_instr__jump_to_operation = [0, 1, 0, 0, 0, 0] + [0]*;
pol constant p_instr__loop = [0, 0, 0, 0, 0, 1] + [1]*;
pol constant p_instr__reset = [1, 0, 0, 0, 0, 0] + [0]*;
pol constant p_instr_return = [0, 0, 1, 1, 1, 0] + [0]*;
pol constant p_read__output_0__input_0 = [0, 0, 1, 0, 0, 0] + [0]*;
pol constant p_read__output_0_pc = [0, 0, 0, 0, 0, 0] + [0]*;
pol constant p_read__output_0_pc = [0]*;
{ pc, instr__jump_to_operation, instr__reset, instr__loop, instr_return, _output_0_const, _output_0_read_free, read__output_0_pc, read__output_0__input_0 } in { p_line, p_instr__jump_to_operation, p_instr__reset, p_instr__loop, p_instr_return, p__output_0_const, p__output_0_read_free, p_read__output_0_pc, p_read__output_0__input_0 };
pol constant _block_enforcer_last_step = [0]* + [1];
pol commit _operation_id_no_change;
Expand Down Expand Up @@ -396,7 +396,7 @@ pol pc_update = ((((((instr_jmpz * (instr_jmpz_pc_update + instr_jmpz_pc_update_
pc' = ((1 - first_step') * pc_update);
pol constant p_line = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + [10]*;
pol commit X_free_value(i) query match pc(i) { 2 => ("input", 1), 4 => ("input", (CNT(i) + 1)), 7 => ("input", 0), };
pol constant p_X_const = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + [0]*;
pol constant p_X_const = [0]*;
pol constant p_X_read_free = [0, 0, 1, 0, 1, 0, 0, -1, 0, 0, 0] + [0]*;
pol constant p_instr__jump_to_operation = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0] + [0]*;
pol constant p_instr__loop = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1] + [1]*;
Expand All @@ -410,7 +410,7 @@ pol constant p_instr_jmpz_param_l = [0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0] + [0]*;
pol constant p_instr_return = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0] + [0]*;
pol constant p_read_X_A = [0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0] + [0]*;
pol constant p_read_X_CNT = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0] + [0]*;
pol constant p_read_X_pc = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + [0]*;
pol constant p_read_X_pc = [0]*;
pol constant p_reg_write_X_A = [0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0] + [0]*;
pol constant p_reg_write_X_CNT = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0] + [0]*;
{ pc, reg_write_X_A, reg_write_X_CNT, instr_jmpz, instr_jmpz_param_l, instr_jmp, instr_jmp_param_l, instr_dec_CNT, instr_assert_zero, instr__jump_to_operation, instr__reset, instr__loop, instr_return, X_const, X_read_free, read_X_A, read_X_CNT, read_X_pc } in { p_line, p_reg_write_X_A, p_reg_write_X_CNT, p_instr_jmpz, p_instr_jmpz_param_l, p_instr_jmp, p_instr_jmp_param_l, p_instr_dec_CNT, p_instr_assert_zero, p_instr__jump_to_operation, p_instr__reset, p_instr__loop, p_instr_return, p_X_const, p_X_read_free, p_read_X_A, p_read_X_CNT, p_read_X_pc };
Expand Down Expand Up @@ -476,7 +476,7 @@ pol constant p_instr_adjust_fp_param_amount = [0, 0, 0, -2, 0] + [0]*;
pol constant p_instr_adjust_fp_param_t = [0, 0, 0, 3, 0] + [0]*;
pol constant p_instr_inc_fp = [0, 0, 1, 0, 0] + [0]*;
pol constant p_instr_inc_fp_param_amount = [0, 0, 7, 0, 0] + [0]*;
pol constant p_instr_return = [0, 0, 0, 0, 0] + [0]*;
pol constant p_instr_return = [0]*;
{ pc, instr_inc_fp, instr_inc_fp_param_amount, instr_adjust_fp, instr_adjust_fp_param_amount, instr_adjust_fp_param_t, instr__jump_to_operation, instr__reset, instr__loop, instr_return } in { p_line, p_instr_inc_fp, p_instr_inc_fp_param_amount, p_instr_adjust_fp, p_instr_adjust_fp_param_amount, p_instr_adjust_fp_param_t, p_instr__jump_to_operation, p_instr__reset, p_instr__loop, p_instr_return };
pol constant _block_enforcer_last_step = [0]* + [1];
pol commit _operation_id_no_change;
Expand Down

0 comments on commit f67134e

Please sign in to comment.