Unverified Commit db0ef2b2 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Reland "[turbopack] Fix some modification tracking bugs in TaskStorage" (#90098)

Reverts vercel/next.js#90096

* always `track_mutation()` before the mutation
* only `track_mutation()` if we are actually going to mutate

The previous attempt was reverted because we would call `track_modification` too often which lead to a test failure in the filesystem-cache-test because the DB grew too much.
parent 715103ad
...@@ -97,6 +97,17 @@ for (const cacheEnabled of [false, true]) { ...@@ -97,6 +97,17 @@ for (const cacheEnabled of [false, true]) {
) )
} }
// Ensure each test starts with a fresh server and no test leaks a
// running server into the next one.
beforeEach(async () => {
// stop() is a no-op if already stopped; start() rebuilds + starts.
await stop()
await start()
})
afterEach(async () => {
await stop()
})
// Very flakey with Webpack enabled // Very flakey with Webpack enabled
;(process.env.IS_TURBOPACK_TEST ? it : it.skip)( ;(process.env.IS_TURBOPACK_TEST ? it : it.skip)(
'should cache or not cache loaders', 'should cache or not cache loaders',
......
...@@ -393,13 +393,44 @@ impl TaskStorage { ...@@ -393,13 +393,44 @@ impl TaskStorage {
/// ///
/// The `extract` closure should return `Some(&mut T)` for the matching variant, /// The `extract` closure should return `Some(&mut T)` for the matching variant,
/// or `None` for non-matching variants. /// or `None` for non-matching variants.
pub fn find_lazy_mut<T>( fn find_lazy_mut<T>(
&mut self, &mut self,
extract: impl Fn(&mut LazyField) -> Option<&mut T>, extract: impl Fn(&mut LazyField) -> Option<&mut T>,
) -> Option<&mut T> { ) -> Option<&mut T> {
self.lazy.iter_mut().find_map(extract) self.lazy.iter_mut().find_map(extract)
} }
/// Find and extract a lazy field, returning its index and a reference to the inner value.
///
/// Combines index lookup and extraction into a single scan. The returned index
/// can be used with `lazy_at_mut` for subsequent mutation without re-scanning.
fn find_lazy_ref<T>(&self, extract: impl Fn(&LazyField) -> Option<&T>) -> Option<(usize, &T)> {
self.lazy
.iter()
.enumerate()
.find_map(|(idx, field)| extract(field).map(|val| (idx, val)))
}
/// Access a lazy field by index (mutable), extracting the inner value.
///
/// # Panics
/// Panics if `idx` is out of bounds or the extractor returns `None`.
fn lazy_at_mut<T>(
&mut self,
idx: usize,
extract: impl FnOnce(&mut LazyField) -> Option<&mut T>,
) -> &mut T {
extract(&mut self.lazy[idx]).unwrap()
}
/// Take a lazy field by known index, removing it from the Vec via swap_remove.
///
/// # Panics
/// Panics if `idx` is out of bounds.
fn lazy_take_at<T>(&mut self, idx: usize, extract: impl FnOnce(LazyField) -> T) -> T {
extract(self.lazy.swap_remove(idx))
}
/// Get or create a lazy field, returning a mutable reference. /// Get or create a lazy field, returning a mutable reference.
/// ///
/// Uses a single `extract` closure that serves as both the matcher (by returning Some/None) /// Uses a single `extract` closure that serves as both the matcher (by returning Some/None)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment