feat(thread_aware): expose ThreadRegistry::with_hardware for SystemHardware injection#535
feat(thread_aware): expose ThreadRegistry::with_hardware for SystemHardware injection#535sandersaares wants to merge 7 commits into
Conversation
Promote with_hardware to public API so services can inject a single shared SystemHardware instance rather than re-resolving SystemHardware::current(). new() stays a convenience wrapper threading the resolved instance through. Allow many_cpus::SystemHardware in external types. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
f07843f to
f36f030
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #535 +/- ##
=======================================
Coverage 100.0% 100.0%
=======================================
Files 343 343
Lines 26202 26208 +6
=======================================
+ Hits 26202 26208 +6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR makes ThreadRegistry::with_hardware a public API (behind the threads feature) so downstream consumers can inject and reuse a shared many_cpus::SystemHardware instead of repeatedly resolving SystemHardware::current() across layers.
Changes:
- Promotes
ThreadRegistry::with_hardwarefrompub(crate)topuband updates rustdoc to describe intended injection/reuse. - Keeps
ThreadRegistry::newbehavior as a convenience wrapper that resolvesSystemHardware::current(). - Adds
many_cpus_impl::system_hardware::SystemHardwaretocargo_check_external_typesallowlist to account for the new public signature.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| crates/thread_aware/src/registry.rs | Exposes with_hardware publicly and adjusts docs for new/with_hardware. |
| crates/thread_aware/Cargo.toml | Allowlists SystemHardware’s defining path for public API external-type checks. |
Comments suppressed due to low confidence (2)
crates/thread_aware/src/registry.rs:64
- The panic documentation says "more than
u16::MAXprocessors or memory regions", butwith_hardwarecurrently assertsprocessors.len() < u16::MAX as usizeandnuma_nodes.len() < u16::MAX as usize, which also panics when the count is exactlyu16::MAX. Update the docs to match the actual boundary (or adjust the checks).
/// This will panic if there are not enough processors available when using `Manual` or if no processors are available when using `Auto` or `All`.
/// If there are more than `u16::MAX` processors or memory regions.
crates/thread_aware/src/registry.rs:88
- Now that
with_hardwareis part of the public API, the panic message from thisexpectis customer-facing. It would be more actionable if it included the requestedProcessorCountvalue (e.g., which manual count was requested).
let processors = match count {
ProcessorCount::Auto | ProcessorCount::All => builder.take_all(),
ProcessorCount::Manual(count) => builder.take(*count),
}
.expect("Not enough processors available");
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The assert rejected exactly u16::MAX, contradicting the documented 'more than u16::MAX' boundary; both fit in u16 so allow them. Use try_from per clippy. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…asserts Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The u16::MAX processor/memory-region asserts can't be exercised via fake hardware (region count can never exceed processor count), so move them into a coverage(off) helper. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ip mutating unreachable guard The coverage CI compiles the lib crate (not just tests) with coverage instrumentation, so the coverage_attribute feature must be gated on coverage_nightly alone, matching crates like bytesbuf that put coverage(off) on production code. Mark check_capacity mutants::skip since its asserts are unreachable via tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Vaiz
left a comment
There was a problem hiding this comment.
Thing to consider: now that we expose a type from 3d party crate, it means any breaking change in many_cpus crate will be propagated as breaking change in thread_aware
Is there some other way to do it?
|
@Vaiz If you have specific proposals, please propose. Otherwise, this is not particularly new information and has already been considered. I was thinking it might be nice to split |
|
| pub(crate) fn with_hardware(count: &ProcessorCount, hardware: &SystemHardware) -> Self { | ||
| let builder = hardware.processors().to_builder(); | ||
|
|
||
| pub fn with_hardware(count: &ProcessorCount, hardware: &SystemHardware) -> Self { |
There was a problem hiding this comment.
yet another exposure to APIs that can change. thread_aware being such a foundational crate, I believe we should have a minimal "trait-only" crate that exposes bare minimum (ThreadAware trait, Affinity type) so libraries that expose these APIs can rely on stable crate.
Thanks for concrete suggestion! I am not going to go down this path because it would require reinventing the entire |
Promote
ThreadRegistry::with_hardwareto public API so consumers can inject a single sharedmany_cpus::SystemHardwareinstance instead of having every layer re-resolveSystemHardware::current().SystemHardwareis meant to be reused as a service, but the only public constructor (new/default) hid the injection path, leaving customers unable to supply their own.ThreadRegistry::newkeeps resolvingSystemHardware::current()at the top of the chain and threads that one instance through, so simple call sites are unchanged. Addedmany_cpus::SystemHardwareto the crate'sallowed_external_typesallowlist since it now appears in the public signature.Also fixes a pre-existing boundary off-by-one: the asserts now permit exactly
u16::MAXprocessors/memory regions, matching the documented 'more than u16::MAX' panic boundary. And the 'not enough processors available' panic now reports the requested vs available counts to make injection failures actionable.This is the only place in the workspace that touches
SystemHardware; no other crate constructsThreadRegistry.