Skip to content

Add sync option to -Z threads to force synchronization on one thread#156198

Open
Zoxc wants to merge 1 commit intorust-lang:mainfrom
Zoxc:threads-sync
Open

Add sync option to -Z threads to force synchronization on one thread#156198
Zoxc wants to merge 1 commit intorust-lang:mainfrom
Zoxc:threads-sync

Conversation

@Zoxc
Copy link
Copy Markdown
Contributor

@Zoxc Zoxc commented May 5, 2026

This adds a sync option to -Z threads to force synchronization while still using one thread. This is useful to measure overhead of synchronization without the noise of additional threads.

@rustbot rustbot added A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 5, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 5, 2026

r? @folkertdev

rustbot has assigned @folkertdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 19 candidates

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Copy link
Copy Markdown
Contributor

@folkertdev folkertdev left a comment

Choose a reason for hiding this comment

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

A small nit, after that r=me

View changes since this review

Comment on lines +1070 to 1089
pub(crate) fn parse_threads(slot: &mut Option<usize>, v: Option<&str>) -> bool {
let n = match v {
Some("sync") => {
*slot = Some(1);
return true;
}
None => false,
Some(s) => match s.parse().ok() {
Some(0) => std::thread::available_parallelism().map_or(1, NonZero::<usize>::get),
Some(i) => i,
None => return false,
},
None => return false,
};

// We want to cap the number of threads here to avoid large numbers like 999999 and compiler panics.
// This solution was suggested here https://github.com/rust-lang/rust/issues/117638#issuecomment-1800925067
*slot = slot.clone().min(MAX_THREADS_CAP);
ret
let n = n.min(MAX_THREADS_CAP);
*slot = (n > 1).then_some(n);
true
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this will now be clearer as

Suggested change
pub(crate) fn parse_threads(slot: &mut Option<usize>, v: Option<&str>) -> bool {
let n = match v {
Some("sync") => {
*slot = Some(1);
return true;
}
None => false,
Some(s) => match s.parse().ok() {
Some(0) => std::thread::available_parallelism().map_or(1, NonZero::<usize>::get),
Some(i) => i,
None => return false,
},
None => return false,
};
// We want to cap the number of threads here to avoid large numbers like 999999 and compiler panics.
// This solution was suggested here https://github.com/rust-lang/rust/issues/117638#issuecomment-1800925067
*slot = slot.clone().min(MAX_THREADS_CAP);
ret
let n = n.min(MAX_THREADS_CAP);
*slot = (n > 1).then_some(n);
true
}
pub(crate) fn parse_threads(slot: &mut Option<usize>, v: Option<&str>) -> bool {
let Some(s) = v else { return false };
// Configures to use synchronization despite only using one thread.
if s == "sync" {
*slot = Some(1);
return true;
}
let n = match s.parse().ok() {
Some(0) => std::thread::available_parallelism().map_or(1, NonZero::<usize>::get),
Some(i) => i,
None => return false,
};
// We want to cap the number of threads here to avoid large numbers like 999999 and compiler panics.
// This solution was suggested here https://github.com/rust-lang/rust/issues/117638#issuecomment-1800925067
let n = n.min(MAX_THREADS_CAP);
*slot = (n > 1).then_some(n);
true
}

maybe with another comment on the (n > 1).then_some(n) line.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 6, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 6, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants