Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Turbopack] remove entries from versioned content map again #69913

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions crates/next-api/src/versioned_content_map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

use anyhow::{bail, Result};
use indexmap::IndexSet;
use next_core::emit_assets;
use serde::{Deserialize, Serialize};
use turbo_tasks::{
Expand Down Expand Up @@ -31,11 +32,12 @@ struct MapEntry {
#[turbo_tasks::value(transparent)]
struct OptionMapEntry(Option<MapEntry>);

type PathToOutputOperation = HashMap<Vc<FileSystemPath>, Vc<OutputAssets>>;
type PathToOutputOperation = HashMap<Vc<FileSystemPath>, IndexSet<Vc<OutputAssets>>>;
type OutputOperationToComputeEntry = HashMap<Vc<OutputAssets>, Vc<OptionMapEntry>>;

#[turbo_tasks::value]
pub struct VersionedContentMap {
// TODO: turn into a bi-directional multimap, OutputAssets -> IndexSet<FileSystemPath>
map_path_to_op: State<PathToOutputOperation>,
map_op_to_compute_entry: State<OutputOperationToComputeEntry>,
}
Expand Down Expand Up @@ -110,15 +112,31 @@ impl VersionedContentMap {
Ok(entries)
}
let entries = get_entries(assets).await.unwrap_or_default();

self.await?.map_path_to_op.update_conditionally(|map| {
let mut changed = false;
for &(k, _) in entries.iter() {
if map.insert(k, assets) != Some(assets) {
changed = true;
}

// get current map's keys, subtract keys that don't exist in operation
let mut stale_assets = map.keys().copied().collect::<HashSet<_>>();

for (k, _) in entries.iter() {
let res = map.entry(*k).or_default().insert(assets);
stale_assets.remove(k);
changed = changed || res;
}

// Make more efficient with reverse map
for k in &stale_assets {
let res = map
.get_mut(k)
// guaranteed
.unwrap()
.remove(&assets);
changed = changed || res
}
changed
});

// Make sure all written client assets are up-to-date
let side_effects = emit_assets(assets, node_root, client_relative_path, client_output_path);
let map_entry = Vc::cell(Some(MapEntry {
Expand Down Expand Up @@ -201,7 +219,7 @@ impl VersionedContentMap {
async fn raw_get(&self, path: Vc<FileSystemPath>) -> Result<Vc<OptionMapEntry>> {
let assets = {
let map = self.map_path_to_op.get();
map.get(&path).copied()
map.get(&path).and_then(|m| m.iter().last().copied())
};
let Some(assets) = assets else {
return Ok(Vc::cell(None));
Expand Down
Loading