Skip to content

Commit

Permalink
improve failsafe_analyse (vercel/turborepo#8775)
Browse files Browse the repository at this point in the history
### Description

This puts the results into the same cells to avoid unnecessary
invalidations

Improves performance

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->
  • Loading branch information
sokra authored Jul 17, 2024
1 parent b1c2dd9 commit b815b1c
Showing 1 changed file with 43 additions and 37 deletions.
80 changes: 43 additions & 37 deletions crates/turbopack-ecmascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,24 +391,30 @@ impl EcmascriptModuleAsset {
let result = self.analyze();
let result_value = result.await?;

if result_value.successful {
this.last_successful_analysis
.set(Some(MemoizedSuccessfulAnalysis {
operation: result,
// We need to store the ReadRefs since we want to keep a snapshot.
references: result_value.references.await?,
local_references: result_value.local_references.await?,
reexport_references: result_value.reexport_references.await?,
evaluation_references: result_value.evaluation_references.await?,
exports: result_value.exports.await?,
async_module: result_value.async_module.await?,
source_map: if let Some(map) = *result_value.source_map.await? {
Some(map.await?)
} else {
None
},
}));
} else if let Some(MemoizedSuccessfulAnalysis {
let successful = result_value.successful;
let current_memo = MemoizedSuccessfulAnalysis {
operation: result,
// We need to store the ReadRefs since we want to keep a snapshot.
references: result_value.references.await?,
local_references: result_value.local_references.await?,
reexport_references: result_value.reexport_references.await?,
evaluation_references: result_value.evaluation_references.await?,
exports: result_value.exports.await?,
async_module: result_value.async_module.await?,
source_map: if let Some(map) = *result_value.source_map.await? {
Some(map.await?)
} else {
None
},
};
let state_ref;
let best_value = if successful {
&current_memo
} else {
state_ref = this.last_successful_analysis.get();
state_ref.as_ref().unwrap_or(&current_memo)
};
let MemoizedSuccessfulAnalysis {
operation,
references,
local_references,
Expand All @@ -417,26 +423,26 @@ impl EcmascriptModuleAsset {
exports,
async_module,
source_map,
}) = &*this.last_successful_analysis.get()
{
// It's important to connect to the last operation here to keep it active, so
// it's potentially recomputed when garbage collected
Vc::connect(*operation);
return Ok(AnalyzeEcmascriptModuleResult {
references: ReadRef::cell(references.clone()),
local_references: ReadRef::cell(local_references.clone()),
reexport_references: ReadRef::cell(reexport_references.clone()),
evaluation_references: ReadRef::cell(evaluation_references.clone()),
exports: ReadRef::cell(exports.clone()),
code_generation: result_value.code_generation,
async_module: ReadRef::cell(async_module.clone()),
source_map: Vc::cell(source_map.clone().map(ReadRef::cell)),
successful: false,
}
.cell());
} = best_value;
// It's important to connect to the last operation here to keep it active, so
// it's potentially recomputed when garbage collected
Vc::connect(*operation);
let result = AnalyzeEcmascriptModuleResult {
references: ReadRef::cell(references.clone()),
local_references: ReadRef::cell(local_references.clone()),
reexport_references: ReadRef::cell(reexport_references.clone()),
evaluation_references: ReadRef::cell(evaluation_references.clone()),
exports: ReadRef::cell(exports.clone()),
code_generation: result_value.code_generation,
async_module: ReadRef::cell(async_module.clone()),
source_map: Vc::cell(source_map.clone().map(ReadRef::cell)),
successful,
}

Ok(ReadRef::cell(result_value))
.cell();
if successful {
this.last_successful_analysis.set(Some(current_memo));
}
Ok(result)
}

#[turbo_tasks::function]
Expand Down

0 comments on commit b815b1c

Please sign in to comment.