Skip to content

Commit

Permalink
refactor: optimize IndentStats storage and initialization for improve…
Browse files Browse the repository at this point in the history
…d performance
  • Loading branch information
gvozdvmozgu authored and benfdking committed Jan 10, 2025
1 parent 5971648 commit 429c55f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
13 changes: 6 additions & 7 deletions crates/lib/src/utils/reflow/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn get_consumed_whitespace(segment: Option<&ErasedSegment>) -> Option<String> {
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ReflowPointData {
segments: Vec<ErasedSegment>,
stats: IndentStats,
stats: OnceCell<IndentStats>,
class_types: OnceCell<SyntaxSet>,
}

Expand All @@ -54,12 +54,10 @@ impl Deref for ReflowPoint {

impl ReflowPoint {
pub fn new(segments: Vec<ErasedSegment>) -> Self {
let stats = Self::generate_indent_stats(&segments);

Self {
value: Rc::new(ReflowPointData {
segments,
stats,
stats: OnceCell::new(),
class_types: OnceCell::new(),
}),
}
Expand Down Expand Up @@ -100,7 +98,7 @@ impl ReflowPoint {
IndentStats {
impulse: running_sum,
trough,
implicit_indents,
implicit_indents: implicit_indents.into(),
}
}

Expand Down Expand Up @@ -550,7 +548,8 @@ impl ReflowPoint {
}

pub fn indent_impulse(&self) -> &IndentStats {
&self.stats
self.stats
.get_or_init(|| Self::generate_indent_stats(self.segments()))
}
}

Expand All @@ -574,7 +573,7 @@ fn indent_description(indent: &str) -> String {
pub struct IndentStats {
pub impulse: isize,
pub trough: isize,
pub implicit_indents: Vec<isize>,
pub implicit_indents: Rc<[isize]>,
}

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

if unclosed_bracket || !allow_implicit_indents {
indent_stats.implicit_indents = Vec::new();
indent_stats.implicit_indents = Default::default();
}
}

Expand Down

0 comments on commit 429c55f

Please sign in to comment.