Skip to content

Commit da96e23

Browse files
committed
make the debug assertions conditional
1 parent 7083348 commit da96e23

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

turbopack/crates/turbopack-ecmascript/src/references/exports_info.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ use crate::{
2323
/// initialize the binding a single time.
2424
///
2525
/// This singleton behavior must be enforced by the caller!
26-
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)]
26+
#[derive(
27+
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
28+
)]
2729
pub struct ExportsInfoBinding {}
2830

2931
impl ExportsInfoBinding {
@@ -89,7 +91,9 @@ impl From<ExportsInfoBinding> for CodeGen {
8991
///
9092
/// There can be many references, and they appear at any nesting in the file. But all references
9193
/// refer to the same mutable object.
92-
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)]
94+
#[derive(
95+
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
96+
)]
9397
pub struct ExportsInfoRef {
9498
ast_path: AstPath,
9599
}

turbopack/crates/turbopack-ecmascript/src/references/mod.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ impl AnalyzeEcmascriptModuleResult {
211211
}
212212
}
213213

214+
/// In debug builds, use FxIndexSet to catch duplicate code gens
215+
/// In release builds, use Vec for better performance
216+
#[cfg(debug_assertions)]
217+
type CodeGenCollection = FxIndexSet<CodeGen>;
218+
#[cfg(not(debug_assertions))]
219+
type CodeGenCollection = Vec<CodeGen>;
220+
214221
/// A temporary analysis result builder to pass around, to be turned into an
215222
/// `Vc<AnalyzeEcmascriptModuleResult>` eventually.
216223
pub struct AnalyzeEcmascriptModuleResultBuilder {
@@ -227,7 +234,7 @@ pub struct AnalyzeEcmascriptModuleResultBuilder {
227234
// This caches repeated access because EsmAssetReference::new is not a turbo task function.
228235
esm_references_rewritten: FxHashMap<usize, FxIndexMap<RcStr, ResolvedVc<EsmAssetReference>>>,
229236

230-
code_gens: FxIndexSet<CodeGen>,
237+
code_gens: CodeGenCollection,
231238
exports: EcmascriptExports,
232239
async_module: ResolvedVc<OptionAsyncModule>,
233240
successful: bool,
@@ -293,12 +300,19 @@ impl AnalyzeEcmascriptModuleResultBuilder {
293300
C: Into<CodeGen>,
294301
{
295302
if self.analyze_mode.is_code_gen() {
296-
let (index, added) = self.code_gens.insert_full(code_gen.into());
297-
debug_assert!(
298-
added,
299-
"Duplicate code gen added: {:?}",
300-
self.code_gens.get_index(index)
301-
);
303+
#[cfg(debug_assertions)]
304+
{
305+
let (index, added) = self.code_gens.insert_full(code_gen.into());
306+
debug_assert!(
307+
added,
308+
"Duplicate code gen added: {:?}",
309+
self.code_gens.get_index(index)
310+
);
311+
}
312+
#[cfg(not(debug_assertions))]
313+
{
314+
self.code_gens.push(code_gen.into());
315+
}
302316
}
303317
}
304318

@@ -413,6 +427,12 @@ impl AnalyzeEcmascriptModuleResultBuilder {
413427
}
414428

415429
self.code_gens.shrink_to_fit();
430+
431+
#[cfg(debug_assertions)]
432+
let code_generation = self.code_gens.into_iter().collect::<Vec<_>>();
433+
#[cfg(not(debug_assertions))]
434+
let code_generation = self.code_gens;
435+
416436
Ok(AnalyzeEcmascriptModuleResult::cell(
417437
AnalyzeEcmascriptModuleResult {
418438
references,
@@ -421,7 +441,7 @@ impl AnalyzeEcmascriptModuleResultBuilder {
421441
esm_reexport_references: ResolvedVc::cell(
422442
esm_reexport_references.unwrap_or_default(),
423443
),
424-
code_generation: ResolvedVc::cell(self.code_gens.into_iter().collect::<Vec<_>>()),
444+
code_generation: ResolvedVc::cell(code_generation),
425445
exports: self.exports.resolved_cell(),
426446
async_module: self.async_module,
427447
has_side_effect_free_directive: self.has_side_effect_free_directive,

0 commit comments

Comments
 (0)