@@ -34,6 +34,9 @@ pub struct MagicString<'s> {
34
34
chunk_by_start : FxHashMap < TextSize , ChunkIdx > ,
35
35
chunk_by_end : FxHashMap < TextSize , ChunkIdx > ,
36
36
guessed_indentor : OnceCell < String > ,
37
+
38
+ // This is used to speed up the search for the chunk that contains a given index.
39
+ last_searched_chunk_idx : ChunkIdx ,
37
40
}
38
41
39
42
impl < ' text > MagicString < ' text > {
@@ -62,6 +65,7 @@ impl<'text> MagicString<'text> {
62
65
// setup options
63
66
filename : options. filename ,
64
67
guessed_indentor : OnceCell :: default ( ) ,
68
+ last_searched_chunk_idx : initial_chunk_idx,
65
69
} ;
66
70
67
71
magic_string. chunk_by_start . insert ( 0 , initial_chunk_idx) ;
@@ -213,12 +217,13 @@ impl<'text> MagicString<'text> {
213
217
return ;
214
218
}
215
219
216
- let ( mut candidate, mut candidate_idx, search_right) =
217
- if ( self . source_len - at_index) > at_index {
218
- ( self . first_chunk ( ) , self . first_chunk_idx , true )
219
- } else {
220
- ( self . last_chunk ( ) , self . last_chunk_idx , false )
221
- } ;
220
+ let ( mut candidate, mut candidate_idx, search_right) = {
221
+ let last_searched_chunk = & self . chunks [ self . last_searched_chunk_idx ] ;
222
+ let search_right = at_index > last_searched_chunk. end ( ) ;
223
+
224
+ ( last_searched_chunk, self . last_searched_chunk_idx , search_right)
225
+ } ;
226
+
222
227
223
228
while !candidate. contains ( at_index) {
224
229
let next_idx = if search_right {
0 commit comments