Skip to content

Commit 38bf093

Browse files
authored
perf: Make SourceMapIndex::flatten more efficient (#121)
1 parent d669e77 commit 38bf093

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

src/types.rs

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::sourceview::SourceView;
1515
use crate::utils::{find_common_prefix, greatest_lower_bound};
1616

1717
use debugid::DebugId;
18+
use rustc_hash::FxHashMap;
1819

1920
/// Controls the `SourceMap::rewrite` behavior
2021
///
@@ -1228,27 +1229,59 @@ impl SourceMapIndex {
12281229
}
12291230
};
12301231

1232+
let mut src_id_map = FxHashMap::<u32, u32>::default();
1233+
1234+
for (original_id, (source, contents)) in
1235+
map.sources().zip(map.source_contents()).enumerate()
1236+
{
1237+
let src_id = builder.add_source(source);
1238+
1239+
src_id_map.insert(original_id as u32, src_id);
1240+
1241+
if let Some(contents) = contents {
1242+
builder.set_source_contents(src_id, Some(contents));
1243+
}
1244+
}
1245+
1246+
let mut name_id_map = FxHashMap::<u32, u32>::default();
1247+
1248+
for (original_id, name) in map.names().enumerate() {
1249+
let name_id = builder.add_name(name);
1250+
name_id_map.insert(original_id as u32, name_id);
1251+
}
1252+
12311253
for token in map.tokens() {
12321254
let dst_col = if token.get_dst_line() == 0 {
12331255
token.get_dst_col() + off_col
12341256
} else {
12351257
token.get_dst_col()
12361258
};
1237-
let raw = builder.add(
1259+
1260+
// Use u32 -> u32 map instead of using the hash map in SourceMapBuilder for better performance
1261+
let original_src_id = token.raw.src_id;
1262+
let src_id = if original_src_id == !0 {
1263+
None
1264+
} else {
1265+
src_id_map.get(&original_src_id).copied()
1266+
};
1267+
1268+
let original_name_id = token.raw.name_id;
1269+
let name_id = if original_name_id == !0 {
1270+
None
1271+
} else {
1272+
name_id_map.get(&original_name_id).copied()
1273+
};
1274+
1275+
let raw = builder.add_raw(
12381276
token.get_dst_line() + off_line,
12391277
dst_col,
12401278
token.get_src_line(),
12411279
token.get_src_col(),
1242-
token.get_source(),
1243-
token.get_name(),
1280+
src_id,
1281+
name_id,
12441282
token.is_range(),
12451283
);
1246-
if token.get_source().is_some() && !builder.has_source_contents(raw.src_id) {
1247-
builder.set_source_contents(
1248-
raw.src_id,
1249-
map.get_source_contents(token.get_src_id()),
1250-
);
1251-
}
1284+
12521285
if map.ignore_list.contains(&token.get_src_id()) {
12531286
builder.add_to_ignore_list(raw.src_id);
12541287
}

0 commit comments

Comments
 (0)