perf(coverage): faster source mapping (#21783)

I did not measure this change (O(n) to O(log n)), but mainly this should
be slightly more accurate at getting the line number.
This commit is contained in:
David Sherret 2024-01-04 08:49:17 -05:00 committed by GitHub
parent 7fc6f4902a
commit 0245ac08d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -220,20 +220,11 @@ fn generate_coverage_report(
continue;
}
let dest_line_index = text_lines.line_index(
text_lines
.byte_index_from_char_index(function.ranges[0].start_char_offset),
let line_index = range_to_src_line_index(
&function.ranges[0],
&text_lines,
&maybe_source_map,
);
let line_index = if let Some(source_map) = maybe_source_map.as_ref() {
source_map
.tokens()
.find(|token| token.get_dst_line() as usize == dest_line_index)
.map(|token| token.get_src_line() as usize)
.unwrap_or(0)
} else {
dest_line_index
};
coverage_report.named_functions.push(FunctionCoverageItem {
name: function.function_name.clone(),
line_index,
@ -244,18 +235,8 @@ fn generate_coverage_report(
for (block_number, function) in script_coverage.functions.iter().enumerate() {
let block_hits = function.ranges[0].count;
for (branch_number, range) in function.ranges[1..].iter().enumerate() {
let source_line_index = text_lines.line_index(
text_lines.byte_index_from_char_index(range.start_char_offset),
);
let line_index = if let Some(source_map) = maybe_source_map.as_ref() {
source_map
.tokens()
.find(|token| token.get_dst_line() as usize == source_line_index)
.map(|token| token.get_src_line() as usize)
.unwrap_or(0)
} else {
source_line_index
};
let line_index =
range_to_src_line_index(range, &text_lines, &maybe_source_map);
// From https://manpages.debian.org/unstable/lcov/geninfo.1.en.html:
//
@ -370,6 +351,24 @@ fn generate_coverage_report(
coverage_report
}
fn range_to_src_line_index(
range: &cdp::CoverageRange,
text_lines: &TextLines,
maybe_source_map: &Option<SourceMap>,
) -> usize {
let source_lc = text_lines.line_and_column_index(
text_lines.byte_index_from_char_index(range.start_char_offset),
);
if let Some(source_map) = maybe_source_map.as_ref() {
source_map
.lookup_token(source_lc.line_index as u32, source_lc.column_index as u32)
.map(|token| token.get_src_line() as usize)
.unwrap_or(0)
} else {
source_lc.line_index
}
}
fn collect_coverages(
files: FileFlags,
) -> Result<Vec<cdp::ScriptCoverage>, AnyError> {