Skip to content

Commit

Permalink
misc: avoid explicit .(into_)iter for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ferdinand committed Feb 10, 2024
1 parent 3169422 commit 02018af
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 29 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ default_trait_access = "warn"
doc_link_with_quotes = "warn"
expl_impl_clone_on_copy = "warn"
explicit_deref_methods = "warn"
explicit_into_iter_loop = "warn"
explicit_iter_loop = "warn"
filter_map_next = "warn"
fn_params_excessive_bools = "warn"
from_iter_instead_of_collect = "warn"
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn ensure_no_missing_or_duplicate_labels<'a>(
// identify all and duplicate labels
let mut seen_labels: HashMap<&str, InstructionToken> = HashMap::default();
let mut duplicate_labels = HashSet::default();
for instruction in instructions.iter() {
for instruction in instructions {
if let InstructionToken::Label(label, _) = instruction {
if let Some(first_occurrence) = seen_labels.get(label.as_str()) {
duplicate_labels.insert(first_occurrence.to_owned());
Expand All @@ -141,7 +141,7 @@ fn identify_missing_labels<'a>(
seen_labels: HashMap<&str, InstructionToken>,
) -> HashSet<InstructionToken<'a>> {
let mut missing_labels = HashSet::default();
for instruction in instructions.iter() {
for instruction in instructions {
if let InstructionToken::Instruction(Call(label), _) = instruction {
if !seen_labels.contains_key(label.as_str()) {
missing_labels.insert(instruction.to_owned());
Expand Down
24 changes: 10 additions & 14 deletions triton-vm/src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ impl TritonProfiler {
// collect all categories and their total times
// todo: this can count the same category multiple times if it's nested
let mut category_times = HashMap::new();
for task in self.profile.iter() {
if let Some(category) = &task.category {
for task in &self.profile {
if let Some(ref category) = task.category {
category_times
.entry(category.to_string())
.or_insert(Duration::ZERO)
Expand Down Expand Up @@ -177,8 +177,8 @@ impl TritonProfiler {
}
}
let mut younger_max_weight: Weight = Weight::LikeNothing;
for sibling in younger_siblings.iter() {
younger_max_weight = max(younger_max_weight, report[*sibling].weight);
for &sibling in &younger_siblings {
younger_max_weight = max(younger_max_weight, report[sibling].weight);
}

report[task_index].younger_max_weight = younger_max_weight;
Expand Down Expand Up @@ -473,14 +473,14 @@ impl Display for Report {
"{title}{separation} {total_time_string} {share_string} {category_string}"
)?;

for task in self.tasks.iter() {
for ancestor_index in task.ancestors.iter() {
let mut spacer: ColoredString = if self.tasks[*ancestor_index].is_last_sibling {
for task in &self.tasks {
for &ancestor_index in &task.ancestors {
let mut spacer: ColoredString = if self.tasks[ancestor_index].is_last_sibling {
" ".normal()
} else {
"│ ".normal()
};
let uncle_weight = &self.tasks[*ancestor_index].younger_max_weight;
let uncle_weight = &self.tasks[ancestor_index].younger_max_weight;
spacer = spacer.color(uncle_weight.color());

write!(f, "{spacer}")?;
Expand All @@ -490,11 +490,7 @@ impl Display for Report {
} else {
"├".normal()
}
.color(
max(&task.weight, &task.younger_max_weight)
.to_owned()
.color(),
);
.color(max(task.weight, task.younger_max_weight).color());
let dash = "─".color(task.weight.color());
write!(f, "{tracer}{dash}")?;

Expand Down Expand Up @@ -543,7 +539,7 @@ impl Display for Report {
.collect::<Vec<_>>();
category_times_and_names_sorted_by_time.sort_by_key(|&(_, time)| time);
category_times_and_names_sorted_by_time.reverse();
for (category, category_time) in category_times_and_names_sorted_by_time.into_iter() {
for (category, category_time) in category_times_and_names_sorted_by_time {
let category_relative_time =
category_time.as_secs_f64() / self.total_time.as_secs_f64();
let category_color = Weight::weigh(category_relative_time).color();
Expand Down
6 changes: 3 additions & 3 deletions triton-vm/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl Program {
let mut label_map = HashMap::new();
let mut instruction_pointer = 0;

for labelled_instruction in program.iter() {
for labelled_instruction in program {
match labelled_instruction {
Label(label) => match label_map.entry(label.clone()) {
Entry::Occupied(_) => panic!("Duplicate label: {label}"),
Expand Down Expand Up @@ -555,7 +555,7 @@ impl VMProfiler {
}

fn stop_all_at_cycle(&mut self, cycle: u32) {
for &line_number in self.call_stack.iter() {
for &line_number in &self.call_stack {
self.profile[line_number].stop_at_cycle(cycle);
}
}
Expand Down Expand Up @@ -966,7 +966,7 @@ mod tests {
let (_, profile) = program.profile([].into(), [].into()).unwrap();

println!();
for line in profile.iter() {
for line in &profile {
println!("{line}");
}

Expand Down
12 changes: 6 additions & 6 deletions triton-vm/src/table/constraint_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ impl<II: InputIndicator> ConstraintCircuitMonad<II> {

// Of the remaining nodes, keep the ones occurring the most often.
let mut nodes_and_occurrences = HashMap::new();
for node in low_degree_nodes.iter() {
for node in &low_degree_nodes {
*nodes_and_occurrences.entry(node).or_insert(0) += 1;
}
let max_occurrences = nodes_and_occurrences
Expand Down Expand Up @@ -955,7 +955,7 @@ impl<II: InputIndicator> ConstraintCircuitMonad<II> {
multicircuit: &[ConstraintCircuit<II>],
) -> Vec<ConstraintCircuit<II>> {
let mut all_nodes = vec![];
for circuit in multicircuit.iter() {
for circuit in multicircuit {
let nodes_in_circuit = Self::all_nodes_in_circuit(circuit);
all_nodes.extend(nodes_in_circuit);
}
Expand Down Expand Up @@ -1072,7 +1072,7 @@ impl<II: InputIndicator> ConstraintCircuitBuilder<II> {

/// Substitute all nodes with ID `old_id` with the given `new` node.
pub fn substitute(&self, old_id: usize, new: Rc<RefCell<ConstraintCircuit<II>>>) {
for node in self.all_nodes.borrow().clone().into_iter() {
for node in self.all_nodes.borrow().clone() {
if node.circuit.borrow().id == old_id {
continue;
}
Expand Down Expand Up @@ -1477,7 +1477,7 @@ mod tests {
let ext_rows = ext_rows.view();

let mut values = HashMap::new();
for c in constraints.iter() {
for c in constraints {
evaluate_assert_unique(c, &challenges, base_rows, ext_rows, &mut values);
}

Expand Down Expand Up @@ -2146,11 +2146,11 @@ mod tests {
println!(" {circuit}");
}
println!("new base constraints:");
for constraint in new_base_constraints.iter() {
for constraint in &new_base_constraints {
println!(" {constraint}");
}
println!("new ext constraints:");
for constraint in new_ext_constraints.iter() {
for constraint in &new_ext_constraints {
println!(" {constraint}");
}

Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/table/op_stack_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ impl OpStackTable {

fn clock_jump_differences(op_stack_table: ArrayView2<BFieldElement>) -> Vec<BFieldElement> {
let mut clock_jump_differences = vec![];
for consecutive_rows in op_stack_table.axis_windows(Axis(0), 2).into_iter() {
for consecutive_rows in op_stack_table.axis_windows(Axis(0), 2) {
let current_row = consecutive_rows.row(0);
let next_row = consecutive_rows.row(1);
let current_stack_pointer = current_row[StackPointer.base_table_index()];
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/table/processor_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3451,11 +3451,11 @@ pub(crate) mod tests {
let next_row = rows.consecutive_master_base_table_rows.slice(s![1, ..]);

println!("Testing all constraints of {instruction} for test case {case_idx}…");
for &c in debug_info.debug_cols_curr_row.iter() {
for &c in &debug_info.debug_cols_curr_row {
print!("{c} = {}, ", curr_row[c.master_base_table_index()]);
}
println!();
for &c in debug_info.debug_cols_next_row.iter() {
for &c in &debug_info.debug_cols_next_row {
print!("{c}' = {}, ", next_row[c.master_base_table_index()]);
}
println!();
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/table/u32_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ impl ExtU32Table {
impl U32Table {
pub fn fill_trace(u32_table: &mut ArrayViewMut2<BFieldElement>, aet: &AlgebraicExecutionTrace) {
let mut next_section_start = 0;
for (&u32_table_entry, &multiplicity) in aet.u32_entries.iter() {
for (&u32_table_entry, &multiplicity) in &aet.u32_entries {
let mut first_row = Array2::zeros([1, BASE_WIDTH]);
first_row[[0, CopyFlag.base_table_index()]] = BFieldElement::one();
first_row[[0, Bits.base_table_index()]] = BFieldElement::zero();
Expand Down

0 comments on commit 02018af

Please sign in to comment.