From c0d2cb50debae7c758ceced61f40c105f746d30f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 14 May 2025 08:24:48 -0700 Subject: [PATCH 1/4] make flatten more efficient --- src/types.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/types.rs b/src/types.rs index db6fb16..2bd80b4 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1228,6 +1228,14 @@ impl SourceMapIndex { } }; + for (source, contents) in map.sources().zip(map.source_contents()) { + let src_id = builder.add_source(source); + + if let Some(contents) = contents { + builder.set_source_contents(src_id, Some(contents)); + } + } + for token in map.tokens() { let dst_col = if token.get_dst_line() == 0 { token.get_dst_col() + off_col @@ -1243,12 +1251,7 @@ impl SourceMapIndex { token.get_name(), token.is_range(), ); - if token.get_source().is_some() && !builder.has_source_contents(raw.src_id) { - builder.set_source_contents( - raw.src_id, - map.get_source_contents(token.get_src_id()), - ); - } + if map.ignore_list.contains(&token.get_src_id()) { builder.add_to_ignore_list(raw.src_id); } From 22b421709795836b8128bb4d6a17116da152d93c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 14 May 2025 09:22:09 -0700 Subject: [PATCH 2/4] expand --- src/types.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/types.rs b/src/types.rs index 2bd80b4..61d5b82 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1242,13 +1242,17 @@ impl SourceMapIndex { } else { token.get_dst_col() }; - let raw = builder.add( + + let src_id = token.get_source().map(|source| builder.add_source(source)); + let name_id = token.get_name().map(|name| builder.add_name(name)); + + let raw = builder.add_raw( token.get_dst_line() + off_line, dst_col, token.get_src_line(), token.get_src_col(), - token.get_source(), - token.get_name(), + src_id, + name_id, token.is_range(), ); From 5f8736337758a9bcd79eb572f619245620a60af1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 14 May 2025 09:28:13 -0700 Subject: [PATCH 3/4] cached map --- src/types.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/types.rs b/src/types.rs index 61d5b82..699f150 100644 --- a/src/types.rs +++ b/src/types.rs @@ -15,6 +15,7 @@ use crate::sourceview::SourceView; use crate::utils::{find_common_prefix, greatest_lower_bound}; use debugid::DebugId; +use rustc_hash::FxHashMap; /// Controls the `SourceMap::rewrite` behavior /// @@ -1236,6 +1237,9 @@ impl SourceMapIndex { } } + let mut src_id_map = FxHashMap::::default(); + let mut name_id_map = FxHashMap::::default(); + for token in map.tokens() { let dst_col = if token.get_dst_line() == 0 { token.get_dst_col() + off_col @@ -1243,8 +1247,27 @@ impl SourceMapIndex { token.get_dst_col() }; - let src_id = token.get_source().map(|source| builder.add_source(source)); - let name_id = token.get_name().map(|name| builder.add_name(name)); + // Use u32 -> u32 map instead of using the hash map in SourceMapBuilder for better performance + let original_src_id = token.raw.src_id; + let src_id = if original_src_id == !0 { + None + } else { + Some(*src_id_map.entry(original_src_id).or_insert_with(|| { + token + .get_source() + .map(|source| builder.add_source(source)) + .unwrap() + })) + }; + + let original_name_id = token.raw.name_id; + let name_id = if original_name_id == !0 { + None + } else { + Some(*name_id_map.entry(original_name_id).or_insert_with(|| { + token.get_name().map(|name| builder.add_name(name)).unwrap() + })) + }; let raw = builder.add_raw( token.get_dst_line() + off_line, From eaca8fc85e1e7661553d568a6921af7b1d2fd86c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 15 May 2025 10:39:37 -0700 Subject: [PATCH 4/4] Precompute source id and name id map --- src/types.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/types.rs b/src/types.rs index 699f150..c57c211 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1229,17 +1229,27 @@ impl SourceMapIndex { } }; - for (source, contents) in map.sources().zip(map.source_contents()) { + let mut src_id_map = FxHashMap::::default(); + + for (original_id, (source, contents)) in + map.sources().zip(map.source_contents()).enumerate() + { let src_id = builder.add_source(source); + src_id_map.insert(original_id as u32, src_id); + if let Some(contents) = contents { builder.set_source_contents(src_id, Some(contents)); } } - let mut src_id_map = FxHashMap::::default(); let mut name_id_map = FxHashMap::::default(); + for (original_id, name) in map.names().enumerate() { + let name_id = builder.add_name(name); + name_id_map.insert(original_id as u32, name_id); + } + for token in map.tokens() { let dst_col = if token.get_dst_line() == 0 { token.get_dst_col() + off_col @@ -1252,21 +1262,14 @@ impl SourceMapIndex { let src_id = if original_src_id == !0 { None } else { - Some(*src_id_map.entry(original_src_id).or_insert_with(|| { - token - .get_source() - .map(|source| builder.add_source(source)) - .unwrap() - })) + src_id_map.get(&original_src_id).copied() }; let original_name_id = token.raw.name_id; let name_id = if original_name_id == !0 { None } else { - Some(*name_id_map.entry(original_name_id).or_insert_with(|| { - token.get_name().map(|name| builder.add_name(name)).unwrap() - })) + name_id_map.get(&original_name_id).copied() }; let raw = builder.add_raw(