Skip to content

Commit

Permalink
test(parallel): Add test to debug segfault on windows x64 (#9857)
Browse files Browse the repository at this point in the history
**Description:**

 - Diff range: v1.10.4...v1.10.6

**Related issue:**

 - #9855
  • Loading branch information
kdy1 authored Jan 9, 2025
1 parent 624680b commit ae53a35
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 114 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -497,12 +497,7 @@ jobs:
fail-fast: false
matrix:
crate:
- better_scoped_tls
- string_enum
# - swc
- swc_bundler
# - swc_ecma_codegen
# - swc_ecma_minifier
- swc_parallel
steps:
- uses: actions/checkout@v4
with:
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 4 additions & 108 deletions crates/swc_ecma_utils/src/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@
use once_cell::sync::Lazy;
use swc_common::GLOBALS;
use swc_ecma_ast::*;
use swc_parallel::join;

use self::private::Sealed;

mod private {
pub trait Sealed {}

impl<T> Sealed for Vec<T> {}
impl<T> Sealed for &mut Vec<T> {}
impl<T> Sealed for &mut [T] {}
impl<T> Sealed for &[T] {}
}
use swc_parallel::{
items::{IntoItems, Items},
join,
};

static CPU_COUNT: Lazy<usize> = Lazy::new(num_cpus::get);

Expand All @@ -36,102 +28,6 @@ pub trait Parallel: swc_common::sync::Send + swc_common::sync::Sync {
fn after_module_items(&mut self, _stmts: &mut Vec<ModuleItem>) {}
}

pub trait IntoItems: Sealed {
type Elem;
type Items: Items<Elem = Self::Elem>;

fn into_items(self) -> Self::Items;
}

impl<T, I> IntoItems for I
where
I: Items<Elem = T>,
{
type Elem = T;
type Items = I;

fn into_items(self) -> Self::Items {
self
}
}

impl<'a, T> IntoItems for &'a mut Vec<T>
where
T: Send + Sync,
{
type Elem = &'a mut T;
type Items = &'a mut [T];

fn into_items(self) -> Self::Items {
self
}
}

/// This is considered as a private type and it's NOT A PUBLIC API.
#[allow(clippy::len_without_is_empty)]
pub trait Items: Sized + IntoIterator<Item = Self::Elem> + Send + Sealed {
type Elem: Send + Sync;

fn len(&self) -> usize;

#[cfg(feature = "concurrent")]
fn split_at(self, idx: usize) -> (Self, Self);
}

impl<T> Items for Vec<T>
where
T: Send + Sync,
{
type Elem = T;

fn len(&self) -> usize {
Vec::len(self)
}

#[cfg(feature = "concurrent")]
fn split_at(mut self, at: usize) -> (Self, Self) {
let b = self.split_off(at);

(self, b)
}
}

impl<'a, T> Items for &'a mut [T]
where
T: Send + Sync,
{
type Elem = &'a mut T;

fn len(&self) -> usize {
<[T]>::len(self)
}

#[cfg(feature = "concurrent")]
fn split_at(self, at: usize) -> (Self, Self) {
let (a, b) = self.split_at_mut(at);

(a, b)
}
}

impl<'a, T> Items for &'a [T]
where
T: Send + Sync,
{
type Elem = &'a T;

fn len(&self) -> usize {
<[T]>::len(self)
}

#[cfg(feature = "concurrent")]
fn split_at(self, at: usize) -> (Self, Self) {
let (a, b) = self.split_at(at);

(a, b)
}
}

pub trait ParallelExt: Parallel {
/// Invoke `op` in parallel, if `swc_ecma_utils` is compiled with
/// concurrent feature enabled and `nodes.len()` is bigger than threshold.
Expand Down
6 changes: 6 additions & 0 deletions crates/swc_parallel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ version = "1.0.1"
default = ["parallel"]
# Make it really parallel
parallel = ["chili"]
# Alias for parallel, just for CI. Do not use it if you are not working on SWC.
concurrent = ["parallel"]

[dependencies]
chili = { workspace = true, optional = true }
once_cell = { workspace = true }

[dev-dependencies]
hstr = { workspace = true }
scoped-tls = { workspace = true }
102 changes: 102 additions & 0 deletions crates/swc_parallel/src/items.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use self::private::Sealed;

mod private {
pub trait Sealed {}

impl<T> Sealed for Vec<T> {}
impl<T> Sealed for &mut Vec<T> {}
impl<T> Sealed for &mut [T] {}
impl<T> Sealed for &[T] {}
}
pub trait IntoItems: Sealed {
type Elem;
type Items: Items<Elem = Self::Elem>;

fn into_items(self) -> Self::Items;
}

impl<T, I> IntoItems for I
where
I: Items<Elem = T>,
{
type Elem = T;
type Items = I;

fn into_items(self) -> Self::Items {
self
}
}

impl<'a, T> IntoItems for &'a mut Vec<T>
where
T: Send + Sync,
{
type Elem = &'a mut T;
type Items = &'a mut [T];

fn into_items(self) -> Self::Items {
self
}
}

/// This is considered as a private type and it's NOT A PUBLIC API.
#[allow(clippy::len_without_is_empty)]
pub trait Items: Sized + IntoIterator<Item = Self::Elem> + Send + Sealed {
type Elem: Send + Sync;

fn len(&self) -> usize;

fn split_at(self, idx: usize) -> (Self, Self);
}

impl<T> Items for Vec<T>
where
T: Send + Sync,
{
type Elem = T;

fn len(&self) -> usize {
Vec::len(self)
}

fn split_at(mut self, at: usize) -> (Self, Self) {
let b = self.split_off(at);

(self, b)
}
}

impl<'a, T> Items for &'a mut [T]
where
T: Send + Sync,
{
type Elem = &'a mut T;

fn len(&self) -> usize {
<[T]>::len(self)
}

fn split_at(self, at: usize) -> (Self, Self) {
let (a, b) = self.split_at_mut(at);

(a, b)
}
}

impl<'a, T> Items for &'a [T]
where
T: Send + Sync,
{
type Elem = &'a T;

fn len(&self) -> usize {
<[T]>::len(self)
}

fn split_at(self, at: usize) -> (Self, Self) {
let (a, b) = self.split_at(at);

(a, b)
}
}
//
2 changes: 2 additions & 0 deletions crates/swc_parallel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use std::{cell::RefCell, mem::transmute};

pub mod items;

#[derive(Default)]
pub struct MaybeScope<'a>(ScopeLike<'a>);

Expand Down
Loading

0 comments on commit ae53a35

Please sign in to comment.