Skip to content

Commit 555f117

Browse files
authored
Speed up comment presence checks (#799)
1 parent 766d123 commit 555f117

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

librubyfmt/src/file_comments.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::{BTreeSet, HashSet};
1+
use std::collections::BTreeSet;
22

33
use crate::comment_block::CommentBlock;
44
use crate::parser_state::line_difference_requires_newline;
@@ -66,7 +66,8 @@ pub struct FileComments {
6666
lines_with_ruby: BTreeSet<LineNumber>,
6767
last_lineno: LineNumber,
6868
line_index: LineIndex,
69-
comment_start_offsets: HashSet<usize>,
69+
/// Sorted list of byte offsets where comments start
70+
comment_start_offsets: Vec<usize>,
7071
}
7172

7273
impl FileComments {
@@ -80,7 +81,7 @@ impl FileComments {
8081
);
8182
file_comments
8283
.comment_start_offsets
83-
.insert(comment.location().start_offset());
84+
.push(comment.location().start_offset());
8485
}
8586

8687
// Lookup lines that have any Ruby
@@ -180,11 +181,13 @@ impl FileComments {
180181
start_offset: SourceOffset,
181182
end_offset: SourceOffset,
182183
) -> bool {
183-
let range = start_offset..end_offset;
184+
let idx = self
185+
.comment_start_offsets
186+
.partition_point(|&offset| offset < start_offset);
184187

185188
self.comment_start_offsets
186-
.iter()
187-
.any(|offset| range.contains(offset))
189+
.get(idx)
190+
.is_some_and(|&offset| offset < end_offset)
188191
}
189192

190193
pub fn extract_comments_to_line(

0 commit comments

Comments
 (0)