diff --git a/test/e2e/favicon-short-circuit/favicon-short-circuit.test.ts b/test/e2e/favicon-short-circuit/favicon-short-circuit.test.ts index 049e9d6f5c3dd8..f252b88d4890f4 100644 --- a/test/e2e/favicon-short-circuit/favicon-short-circuit.test.ts +++ b/test/e2e/favicon-short-circuit/favicon-short-circuit.test.ts @@ -28,9 +28,9 @@ describe('favicon-short-circuit', () => { expect(res.status).toBe(404) expect(res.headers.get('content-type')).toBe('text/html; charset=utf-8') - // Expect we got the right body. + // Expect we got HTML as text (with a non-empty body unlike in dev) and also not binary data const html = await res.text() - expect(html).toContain('') + expect(html).toContain(' !IGNORE_NAME.test(f)) - run2 = run2.filter(([f, _]) => !IGNORE_NAME.test(f)) - // First, compare file names let run1FileNames = run1.map(([f, _]) => f) let run2FileNames = run2.map(([f, _]) => f) diff --git a/turbopack/crates/turbo-tasks-auto-hash-map/src/map.rs b/turbopack/crates/turbo-tasks-auto-hash-map/src/map.rs index 92d599b459235a..8821cfcf93630d 100644 --- a/turbopack/crates/turbo-tasks-auto-hash-map/src/map.rs +++ b/turbopack/crates/turbo-tasks-auto-hash-map/src/map.rs @@ -271,7 +271,9 @@ impl AutoMap { if list.capacity() > list.len() * 3 { - list.shrink_to_fit(); + // You need to call "grow" to shrink a SmallVec to a specific capacity + // There is no Vec::shrink_to() equivalent for SmallVec + list.grow(list.len().next_power_of_two()); } } AutoMap::Map(map) => { @@ -280,6 +282,7 @@ impl AutoMap map.len() * 3 { + // HashMaps will always have a capacity that is a power of two map.shrink_to_fit(); } } diff --git a/turbopack/crates/turbo-tasks-backend/src/backend/mod.rs b/turbopack/crates/turbo-tasks-backend/src/backend/mod.rs index 6a83bbea783f82..ed44f3ef298a7e 100644 --- a/turbopack/crates/turbo-tasks-backend/src/backend/mod.rs +++ b/turbopack/crates/turbo-tasks-backend/src/backend/mod.rs @@ -2580,6 +2580,10 @@ impl TurboTasksBackendInner { task.shrink_to_fit(CachedDataItemType::CellDependency); task.shrink_to_fit(CachedDataItemType::OutputDependency); task.shrink_to_fit(CachedDataItemType::CollectiblesDependency); + task.shrink_to_fit(CachedDataItemType::OutdatedOutputDependency); + task.shrink_to_fit(CachedDataItemType::OutdatedCellDependency); + task.shrink_to_fit(CachedDataItemType::OutdatedCollectiblesDependency); + task.shrink_to_fit(CachedDataItemType::OutdatedCollectible); drop(task); } diff --git a/turbopack/crates/turbo-tasks/src/priority_runner.rs b/turbopack/crates/turbo-tasks/src/priority_runner.rs index 484a63c70fec0c..ce4eed37397cb5 100644 --- a/turbopack/crates/turbo-tasks/src/priority_runner.rs +++ b/turbopack/crates/turbo-tasks/src/priority_runner.rs @@ -176,9 +176,7 @@ impl< ) -> Option<(E::Future, Option>)> { let mut queue = self.queue.lock(); if let Some(heap_item) = queue.pop() { - if queue.len() * 2 + 16 < queue.capacity() { - queue.shrink_to_fit(); - } + shrink_amortized(&mut queue); drop(queue); let tx = heap_item.tx; Some(( @@ -198,9 +196,7 @@ impl< ) -> bool { let mut queue = self.queue.lock(); if let Some(heap_item) = queue.pop() { - if queue.len() * 2 + 16 < queue.capacity() { - queue.shrink_to_fit(); - } + shrink_amortized(&mut queue); drop(queue); let tx = heap_item.tx; let new_future = @@ -218,6 +214,15 @@ impl< } } +fn shrink_amortized(queue: &mut BinaryHeap>) { + // Amortized shrinking of the queue, but with a lower threshold to avoid + // frequent reallocations when the queue is small. + if queue.capacity() > queue.len() * 3 && queue.capacity() > 128 { + let new_capacity = queue.len().next_power_of_two().max(128); + queue.shrink_to(new_capacity); + } +} + #[derive(Debug)] enum WorkerState { UnfinishedFuture,