Skip to content

Commit

Permalink
refactor(reflow): optimize code to reduce unnecessary cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
gvozdvmozgu authored and benfdking committed Jan 11, 2025
1 parent c0158c2 commit 1fc8f2f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
20 changes: 10 additions & 10 deletions crates/lib/src/utils/reflow/depth_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ impl StackPosition {
}
}

#[derive(Clone)]
pub struct DepthMap {
depth_info: AHashMap<u32, DepthInfo>,
}
Expand Down Expand Up @@ -130,11 +129,11 @@ impl DepthInfo {
}
}

pub fn trim(&self, amount: usize) -> DepthInfo {
pub fn trim(self, amount: usize) -> DepthInfo {
// Return a DepthInfo object with some amount trimmed.
if amount == 0 {
// The trivial case.
return self.clone();
return self;
}

let slice_set: IntSet<_> = IntSet::from_iter(
Expand All @@ -149,18 +148,19 @@ impl DepthInfo {
.copied()
.collect();

let stack_positions = self
.stack_positions
.into_iter()
.filter(|(hash, _)| new_hash_set.contains(hash))
.collect();

DepthInfo {
stack_depth: self.stack_depth - amount,
stack_hashes: self.stack_hashes[..self.stack_hashes.len() - amount].to_vec(),
stack_hash_set: new_hash_set.clone(),
stack_hash_set: new_hash_set,
stack_class_types: self.stack_class_types[..self.stack_class_types.len() - amount]
.to_vec(),
stack_positions: self
.stack_positions
.iter()
.filter(|(k, _)| new_hash_set.contains(k))
.map(|(k, v)| (*k, v.clone()))
.collect(),
stack_positions,
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/utils/reflow/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ impl IndentStats {
}
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq)]
pub struct ReflowBlockData {
segment: ErasedSegment,
spacing_before: Spacing,
Expand Down
6 changes: 3 additions & 3 deletions crates/lib/src/utils/reflow/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ impl<'a> ReflowSequence<'a> {
}

// https://github.com/sqlfluff/sqlfluff/blob/baceed9907908e055b79ca50ce6203bcd7949f39/src/sqlfluff/utils/reflow/sequence.py#L397
pub fn replace(&mut self, target: ErasedSegment, edit: &[ErasedSegment]) -> Self {
pub fn replace(mut self, target: ErasedSegment, edit: &[ErasedSegment]) -> Self {
let target_raws = target.get_raw_segments();

let mut edit_raws: Vec<ErasedSegment> = Vec::new();
Expand Down Expand Up @@ -372,9 +372,9 @@ impl<'a> ReflowSequence<'a> {

ReflowSequence {
elements: new_elements,
root_segment: self.root_segment.clone(),
root_segment: self.root_segment,
reflow_config: self.reflow_config,
depth_map: self.depth_map.clone(),
depth_map: self.depth_map,
lint_results: vec![LintResult::new(
target.clone().into(),
vec![LintFix::replace(target.clone(), edit.to_vec(), None)],
Expand Down

0 comments on commit 1fc8f2f

Please sign in to comment.