Skip to content

Commit 801a1a2

Browse files
author
Lichu Acuña
committed
Implemented module ID global optimization using GlobalInformation.
1 parent 544f700 commit 801a1a2

File tree

5 files changed

+34
-31
lines changed

5 files changed

+34
-31
lines changed

crates/turbopack-browser/src/chunking_context.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::{bail, Context, Result};
22
use tracing::Instrument;
3-
use turbo_tasks::{debug::ValueDebug, RcStr, Value, ValueToString, Vc};
3+
use turbo_tasks::{RcStr, Value, ValueToString, Vc};
44
use turbo_tasks_fs::FileSystemPath;
55
use turbopack_core::{
66
chunk::{
@@ -243,16 +243,6 @@ impl BrowserChunkingContext {
243243

244244
#[turbo_tasks::value_impl]
245245
impl ChunkingContext for BrowserChunkingContext {
246-
#[turbo_tasks::function]
247-
async fn chunk_item_id_from_ident(
248-
self: Vc<Self>,
249-
ident: Vc<AssetIdent>,
250-
) -> Result<Vc<ModuleId>> {
251-
let this = self.await?;
252-
dbg!(this.global_information.dbg().await?);
253-
Ok(ModuleId::String(ident.to_string().await?.clone_value()).cell())
254-
}
255-
256246
#[turbo_tasks::function]
257247
fn name(&self) -> Vc<RcStr> {
258248
if let Some(name) = &self.name {
@@ -524,4 +514,9 @@ impl ChunkingContext for BrowserChunkingContext {
524514
self.chunk_item_id_from_ident(AsyncLoaderModule::asset_ident_for(module))
525515
})
526516
}
517+
518+
#[turbo_tasks::function]
519+
async fn global_information(self: Vc<Self>) -> Result<Vc<OptionGlobalInformation>> {
520+
Ok(self.await?.global_information)
521+
}
527522
}

crates/turbopack-core/src/changed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async fn get_referenced_output_assets(
1717
Ok(parent.references().await?.clone_value().into_iter())
1818
}
1919

20-
async fn get_referenced_modules(
20+
pub async fn get_referenced_modules(
2121
parent: Vc<Box<dyn Module>>,
2222
) -> Result<impl Iterator<Item = Vc<Box<dyn Module>>> + Send> {
2323
Ok(primary_referenced_modules(parent)

crates/turbopack-core/src/chunk/chunking_context.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use turbo_tasks::{trace::TraceRawVcs, RcStr, TaskInput, Upcast, Value, ValueToSt
44
use turbo_tasks_fs::FileSystemPath;
55
use turbo_tasks_hash::DeterministicHash;
66

7-
use super::{availability_info::AvailabilityInfo, ChunkableModule, EvaluatableAssets};
7+
use super::{
8+
availability_info::AvailabilityInfo, global_information::OptionGlobalInformation,
9+
ChunkableModule, EvaluatableAssets,
10+
};
811
use crate::{
912
chunk::{ChunkItem, ModuleId},
1013
environment::Environment,
@@ -114,10 +117,15 @@ pub trait ChunkingContext {
114117
availability_info: Value<AvailabilityInfo>,
115118
) -> Result<Vc<EntryChunkGroupResult>>;
116119

120+
fn global_information(self: Vc<Self>) -> Vc<OptionGlobalInformation>;
121+
117122
async fn chunk_item_id_from_ident(
118123
self: Vc<Self>,
119124
ident: Vc<AssetIdent>,
120125
) -> Result<Vc<ModuleId>> {
126+
if let Some(global_information) = &*self.global_information().await? {
127+
return Ok(global_information.get_module_id(ident).await?);
128+
}
121129
Ok(ModuleId::String(ident.to_string().await?.clone_value()).cell())
122130
}
123131

crates/turbopack-core/src/chunk/global_information.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
use std::collections::HashMap;
22

3-
use turbo_tasks::{RcStr, Vc};
3+
use anyhow::Result;
4+
use turbo_tasks::{ValueToString, Vc};
45

56
use super::ModuleId;
67
use crate::ident::AssetIdent;
78

89
#[turbo_tasks::value]
910
#[derive(Clone, Debug)]
1011
pub struct GlobalInformation {
11-
pub test_str: Vc<RcStr>,
1212
pub module_id_map: HashMap<AssetIdent, ModuleId>,
1313
}
1414

1515
impl GlobalInformation {
16-
pub fn get_module_id(&self, asset_ident: &AssetIdent) -> ModuleId {
17-
self.module_id_map.get(asset_ident).cloned().expect(
18-
"No module ID found for the given asset identifier. This is an internal Turbopack \
19-
error. Please report it.",
20-
)
16+
pub async fn get_module_id(&self, asset_ident: Vc<AssetIdent>) -> Result<Vc<ModuleId>> {
17+
let ident_str = asset_ident.to_string().await?;
18+
let ident = asset_ident.await?;
19+
let hashed_module_id = self.module_id_map.get(&ident);
20+
if let Some(hashed_module_id) = hashed_module_id {
21+
dbg!("Hashed module ID found", &ident_str, hashed_module_id);
22+
return Ok(hashed_module_id.clone().cell());
23+
}
24+
dbg!("Hashed module ID not found", &ident_str);
25+
return Ok(ModuleId::String(ident_str.clone_value()).cell());
2126
}
2227
}
2328

crates/turbopack-nodejs/src/chunking_context.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::iter::once;
22

33
use anyhow::{bail, Context, Result};
44
use tracing::Instrument;
5-
use turbo_tasks::{debug::ValueDebug, RcStr, Value, ValueToString, Vc};
5+
use turbo_tasks::{RcStr, Value, ValueToString, Vc};
66
use turbo_tasks_fs::FileSystemPath;
77
use turbopack_core::{
88
chunk::{
@@ -136,16 +136,6 @@ impl NodeJsChunkingContext {
136136

137137
#[turbo_tasks::value_impl]
138138
impl NodeJsChunkingContext {
139-
#[turbo_tasks::function]
140-
async fn chunk_item_id_from_ident(
141-
self: Vc<Self>,
142-
ident: Vc<AssetIdent>,
143-
) -> Result<Vc<ModuleId>> {
144-
let this = self.await?;
145-
dbg!(this.global_information.dbg().await?);
146-
Ok(ModuleId::String(ident.to_string().await?.clone_value()).cell())
147-
}
148-
149139
#[turbo_tasks::function]
150140
fn new(this: Value<NodeJsChunkingContext>) -> Vc<Self> {
151141
this.into_value().cell()
@@ -395,4 +385,9 @@ impl ChunkingContext for NodeJsChunkingContext {
395385
self.chunk_item_id_from_ident(AsyncLoaderModule::asset_ident_for(module))
396386
})
397387
}
388+
389+
#[turbo_tasks::function]
390+
async fn global_information(self: Vc<Self>) -> Result<Vc<OptionGlobalInformation>> {
391+
Ok(self.await?.global_information)
392+
}
398393
}

0 commit comments

Comments
 (0)