From 801a1a2800e40cb24a4020912642705f639e7a3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lichu=20Acu=C3=B1a?= Date: Wed, 31 Jul 2024 14:56:49 -0300 Subject: [PATCH] Implemented module ID global optimization using GlobalInformation. --- .../turbopack-browser/src/chunking_context.rs | 17 ++++++----------- crates/turbopack-core/src/changed.rs | 2 +- .../src/chunk/chunking_context.rs | 10 +++++++++- .../src/chunk/global_information.rs | 19 ++++++++++++------- .../turbopack-nodejs/src/chunking_context.rs | 17 ++++++----------- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/crates/turbopack-browser/src/chunking_context.rs b/crates/turbopack-browser/src/chunking_context.rs index 1dfe087c3adb4..e1044a7adf4a9 100644 --- a/crates/turbopack-browser/src/chunking_context.rs +++ b/crates/turbopack-browser/src/chunking_context.rs @@ -1,6 +1,6 @@ use anyhow::{bail, Context, Result}; use tracing::Instrument; -use turbo_tasks::{debug::ValueDebug, RcStr, Value, ValueToString, Vc}; +use turbo_tasks::{RcStr, Value, ValueToString, Vc}; use turbo_tasks_fs::FileSystemPath; use turbopack_core::{ chunk::{ @@ -243,16 +243,6 @@ impl BrowserChunkingContext { #[turbo_tasks::value_impl] impl ChunkingContext for BrowserChunkingContext { - #[turbo_tasks::function] - async fn chunk_item_id_from_ident( - self: Vc, - ident: Vc, - ) -> Result> { - let this = self.await?; - dbg!(this.global_information.dbg().await?); - Ok(ModuleId::String(ident.to_string().await?.clone_value()).cell()) - } - #[turbo_tasks::function] fn name(&self) -> Vc { if let Some(name) = &self.name { @@ -524,4 +514,9 @@ impl ChunkingContext for BrowserChunkingContext { self.chunk_item_id_from_ident(AsyncLoaderModule::asset_ident_for(module)) }) } + + #[turbo_tasks::function] + async fn global_information(self: Vc) -> Result> { + Ok(self.await?.global_information) + } } diff --git a/crates/turbopack-core/src/changed.rs b/crates/turbopack-core/src/changed.rs index 159d40f4d06a6..7a03c9dce12d9 100644 --- a/crates/turbopack-core/src/changed.rs +++ b/crates/turbopack-core/src/changed.rs @@ -17,7 +17,7 @@ async fn get_referenced_output_assets( Ok(parent.references().await?.clone_value().into_iter()) } -async fn get_referenced_modules( +pub async fn get_referenced_modules( parent: Vc>, ) -> Result>> + Send> { Ok(primary_referenced_modules(parent) diff --git a/crates/turbopack-core/src/chunk/chunking_context.rs b/crates/turbopack-core/src/chunk/chunking_context.rs index 027fad30732cb..a8e07185e793e 100644 --- a/crates/turbopack-core/src/chunk/chunking_context.rs +++ b/crates/turbopack-core/src/chunk/chunking_context.rs @@ -4,7 +4,10 @@ use turbo_tasks::{trace::TraceRawVcs, RcStr, TaskInput, Upcast, Value, ValueToSt use turbo_tasks_fs::FileSystemPath; use turbo_tasks_hash::DeterministicHash; -use super::{availability_info::AvailabilityInfo, ChunkableModule, EvaluatableAssets}; +use super::{ + availability_info::AvailabilityInfo, global_information::OptionGlobalInformation, + ChunkableModule, EvaluatableAssets, +}; use crate::{ chunk::{ChunkItem, ModuleId}, environment::Environment, @@ -114,10 +117,15 @@ pub trait ChunkingContext { availability_info: Value, ) -> Result>; + fn global_information(self: Vc) -> Vc; + async fn chunk_item_id_from_ident( self: Vc, ident: Vc, ) -> Result> { + if let Some(global_information) = &*self.global_information().await? { + return Ok(global_information.get_module_id(ident).await?); + } Ok(ModuleId::String(ident.to_string().await?.clone_value()).cell()) } diff --git a/crates/turbopack-core/src/chunk/global_information.rs b/crates/turbopack-core/src/chunk/global_information.rs index 74e06f0703def..e1f42a46880a4 100644 --- a/crates/turbopack-core/src/chunk/global_information.rs +++ b/crates/turbopack-core/src/chunk/global_information.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; -use turbo_tasks::{RcStr, Vc}; +use anyhow::Result; +use turbo_tasks::{ValueToString, Vc}; use super::ModuleId; use crate::ident::AssetIdent; @@ -8,16 +9,20 @@ use crate::ident::AssetIdent; #[turbo_tasks::value] #[derive(Clone, Debug)] pub struct GlobalInformation { - pub test_str: Vc, pub module_id_map: HashMap, } impl GlobalInformation { - pub fn get_module_id(&self, asset_ident: &AssetIdent) -> ModuleId { - self.module_id_map.get(asset_ident).cloned().expect( - "No module ID found for the given asset identifier. This is an internal Turbopack \ - error. Please report it.", - ) + pub async fn get_module_id(&self, asset_ident: Vc) -> Result> { + let ident_str = asset_ident.to_string().await?; + let ident = asset_ident.await?; + let hashed_module_id = self.module_id_map.get(&ident); + if let Some(hashed_module_id) = hashed_module_id { + dbg!("Hashed module ID found", &ident_str, hashed_module_id); + return Ok(hashed_module_id.clone().cell()); + } + dbg!("Hashed module ID not found", &ident_str); + return Ok(ModuleId::String(ident_str.clone_value()).cell()); } } diff --git a/crates/turbopack-nodejs/src/chunking_context.rs b/crates/turbopack-nodejs/src/chunking_context.rs index 307aa5d8939b6..2503444cdba5e 100644 --- a/crates/turbopack-nodejs/src/chunking_context.rs +++ b/crates/turbopack-nodejs/src/chunking_context.rs @@ -2,7 +2,7 @@ use std::iter::once; use anyhow::{bail, Context, Result}; use tracing::Instrument; -use turbo_tasks::{debug::ValueDebug, RcStr, Value, ValueToString, Vc}; +use turbo_tasks::{RcStr, Value, ValueToString, Vc}; use turbo_tasks_fs::FileSystemPath; use turbopack_core::{ chunk::{ @@ -136,16 +136,6 @@ impl NodeJsChunkingContext { #[turbo_tasks::value_impl] impl NodeJsChunkingContext { - #[turbo_tasks::function] - async fn chunk_item_id_from_ident( - self: Vc, - ident: Vc, - ) -> Result> { - let this = self.await?; - dbg!(this.global_information.dbg().await?); - Ok(ModuleId::String(ident.to_string().await?.clone_value()).cell()) - } - #[turbo_tasks::function] fn new(this: Value) -> Vc { this.into_value().cell() @@ -395,4 +385,9 @@ impl ChunkingContext for NodeJsChunkingContext { self.chunk_item_id_from_ident(AsyncLoaderModule::asset_ident_for(module)) }) } + + #[turbo_tasks::function] + async fn global_information(self: Vc) -> Result> { + Ok(self.await?.global_information) + } }