Skip to content

feature/rayon-like-parallel-execution#1

Merged
AsfhtgkDavid merged 4 commits into
AsfhtgkDavid:mainfrom
Tejtex:feature/rayon-like-parallel-execution
Feb 22, 2026
Merged

feature/rayon-like-parallel-execution#1
AsfhtgkDavid merged 4 commits into
AsfhtgkDavid:mainfrom
Tejtex:feature/rayon-like-parallel-execution

Conversation

@Tejtex
Copy link
Copy Markdown
Contributor

@Tejtex Tejtex commented Feb 21, 2026

It adds the trait ParallelRun, which runs the tasks in parallel, without spawning a separate thread for each task.

Example:

use struct_threads::{Runnable, ParallelRun};
 
struct MathTask(i32, i32);

impl Runnable for MathTask {
  type Output = i32;

  fn run(self) -> Self::Output {
    self.0 + self.1
  }
}

let tasks = vec![MathTask(1, 2), MathTask(3, 4), MathTask(5, 6)];

let results = tasks.par_run(); // Provided by the ParallelRun trait
assert_eq!(results, vec![3, 7, 11]);

Comment thread src/traits.rs Outdated
Comment thread src/traits.rs Outdated
impl<T: Runnable> ParallelRun for Vec<T> {
type Output = T::Output;

fn par_run(self) -> Vec<Self::Output> {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting, but I suggest creating two methods: the first returns Vec<JoinHandler> and is non-blocking, while the second calls the first and performs a join. This would provide more options for user.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I though about it, but i dont know if this could work, because the results wouldnt be ordered and the user would have to sort it themselves.

@Tejtex
Copy link
Copy Markdown
Contributor Author

Tejtex commented Feb 22, 2026

I will make it O(N) today, without sorting.

@Tejtex Tejtex requested a review from AsfhtgkDavid February 22, 2026 15:19
@Tejtex Tejtex marked this pull request as draft February 22, 2026 15:19
@Tejtex
Copy link
Copy Markdown
Contributor Author

Tejtex commented Feb 22, 2026

It now returns a Result. I also added benchmarks.

@Tejtex Tejtex marked this pull request as ready for review February 22, 2026 16:05
Comment thread src/traits.rs Outdated
let chunk_size = self.len().div_ceil(threads);

let mut iter = self.into_iter();
let mut handles = Vec::new();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Vec::with_capacity(threads)

Comment thread src/traits.rs
Comment thread src/traits.rs Outdated
fn par_run(self) -> std::thread::Result<Vec<Self::Output>> {
let threads = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add .min(self.len)

@AsfhtgkDavid AsfhtgkDavid merged commit 224b210 into AsfhtgkDavid:main Feb 22, 2026
2 of 3 checks passed
@AsfhtgkDavid AsfhtgkDavid mentioned this pull request Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants