From 5c5b23cfa1895b1ed823da93606307164bf9eee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Mon, 20 Jan 2025 01:23:58 +0900 Subject: [PATCH] parallel merge --- .../src/simplify/dce/mod.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs b/crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs index 9107b9b7ac6b..6accf1f02654 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs @@ -10,6 +10,8 @@ use std::{ use ahash::RandomState; use indexmap::{IndexMap, IndexSet}; use petgraph::{algo::tarjan_scc, Direction::Incoming}; +#[cfg(feature = "concurrent")] +use rayon::prelude::*; use rustc_hash::{FxHashMap, FxHashSet}; use swc_atoms::{atom, JsWord}; use swc_common::{ @@ -144,7 +146,6 @@ impl Data { } /// Traverse the graph and subtract usages from `used_names`. - #[allow(unused)] fn subtract_cycles(&mut self) { let edges = take(&mut self.edges); @@ -1215,13 +1216,19 @@ fn merge_data(data: Arc>>) -> Data { .into_iter() .map(|d| d.into_inner()) .collect::>(); - let mut merged = Data::default(); - for data in data { + #[cfg(feature = "concurrent")] + let iter = data.into_par_iter(); + + #[cfg(not(feature = "concurrent"))] + let iter = data.into_iter(); + + let mut merged = iter.reduce(Data::default, |mut merged, data| { merged.merge(data); - } + merged + }); - // merged.subtract_cycles(); + merged.subtract_cycles(); merged }