Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ways to throttle thread creation #7

Closed
wants to merge 1 commit into from

Conversation

Zoxc
Copy link

@Zoxc Zoxc commented Mar 4, 2020

This changes thread spawning to only spawn a single thread at a time. The thread_wait_time method allows you to specify a wait time before adding a thread to the pool which also delays spawning the next thread. The thread pool can now terminate before all worker threads have spawned. A set_thread_target method is added which allows you to tell the thread pool how many threads it should spawn. This thread target can change over time, but the thread pool will never remove threads once they are spawned.

To test this I used a crate which depends on 101 dummy crates each with 16 empty functions. In rustc I used set_thread_target(16) before creating TyCtxt and set_thread_target(1) after encoding metadata, which corresponds to the parts of rustc that takes advantage of the thread pool. thread_wait_time was used in rustc like this:

thread_wait_time(|index| {
    // Throttle thread creation to avoid grabbing all jobserver tokens from competing
    // rustc instances and to avoid creating many threads on smaller crates.
    if index == 0 { None } else { Some(Duration::from_millis(20 / index as u64)) }
})

I then compiled this crate with -Z threads=16 and -Z threads=1 with a parallel compiler to measure the overhead caused by spawning more threads. On Windows this was measured to 5% before this PR and 0.2% after. In a Linux VM I measured 85% overhead before this PR and 12% after. Most of the Linux improvement is likely due to reducing the thundering herd problem when the jobserver gives out a token, since less threads will be waiting on tokens.

cc @cuviper @Mark-Simulacrum @alexcrichton

This changes thread spawning to only spawn a single thread at a time. The `thread_wait_time` method allows you to specify a wait time before adding a thread to the pool which also delays spawning the next thread. The thread pool can now terminate before all worker threads have spawned. A `set_thread_target` method is added which allows you to tell the thread pool how many threads it should spawn. This thread target can change over time, but the thread pool will never remove threads once they are spawned.
@alexcrichton
Copy link
Member

I don't really have any bandwidth to review this right now, but I also sort of feel like we continue to be moving backwards from the goal of "rustc should use vanilla upstream rayon"?

@nikomatsakis
Copy link

Closing this pull request as Zoxc is stepping back from compiler development; see rust-lang/team#316.

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.

3 participants