Skip to content

Commit

Permalink
refactor(turbopack): Use ResolvedVc for chunk graph related types
Browse files Browse the repository at this point in the history
  • Loading branch information
bgw committed Jan 6, 2025
1 parent fca7358 commit bd01374
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 123 deletions.
18 changes: 12 additions & 6 deletions crates/next-core/src/next_manifests/client_reference_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ impl ClientReferenceManifest {

let client_chunk_item = ecmascript_client_reference
.client_module
.as_chunk_item(Vc::upcast(client_chunking_context));
.as_chunk_item(Vc::upcast(client_chunking_context))
.to_resolved()
.await?;

let client_module_id = client_chunk_item.id().await?;

Expand Down Expand Up @@ -116,16 +118,20 @@ impl ClientReferenceManifest {
if let Some(ssr_chunking_context) = ssr_chunking_context {
let ssr_chunk_item = ecmascript_client_reference
.ssr_module
.as_chunk_item(Vc::upcast(ssr_chunking_context));
.as_chunk_item(Vc::upcast(ssr_chunking_context))
.to_resolved()
.await?;
let ssr_module_id = ssr_chunk_item.id().await?;

let rsc_chunk_item: Vc<Box<dyn ChunkItem>> =
let rsc_chunk_item: ResolvedVc<Box<dyn ChunkItem>> =
ResolvedVc::try_downcast_type::<EcmascriptClientReferenceProxyModule>(
parent_module,
)
.await?
.unwrap()
.as_chunk_item(Vc::upcast(ssr_chunking_context));
.as_chunk_item(Vc::upcast(ssr_chunking_context))
.to_resolved()
.await?;
let rsc_module_id = rsc_chunk_item.id().await?;

let (ssr_chunks_paths, ssr_is_async) = if runtime == NextRuntime::Edge {
Expand Down Expand Up @@ -362,13 +368,13 @@ pub fn get_client_reference_module_key(server_path: &str, export_name: &str) ->

async fn is_item_async(
availability_info: &AvailabilityInfo,
chunk_item: Vc<Box<dyn ChunkItem>>,
chunk_item: ResolvedVc<Box<dyn ChunkItem>>,
) -> Result<bool> {
let Some(available_chunk_items) = availability_info.available_chunk_items() else {
return Ok(false);
};

let Some(info) = &*available_chunk_items.get(chunk_item).await? else {
let Some(info) = available_chunk_items.await?.get(chunk_item).await? else {
return Ok(false);
};

Expand Down
8 changes: 4 additions & 4 deletions turbopack/crates/turbopack-browser/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ impl ChunkingContext for BrowserChunkingContext {

#[turbo_tasks::function]
async fn chunk_group(
self: Vc<Self>,
self: ResolvedVc<Self>,
ident: Vc<AssetIdent>,
module: ResolvedVc<Box<dyn ChunkableModule>>,
availability_info: Value<AvailabilityInfo>,
Expand All @@ -403,7 +403,7 @@ impl ChunkingContext for BrowserChunkingContext {
chunks,
availability_info,
} = make_chunk_group(
Vc::upcast(self),
ResolvedVc::upcast(self),
[ResolvedVc::upcast(module)],
input_availability_info,
)
Expand Down Expand Up @@ -454,7 +454,7 @@ impl ChunkingContext for BrowserChunkingContext {

#[turbo_tasks::function]
async fn evaluated_chunk_group(
self: Vc<Self>,
self: ResolvedVc<Self>,
ident: Vc<AssetIdent>,
evaluatable_assets: Vc<EvaluatableAssets>,
availability_info: Value<AvailabilityInfo>,
Expand All @@ -476,7 +476,7 @@ impl ChunkingContext for BrowserChunkingContext {
let MakeChunkGroupResult {
chunks,
availability_info,
} = make_chunk_group(Vc::upcast(self), entries, availability_info).await?;
} = make_chunk_group(ResolvedVc::upcast(self), entries, availability_info).await?;

let mut assets: Vec<ResolvedVc<Box<dyn OutputAsset>>> = chunks
.iter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ impl EcmascriptDevChunkContentEntries {
async move {
Ok((
chunk_item.id().await?,
EcmascriptDevChunkContentEntry::new(chunk_item, async_module_info).await?,
EcmascriptDevChunkContentEntry::new(
*chunk_item,
async_module_info.map(|info| *info),
)
.await?,
))
}
.instrument(info_span!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ pub enum AvailabilityInfo {
}

impl AvailabilityInfo {
pub fn available_chunk_items(&self) -> Option<Vc<AvailableChunkItems>> {
pub fn available_chunk_items(&self) -> Option<ResolvedVc<AvailableChunkItems>> {
match self {
Self::Untracked => None,
Self::Root => None,
Self::Complete {
available_chunk_items,
..
} => Some(**available_chunk_items),
} => Some(*available_chunk_items),
}
}

Expand Down
24 changes: 14 additions & 10 deletions turbopack/crates/turbopack-core/src/chunk/available_chunk_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ pub struct AvailableChunkItemInfo {
pub is_async: bool,
}

#[turbo_tasks::value(transparent)]
pub struct OptionAvailableChunkItemInfo(Option<AvailableChunkItemInfo>);

#[turbo_tasks::value(transparent)]
pub struct AvailableChunkItemInfoMap(
FxIndexMap<ResolvedVc<Box<dyn ChunkItem>>, AvailableChunkItemInfo>,
Expand Down Expand Up @@ -48,12 +45,13 @@ impl AvailableChunkItems {
self: ResolvedVc<Self>,
chunk_items: ResolvedVc<AvailableChunkItemInfoMap>,
) -> Result<Vc<Self>> {
let this = &*self.await?;
let chunk_items = chunk_items
.await?
.into_iter()
.map(|(&chunk_item, &info)| async move {
Ok(self
.get(*chunk_item)
Ok(this
.get(chunk_item)
.await?
.is_none()
.then_some((chunk_item, info)))
Expand Down Expand Up @@ -87,18 +85,24 @@ impl AvailableChunkItems {
}
Ok(Vc::cell(hasher.finish()))
}
}

#[turbo_tasks::function]
impl AvailableChunkItems {
// This is not a turbo-tasks function because:
//
// - We want to enforce that the `chunk_item` key is a `ResolvedVc`, so that it will actually
// match the entries in the `HashMap`.
// - This is a cheap operation that's unlikely to be worth caching.
pub async fn get(
&self,
chunk_item: ResolvedVc<Box<dyn ChunkItem>>,
) -> Result<Vc<OptionAvailableChunkItemInfo>> {
) -> Result<Option<AvailableChunkItemInfo>> {
if let Some(&info) = self.chunk_items.await?.get(&chunk_item) {
return Ok(Vc::cell(Some(info)));
return Ok(Some(info));
};
if let Some(parent) = self.parent {
return Ok(parent.get(*chunk_item));
return Box::pin(parent.await?.get(chunk_item)).await;
}
Ok(Vc::cell(None))
Ok(None)
}
}
34 changes: 20 additions & 14 deletions turbopack/crates/turbopack-core/src/chunk/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use std::collections::HashSet;

use anyhow::Result;
use auto_hash_map::AutoSet;
use turbo_tasks::{
FxIndexMap, FxIndexSet, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Value, Vc,
};
use turbo_tasks::{FxIndexMap, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Value, Vc};

use super::{
availability_info::AvailabilityInfo, available_chunk_items::AvailableChunkItemInfo,
Expand All @@ -22,7 +20,7 @@ pub struct MakeChunkGroupResult {

/// Creates a chunk group from a set of entries.
pub async fn make_chunk_group(
chunking_context: Vc<Box<dyn ChunkingContext>>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
chunk_group_entries: impl IntoIterator<Item = ResolvedVc<Box<dyn Module>>>,
availability_info: AvailabilityInfo,
) -> Result<MakeChunkGroupResult> {
Expand Down Expand Up @@ -53,7 +51,7 @@ pub async fn make_chunk_group(
.keys()
.copied()
.chain(self_async_children.into_iter())
.map(|chunk_item| (chunk_item, AutoSet::<Vc<Box<dyn ChunkItem>>>::new()))
.map(|chunk_item| (chunk_item, AutoSet::<ResolvedVc<Box<dyn ChunkItem>>>::new()))
.collect::<FxIndexMap<_, _>>();

// Propagate async inheritance
Expand Down Expand Up @@ -86,7 +84,7 @@ pub async fn make_chunk_group(
let mut chunk_items = chunk_items
.into_iter()
.map(|chunk_item| (chunk_item, None))
.collect::<FxIndexMap<_, Option<Vc<AsyncModuleInfo>>>>();
.collect::<FxIndexMap<_, Option<ResolvedVc<AsyncModuleInfo>>>>();

// Insert AsyncModuleInfo for every async module
for (async_item, referenced_async_modules) in async_chunk_items {
Expand All @@ -96,13 +94,18 @@ pub async fn make_chunk_group(
.iter()
.copied()
.filter(|item| referenced_async_modules.contains(item))
.map(|item| *item)
.collect()
} else {
Default::default()
};
chunk_items.insert(
async_item,
Some(AsyncModuleInfo::new(referenced_async_modules)),
Some(
AsyncModuleInfo::new(referenced_async_modules)
.to_resolved()
.await?,
),
);
}

Expand All @@ -112,7 +115,7 @@ pub async fn make_chunk_group(
.iter()
.map(|(&chunk_item, async_info)| async move {
Ok((
chunk_item.to_resolved().await?,
chunk_item,
AvailableChunkItemInfo {
is_async: async_info.is_some(),
},
Expand All @@ -130,9 +133,12 @@ pub async fn make_chunk_group(
let async_loaders = async_modules
.into_iter()
.map(|module| {
chunking_context.async_loader_chunk_item(*module, Value::new(availability_info))
chunking_context
.async_loader_chunk_item(*module, Value::new(availability_info))
.to_resolved()
})
.collect::<Vec<_>>();
.try_join()
.await?;
let has_async_loaders = !async_loaders.is_empty();
let async_loader_chunk_items = async_loaders.iter().map(|&chunk_item| (chunk_item, None));

Expand All @@ -151,7 +157,7 @@ pub async fn make_chunk_group(

let mut referenced_output_assets = (*external_output_assets.await?).clone();
referenced_output_assets.extend(
references_to_output_assets(external_module_references)
references_to_output_assets(&external_module_references)
.await?
.await?
.iter()
Expand All @@ -175,7 +181,7 @@ pub async fn make_chunk_group(

// Pass chunk items to chunking algorithm
let mut chunks = make_chunks(
chunking_context,
*chunking_context,
Vc::cell(chunk_items.into_iter().collect()),
"".into(),
Vc::cell(referenced_output_assets),
Expand All @@ -188,7 +194,7 @@ pub async fn make_chunk_group(
// We want them to be separate since they are specific to this chunk group due
// to available chunk items differing
let async_loader_chunks = make_chunks(
chunking_context,
*chunking_context,
Vc::cell(async_loader_chunk_items.into_iter().collect()),
"async-loader-".into(),
async_loader_external_module_references,
Expand All @@ -206,7 +212,7 @@ pub async fn make_chunk_group(
}

pub async fn references_to_output_assets(
references: FxIndexSet<Vc<Box<dyn ModuleReference>>>,
references: impl IntoIterator<Item = &ResolvedVc<Box<dyn ModuleReference>>>,
) -> Result<Vc<OutputAssets>> {
let output_assets = references
.into_iter()
Expand Down
8 changes: 5 additions & 3 deletions turbopack/crates/turbopack-core/src/chunk/chunking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ pub async fn make_chunks(
.await?
.iter()
.map(|&(chunk_item, async_info)| async move {
let chunk_item_info = chunk_item_info(chunking_context, chunk_item, async_info).await?;
let chunk_item_info =
chunk_item_info(chunking_context, *chunk_item, async_info.map(|info| *info))
.await?;
Ok((chunk_item, async_info, chunk_item_info))
})
.try_join()
Expand Down Expand Up @@ -118,8 +120,8 @@ pub async fn make_chunks(
}

type ChunkItemWithInfo = (
Vc<Box<dyn ChunkItem>>,
Option<Vc<AsyncModuleInfo>>,
ResolvedVc<Box<dyn ChunkItem>>,
Option<ResolvedVc<AsyncModuleInfo>>,
usize,
ReadRef<RcStr>,
);
Expand Down
Loading

0 comments on commit bd01374

Please sign in to comment.