Skip to content

Commit

Permalink
avoid unnessecary indirection in async modules (vercel/turborepo#5627)
Browse files Browse the repository at this point in the history
### Description

Performance
  • Loading branch information
sokra authored Aug 28, 2023
1 parent c639fb6 commit dad3e6b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 57 deletions.
47 changes: 21 additions & 26 deletions crates/turbopack-ecmascript/src/references/async_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use swc_core::{
use turbo_tasks::{primitives::Bools, trace::TraceRawVcs, TryFlatJoinIterExt, Value, Vc};
use turbopack_core::chunk::availability_info::AvailabilityInfo;

use super::esm::base::ReferencedAsset;
use crate::{
chunk::{
esm_scope::{EsmScope, EsmScopeScc},
Expand Down Expand Up @@ -156,12 +157,24 @@ impl AsyncModule {
.iter()
.map(|r| async {
let referenced_asset = r.get_referenced_asset().await?;
let ident = if *r.is_async(availability_info).await? {
referenced_asset.get_ident().await?
} else {
None
};
anyhow::Ok(ident)
Ok(match &*referenced_asset {
ReferencedAsset::OriginalReferenceTypeExternal(_) => {
// TODO(WEB-1259): we need to detect if external modules are esm
None
}
ReferencedAsset::Some(placeable) => {
if *placeable
.get_async_module()
.is_async(availability_info)
.await?
{
referenced_asset.get_ident().await?
} else {
None
}
}
ReferencedAsset::None => None,
})
})
.try_flat_join()
.await?;
Expand All @@ -170,26 +183,8 @@ impl AsyncModule {
}

#[turbo_tasks::function]
async fn has_top_level_await(self: Vc<Self>) -> Result<Vc<bool>> {
Ok(Vc::cell(self.await?.has_top_level_await))
}

#[turbo_tasks::function]
async fn is_self_async(self: Vc<Self>) -> Result<Vc<bool>> {
let this = self.await?;

if this.has_top_level_await {
return Ok(Vc::cell(true));
}

let bools = Vc::<Bools>::cell(
this.references
.iter()
.map(|r| r.is_external_esm())
.collect(),
);

Ok(bools.any())
pub(crate) fn is_self_async(&self) -> Vc<bool> {
Vc::cell(self.has_top_level_await)
}

#[turbo_tasks::function]
Expand Down
32 changes: 1 addition & 31 deletions crates/turbopack-ecmascript/src/references/esm/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use swc_core::{
use turbo_tasks::{Value, ValueToString, Vc};
use turbopack_core::{
chunk::{
availability_info::AvailabilityInfo, ChunkableModuleReference, ChunkingContext,
ChunkingType, ChunkingTypeOption, ModuleId,
ChunkableModuleReference, ChunkingContext, ChunkingType, ChunkingTypeOption, ModuleId,
},
issue::{IssueSeverity, OptionIssueSource},
module::Module,
Expand Down Expand Up @@ -147,35 +146,6 @@ impl EsmAssetReference {
this.request,
))
}

#[turbo_tasks::function]
pub(crate) async fn is_external_esm(self: Vc<Self>) -> Result<Vc<bool>> {
let asset = self.get_referenced_asset().await?;

let ReferencedAsset::OriginalReferenceTypeExternal(_) = &*asset else {
return Ok(Vc::cell(false));
};

// TODO(WEB-1259): we need to detect if external modules are esm
Ok(Vc::cell(false))
}

#[turbo_tasks::function]
pub(crate) async fn is_async(
self: Vc<Self>,
availability_info: Value<AvailabilityInfo>,
) -> Result<Vc<bool>> {
if *self.is_external_esm().await? {
return Ok(Vc::cell(true));
}

let asset = self.get_referenced_asset().await?;
let ReferencedAsset::Some(placeable) = &*asset else {
return Ok(Vc::cell(false));
};

Ok(placeable.get_async_module().is_async(availability_info))
}
}

#[turbo_tasks::value_impl]
Expand Down

0 comments on commit dad3e6b

Please sign in to comment.