Unverified Commit d89f14dd authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: more incremental all_server_paths (#82892)

### What?

Split `all_server_paths` into an inner method to make it more incremental.

Use more RcStrs to avoid cloning strings
parent d8d3b2e0
...@@ -34,7 +34,7 @@ pub struct NapiServerPath { ...@@ -34,7 +34,7 @@ pub struct NapiServerPath {
impl From<ServerPath> for NapiServerPath { impl From<ServerPath> for NapiServerPath {
fn from(server_path: ServerPath) -> Self { fn from(server_path: ServerPath) -> Self {
Self { Self {
path: server_path.path, path: server_path.path.into_owned(),
content_hash: format!("{:x}", server_path.content_hash), content_hash: format!("{:x}", server_path.content_hash),
} }
} }
...@@ -59,7 +59,7 @@ impl From<Option<EndpointOutputPaths>> for NapiWrittenEndpoint { ...@@ -59,7 +59,7 @@ impl From<Option<EndpointOutputPaths>> for NapiWrittenEndpoint {
client_paths, client_paths,
}) => Self { }) => Self {
r#type: "nodejs".to_string(), r#type: "nodejs".to_string(),
entry_path: Some(server_entry_path), entry_path: Some(server_entry_path.into_owned()),
client_paths: client_paths.into_iter().map(From::from).collect(), client_paths: client_paths.into_iter().map(From::from).collect(),
server_paths: server_paths.into_iter().map(From::from).collect(), server_paths: server_paths.into_iter().map(From::from).collect(),
..Default::default() ..Default::default()
......
...@@ -1985,7 +1985,7 @@ impl Endpoint for AppEndpoint { ...@@ -1985,7 +1985,7 @@ impl Endpoint for AppEndpoint {
server_entry_path: node_root server_entry_path: node_root
.get_path_to(&*rsc_chunk.path().await?) .get_path_to(&*rsc_chunk.path().await?)
.context("Node.js chunk entry path must be inside the node root")? .context("Node.js chunk entry path must be inside the node root")?
.to_string(), .into(),
server_paths, server_paths,
client_paths, client_paths,
}, },
......
...@@ -1710,9 +1710,9 @@ impl Endpoint for PageEndpoint { ...@@ -1710,9 +1710,9 @@ impl Endpoint for PageEndpoint {
node_root node_root
.get_path_to(&*entry_chunk.path().await?) .get_path_to(&*entry_chunk.path().await?)
.context("ssr chunk entry path must be inside the node root")? .context("ssr chunk entry path must be inside the node root")?
.to_string() .into()
} else { } else {
String::new() // Empty path when no pages should be created rcstr!("") // Empty path when no pages should be created
}; };
EndpointOutputPaths::NodeJs { EndpointOutputPaths::NodeJs {
......
use anyhow::Result; use anyhow::Result;
use next_core::{all_assets_from_entries, next_manifests::AssetBinding}; use next_core::{all_assets_from_entries, next_manifests::AssetBinding};
use serde::{Deserialize, Serialize};
use tracing::Instrument; use tracing::Instrument;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc};
NonLocalValue, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc, trace::TraceRawVcs,
};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::Asset,
output::{OutputAsset, OutputAssets}, output::{OutputAsset, OutputAssets},
}; };
use turbopack_wasm::wasm_edge_var_name; use turbopack_wasm::wasm_edge_var_name;
/// A reference to a server file with content hash for change detection /// A reference to a server file with content hash for change detection
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, TraceRawVcs, NonLocalValue)] #[turbo_tasks::value]
#[derive(Debug, Clone)]
pub struct ServerPath { pub struct ServerPath {
/// Relative to the root_path /// Relative to the root_path
pub path: String, pub path: RcStr,
pub content_hash: u64, pub content_hash: u64,
} }
...@@ -25,6 +23,27 @@ pub struct ServerPath { ...@@ -25,6 +23,27 @@ pub struct ServerPath {
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
pub struct ServerPaths(Vec<ServerPath>); pub struct ServerPaths(Vec<ServerPath>);
#[turbo_tasks::value(transparent)]
pub struct OptionServerPath(Option<ServerPath>);
#[turbo_tasks::function]
async fn server_path(
asset: Vc<Box<dyn OutputAsset>>,
node_root: FileSystemPath,
) -> Result<Vc<OptionServerPath>> {
Ok(Vc::cell(
if let Some(path) = node_root.get_path_to(&*asset.path().await?) {
let content_hash = *asset.content().file_content().hash().await?;
Some(ServerPath {
path: RcStr::from(path),
content_hash,
})
} else {
None
},
))
}
/// Return a list of all server paths with filename and hash for all output /// Return a list of all server paths with filename and hash for all output
/// assets references from the `assets` list. Server paths are identified by /// assets references from the `assets` list. Server paths are identified by
/// being inside `node_root`. /// being inside `node_root`.
...@@ -33,38 +52,24 @@ pub async fn all_server_paths( ...@@ -33,38 +52,24 @@ pub async fn all_server_paths(
assets: Vc<OutputAssets>, assets: Vc<OutputAssets>,
node_root: FileSystemPath, node_root: FileSystemPath,
) -> Result<Vc<ServerPaths>> { ) -> Result<Vc<ServerPaths>> {
let span = tracing::info_span!("all_server_paths"); let span = tracing::info_span!(
"all_server_paths",
assets_count = tracing::field::Empty,
server_assets_count = tracing::field::Empty
);
let span_clone = span.clone();
async move { async move {
let all_assets = all_assets_from_entries(assets).await?; let all_assets = all_assets_from_entries(assets).await?;
let node_root = node_root.clone(); span.record("assets_count", all_assets.len());
Ok(Vc::cell( let server_paths = all_assets
all_assets
.iter() .iter()
.map(|&asset| { .map(|&asset| server_path(*asset, node_root.clone()).owned())
let node_root = node_root.clone();
async move {
Ok(
if let Some(path) = node_root.get_path_to(&*asset.path().await?) {
let content_hash = match *asset.content().await? {
AssetContent::File(file) => *file.hash().await?,
AssetContent::Redirect { .. } => 0,
};
Some(ServerPath {
path: path.to_string(),
content_hash,
})
} else {
None
},
)
}
})
.try_flat_join() .try_flat_join()
.await?, .await?;
)) span.record("server_assets_count", server_paths.len());
Ok(Vc::cell(server_paths))
} }
.instrument(span) .instrument(span_clone)
.await .await
} }
......
...@@ -147,7 +147,7 @@ pub struct EndpointOutput { ...@@ -147,7 +147,7 @@ pub struct EndpointOutput {
pub enum EndpointOutputPaths { pub enum EndpointOutputPaths {
NodeJs { NodeJs {
/// Relative to the root_path /// Relative to the root_path
server_entry_path: String, server_entry_path: RcStr,
server_paths: Vec<ServerPath>, server_paths: Vec<ServerPath>,
client_paths: Vec<RcStr>, client_paths: Vec<RcStr>,
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment