Skip to content

perf: Avoid clone in adjust_mappings when possible #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
26 changes: 16 additions & 10 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -963,13 +963,7 @@ impl SourceMap {
}

/// 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(
tokens: &mut [RawToken],
key: fn(&RawToken) -> (u32, u32),
) -> Vec<Range<'_>> {
tokens.sort_unstable_by_key(key);

fn create_ranges(tokens: &[RawToken], key: fn(&RawToken) -> (u32, u32)) -> Vec<Range<'_>> {
let mut token_iter = tokens.iter().peekable();
let mut ranges = Vec::new();

@@ -993,10 +987,22 @@ 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.
fn key(t: &RawToken) -> (u32, u32) {
(t.dst_line, t.dst_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();
let adjustment_ranges = create_ranges(&mut adjustment_tokens, |t| (t.src_line, t.src_col));
self_tokens.sort_unstable_by_key(key);
let original_ranges = create_ranges(&self_tokens, key);

let adjustment_tokens = if adjustment.tokens.is_sorted_by_key(key) {
Cow::Borrowed(&adjustment.tokens)
} else {
let mut adjustment_tokens = adjustment.tokens.clone();
adjustment_tokens.sort_unstable_by_key(key);
Cow::Owned(adjustment_tokens)
};
let adjustment_ranges = create_ranges(&adjustment_tokens, |t| (t.src_line, t.src_col));
Copy link
Member

Choose a reason for hiding this comment

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

the adjustment_tokens are sorted by src, whereas the key fn defined above is sorted by dst, so this is a change in funcionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, thanks!


let mut original_ranges_iter = original_ranges.iter();