rspack_core 0.102.9

rspack core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use std::{collections::hash_map::Entry, hash::BuildHasherDefault};

use rspack_collections::IdentifierSet;
use rspack_paths::{InternedPath, InternedPathList, InternedPathMap};
use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::SmallVec;
use ustr::IdentityHasher;

use crate::{DependencyId, ModuleIdentifier, utils::incremental_info::IncrementalInfo};

/// Tracks the modules and the dependency lists that currently reference a path.
#[derive(Debug, Default)]
struct PathResourceIds {
  modules: IdentifierSet,
  groups: GroupRefs,
}

/// The dependency lists that mention one path.
#[derive(Debug)]
enum GroupRefs {
  Small(SmallVec<[InternedPathList; 1]>),
  /// Paths that belong to very many distinct lists - resolver candidates are
  /// probed from many different factorize results - switch to a set: removing
  /// a reference from a dense vector scans every entry, so dissolving one of
  /// those lists would cost one full scan per path it mentions and degrade
  /// into quadratic work, while a set removes in constant time.
  Hot(Box<FxHashSet<InternedPathList>>),
}

impl Default for GroupRefs {
  fn default() -> Self {
    Self::Small(SmallVec::new())
  }
}

impl GroupRefs {
  /// Above this many lists sharing a path, removing must not scan.
  const HOT_THRESHOLD: usize = 16;

  fn push(&mut self, list: InternedPathList) {
    match self {
      GroupRefs::Small(lists) => {
        lists.push(list);
        if lists.len() > Self::HOT_THRESHOLD {
          let lists = std::mem::take(lists);
          *self = GroupRefs::Hot(Box::new(lists.into_iter().collect()));
        }
      }
      GroupRefs::Hot(lists) => {
        lists.insert(list);
      }
    }
  }

  fn remove(&mut self, list: &InternedPathList) {
    match self {
      GroupRefs::Small(lists) => lists.retain(|item| item != list),
      GroupRefs::Hot(lists) => {
        lists.remove(list);
      }
    }
  }

  fn is_empty(&self) -> bool {
    match self {
      GroupRefs::Small(lists) => lists.is_empty(),
      GroupRefs::Hot(lists) => lists.is_empty(),
    }
  }

  fn iter(&self) -> GroupRefsIter<'_> {
    match self {
      GroupRefs::Small(lists) => GroupRefsIter::Small(lists.iter()),
      GroupRefs::Hot(lists) => GroupRefsIter::Hot(lists.iter()),
    }
  }
}

/// Borrowed iteration over a path's group references.
#[derive(Debug)]
enum GroupRefsIter<'a> {
  Small(std::slice::Iter<'a, InternedPathList>),
  Hot(std::collections::hash_set::Iter<'a, InternedPathList>),
}

impl<'a> Iterator for GroupRefsIter<'a> {
  type Item = &'a InternedPathList;

  #[inline]
  fn next(&mut self) -> Option<Self::Item> {
    match self {
      GroupRefsIter::Small(lists) => lists.next(),
      GroupRefsIter::Hot(lists) => lists.next(),
    }
  }
}

impl PathResourceIds {
  fn is_empty(&self) -> bool {
    self.modules.is_empty() && self.groups.is_empty()
  }
}

/// The dependencies recorded with one exact interned path list.
type DependencyGroups = FxHashMap<InternedPathList, FxHashSet<DependencyId>>;

/// The resources that reference one path.
pub struct RelatedResourceIds<'a> {
  entry: &'a PathResourceIds,
  groups: &'a DependencyGroups,
}

impl<'a> RelatedResourceIds<'a> {
  /// The modules that built with this path.
  pub fn modules(&self) -> &IdentifierSet {
    &self.entry.modules
  }

  /// Iterates the dependency ids whose factorize result lists this path.
  ///
  /// A dependency id is yielded once per list it was recorded with. Callers
  /// keep adding and removing a dependency balanced, so normally every id
  /// appears exactly once.
  pub fn dependencies(&self) -> DependencyIdsIter<'a> {
    DependencyIdsIter {
      refs: self.entry.groups.iter(),
      groups: self.groups,
      current: None,
    }
  }
}

/// Iterator over the dependency ids recorded for one path, across the lists
/// that mention it.
#[derive(Debug)]
pub struct DependencyIdsIter<'a> {
  refs: GroupRefsIter<'a>,
  groups: &'a DependencyGroups,
  current: Option<std::collections::hash_set::Iter<'a, DependencyId>>,
}

impl Iterator for DependencyIdsIter<'_> {
  type Item = DependencyId;

  #[inline]
  fn next(&mut self) -> Option<Self::Item> {
    loop {
      if let Some(ids) = self.current.as_mut() {
        if let Some(id) = ids.next() {
          return Some(*id);
        }
        self.current = None;
      }
      let list = self.refs.next()?;
      self.current = self.groups.get(list).map(|ids| ids.iter());
    }
  }
}

/// Used to count file usage and track which modules/dependencies use each file
#[derive(Debug, Default)]
pub struct FileCounter {
  inner: InternedPathMap<PathResourceIds>,
  /// The dependency ids grouped by the interned list that recorded them.
  ///
  /// A dependency registers its file list at every path it mentions, but the
  /// ids themselves only belong to the list. Keying them by the interned list
  /// (equal lists share one handle) stores equal content once instead of once
  /// per referenced path, and adding or removing a dependency touches a single
  /// set no matter how many paths its list mentions.
  groups: DependencyGroups,
  incremental_info: IncrementalInfo<InternedPath, BuildHasherDefault<IdentityHasher>>,
}

impl FileCounter {
  /// Adds the files a module built with.
  pub fn add_module_files<'a>(
    &mut self,
    module: ModuleIdentifier,
    paths: impl IntoIterator<Item = &'a InternedPath>,
  ) {
    for path in paths {
      let entry = self.inner.entry(path.clone()).or_default();
      if entry.is_empty() {
        self.incremental_info.mark_as_add(path);
      }
      entry.modules.insert(module);
    }
  }

  /// Removes the files a module built with.
  pub fn remove_module_files<'a>(
    &mut self,
    module: ModuleIdentifier,
    paths: impl IntoIterator<Item = &'a InternedPath>,
  ) {
    for path in paths {
      let Some(entry) = self.inner.get_mut(path) else {
        panic!("unable to remove untracked file {}", path.to_string_lossy());
      };
      if !entry.modules.remove(&module) {
        panic!(
          "unable to remove path '{}' with module '{}', it has not been added.",
          path.to_string_lossy(),
          module,
        )
      }
      if entry.is_empty() {
        self.incremental_info.mark_as_remove(path);
        self.inner.remove(path);
      }
    }
  }

  /// Adds the interned file list that a dependency factorized with.
  pub fn add_dependency_files(&mut self, dependency: DependencyId, list: &InternedPathList) {
    if list.is_empty() {
      return;
    }
    match self.groups.entry(list.clone()) {
      Entry::Occupied(mut group) => {
        group.get_mut().insert(dependency);
      }
      Entry::Vacant(group) => {
        for path in list.as_slice() {
          let entry = self.inner.entry(path.clone()).or_default();
          if entry.is_empty() {
            self.incremental_info.mark_as_add(path);
          }
          entry.groups.push(list.clone());
        }
        group.insert(std::iter::once(dependency).collect());
      }
    }
  }

  /// Removes the interned file list that a dependency factorized with.
  pub fn remove_dependency_files(&mut self, dependency: DependencyId, list: &InternedPathList) {
    if list.is_empty() {
      return;
    }
    let Some(group) = self.groups.get_mut(list) else {
      panic!("unable to remove dependency {dependency:?}, its list is not tracked.");
    };
    if !group.remove(&dependency) {
      panic!("unable to remove dependency {dependency:?}, it has not been added.");
    }
    if group.is_empty() {
      self.groups.remove(list);
      for path in list.as_slice() {
        let entry = self
          .inner
          .get_mut(path)
          .expect("a path of a tracked list should be tracked");
        entry.groups.remove(list);
        if entry.is_empty() {
          self.incremental_info.mark_as_remove(path);
          self.inner.remove(path);
        }
      }
    }
  }

  /// Get the file that has been used
  pub fn files(&self) -> impl Iterator<Item = &InternedPath> {
    self.inner.keys()
  }

  /// Get the resource ids (modules/dependencies) that use a specific file
  pub fn related_resource_ids(&self, path: &InternedPath) -> Option<RelatedResourceIds<'_>> {
    self.inner.get(path).map(|entry| RelatedResourceIds {
      entry,
      groups: &self.groups,
    })
  }

  /// reset incremental info
  pub fn reset_incremental_info(&mut self) {
    self.incremental_info.reset();
  }

  /// Added files compared to the `files()` when call reset_incremental_info
  pub fn added_files(&self) -> impl Iterator<Item = &InternedPath> {
    self.incremental_info.added().iter()
  }

  /// Updated files compared to the `files()` when call reset_incremental_info
  pub fn updated_files(&self) -> impl Iterator<Item = &InternedPath> {
    self.incremental_info.updated().iter()
  }

  /// Removed files compared to the `files()` when call reset_incremental_info
  pub fn removed_files(&self) -> impl Iterator<Item = &InternedPath> {
    self.incremental_info.removed().iter()
  }
}

#[cfg(test)]
mod test {
  use rspack_paths::{InternedPath, InternedPathList, InternedPathSet};

  use super::FileCounter;
  use crate::{DependencyId, ModuleIdentifier};

  #[test]
  fn file_counter_is_available() {
    let mut counter = FileCounter::default();
    let file_a = InternedPath::from(std::path::PathBuf::from("/a"));
    let file_b = InternedPath::from(std::path::PathBuf::from("/b"));
    let file_list_a = {
      let mut list = InternedPathSet::default();
      list.insert(file_a.clone());
      list
    };
    let file_list_b = {
      let mut list = InternedPathSet::default();
      list.insert(file_b.clone());
      list
    };
    let file_list_all = {
      let mut list = InternedPathSet::default();
      list.insert(file_a);
      list.insert(file_b);
      list
    };

    let module_1: ModuleIdentifier = "A".into();
    let module_2: ModuleIdentifier = "B".into();

    counter.add_module_files(module_1, &file_list_all);
    counter.add_module_files(module_2, &file_list_a);
    assert_eq!(counter.files().count(), 2);
    assert_eq!(counter.added_files().count(), 2);
    assert_eq!(counter.removed_files().count(), 0);

    // test repeated additions
    counter.add_module_files(module_1, &file_list_all);
    assert_eq!(counter.files().count(), 2);
    assert_eq!(counter.added_files().count(), 2);
    assert_eq!(counter.removed_files().count(), 0);

    counter.remove_module_files(module_1, &file_list_a);
    assert_eq!(counter.files().count(), 2);
    assert_eq!(counter.added_files().count(), 2);
    assert_eq!(counter.removed_files().count(), 0);

    counter.remove_module_files(module_1, &file_list_b);
    assert_eq!(counter.files().count(), 1);
    assert_eq!(counter.added_files().count(), 1);
    assert_eq!(counter.removed_files().count(), 0);

    counter.remove_module_files(module_2, &file_list_a);
    assert_eq!(counter.files().count(), 0);
    assert_eq!(counter.added_files().count(), 0);
    assert_eq!(counter.removed_files().count(), 0);
  }

  #[test]
  #[should_panic]
  fn file_counter_remove_file_with_panic() {
    let mut counter = FileCounter::default();
    let file_a = InternedPath::from(std::path::PathBuf::from("/a"));
    let file_list_a = {
      let mut list = InternedPathSet::default();
      list.insert(file_a);
      list
    };
    let module: ModuleIdentifier = "A".into();
    counter.remove_module_files(module, &file_list_a);
  }

  #[test]
  fn file_counter_reset_incremental_info() {
    let mut counter = FileCounter::default();
    let file_a = InternedPath::from(std::path::PathBuf::from("/a"));
    let file_list_a = {
      let mut list = InternedPathSet::default();
      list.insert(file_a);
      list
    };
    let module_1: ModuleIdentifier = "A".into();
    let module_2: ModuleIdentifier = "B".into();

    counter.add_module_files(module_1, &file_list_a);
    assert_eq!(counter.added_files().count(), 1);
    assert_eq!(counter.removed_files().count(), 0);

    counter.reset_incremental_info();
    assert_eq!(counter.added_files().count(), 0);
    assert_eq!(counter.removed_files().count(), 0);

    counter.remove_module_files(module_1, &file_list_a);
    assert_eq!(counter.added_files().count(), 0);
    assert_eq!(counter.removed_files().count(), 1);

    counter.add_module_files(module_1, &file_list_a);
    assert_eq!(counter.added_files().count(), 0);
    assert_eq!(counter.removed_files().count(), 0);

    counter.reset_incremental_info();
    assert_eq!(counter.added_files().count(), 0);
    assert_eq!(counter.removed_files().count(), 0);

    counter.add_module_files(module_2, &file_list_a);
    assert_eq!(counter.added_files().count(), 0);
    assert_eq!(counter.removed_files().count(), 0);

    counter.remove_module_files(module_1, &file_list_a);
    assert_eq!(counter.added_files().count(), 0);
    assert_eq!(counter.removed_files().count(), 0);

    counter.remove_module_files(module_2, &file_list_a);
    assert_eq!(counter.added_files().count(), 0);
    assert_eq!(counter.removed_files().count(), 1);
  }

  #[test]
  fn file_counter_tracks_modules_and_dependencies_separately() {
    let mut counter = FileCounter::default();
    let file = InternedPath::from(std::path::PathBuf::from("/a"));
    let dependency_list = InternedPathList::from_vec(vec![file.clone()]);
    let module: ModuleIdentifier = "A".into();
    let dependency = DependencyId::from(1);

    counter.add_module_files(module, [&file]);
    counter.add_dependency_files(dependency, &dependency_list);

    let ids = counter
      .related_resource_ids(&file)
      .expect("should track path resource ids");
    assert_eq!(ids.modules().len(), 1);
    assert_eq!(ids.dependencies().count(), 1);

    counter.remove_module_files(module, [&file]);
    let ids = counter
      .related_resource_ids(&file)
      .expect("should keep dependency after removing module");
    assert!(ids.modules().is_empty());
    assert_eq!(ids.dependencies().count(), 1);
  }
}