Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,25 +956,26 @@ impl SourceMap {
// Helper struct that makes it easier to compare tokens by the start and end
// of the range they cover.
#[derive(Debug, Clone, Copy)]
struct Range {
struct Range<'a> {
start: (u32, u32),
end: (u32, u32),
value: RawToken,
value: &'a RawToken,
}

/// Turns a list of tokens into a list of ranges, using the provided `key` function to determine the order of the tokens.
#[allow(clippy::ptr_arg)]
fn create_ranges(
mut tokens: Vec<RawToken>,
tokens: &mut [RawToken],
key: fn(&RawToken) -> (u32, u32),
) -> Vec<Range> {
) -> Vec<Range<'_>> {
tokens.sort_unstable_by_key(key);

let mut token_iter = tokens.into_iter().peekable();
let mut token_iter = tokens.iter().peekable();
let mut ranges = Vec::new();

while let Some(t) = token_iter.next() {
let start = key(&t);
let next_start = token_iter.peek().map_or((u32::MAX, u32::MAX), key);
let start = key(t);
let next_start = token_iter.peek().map_or((u32::MAX, u32::MAX), |t| key(t));
// A token extends either to the start of the next token or the end of the line, whichever comes sooner
let end = std::cmp::min(next_start, (start.0, u32::MAX));
ranges.push(Range {
Expand All @@ -992,10 +993,10 @@ impl SourceMap {
// We want to compare `self` and `adjustment` tokens by line/column numbers in the "original source" file.
// These line/column numbers are the `dst_line/col` for
// the `self` tokens and `src_line/col` for the `adjustment` tokens.
let self_tokens = std::mem::take(&mut self.tokens);
let original_ranges = create_ranges(self_tokens, |t| (t.dst_line, t.dst_col));
let adjustment_ranges =
create_ranges(adjustment.tokens.clone(), |t| (t.src_line, t.src_col));
let mut self_tokens = std::mem::take(&mut self.tokens);
let original_ranges = create_ranges(&mut self_tokens, |t| (t.dst_line, t.dst_col));
let mut adjustment_tokens = adjustment.tokens.clone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could skip this clone in case the tokens are already sorted? Might be another optimization potentially.

let adjustment_ranges = create_ranges(&mut adjustment_tokens, |t| (t.src_line, t.src_col));

let mut original_ranges_iter = original_ranges.iter();

Expand Down Expand Up @@ -1032,7 +1033,7 @@ impl SourceMap {
let mut token = RawToken {
dst_line,
dst_col,
..original_range.value
..*original_range.value
};

token.dst_line = (token.dst_line as i32 + line_diff) as u32;
Expand Down