Skip to content

Commit

Permalink
feat: support auto-detected parallism (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
ihciah committed Jul 13, 2023
1 parent 4e1cb8e commit ca7877a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion monoio-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT/Apache-2.0"
name = "monoio-macros"
readme = "README.md"
repository = "https://github.com/bytedance/monoio"
version = "0.0.3"
version = "0.1.0"

[lib]
proc-macro = true
Expand Down
15 changes: 10 additions & 5 deletions monoio-macros/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ impl Configuration {
}

let threads = parse_int(threads, span, "threads")? as u32;
if threads == 0 {
return Err(syn::Error::new(span, "`threads` may not be 0."));
}
self.threads = Some((threads, span));
Ok(())
}
Expand Down Expand Up @@ -306,13 +303,21 @@ fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> To
// Check covered when building config.
debug_assert!(matches!(input.sig.output, syn::ReturnType::Default));

let threads = config.threads.unwrap() - 1;
let threads = config.threads.unwrap();
let threads_expr = if threads == 0 {
// auto detected parallism
quote!(::std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1))
} else {
quote!(#threads)
};
input.block = syn::parse2(quote_spanned! {last_stmt_end_span=>
{
let body = async #body;

#[allow(clippy::needless_collect)]
let threads: Vec<_> = (0 .. #threads)
let threads: Vec<_> = (1 .. #threads_expr)
.map(|_| {
::std::thread::spawn(|| {
#rt.build()
Expand Down
2 changes: 1 addition & 1 deletion monoio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ version = "0.1.6"

# common dependencies
[dependencies]
monoio-macros = { version = "0.0.3", path = "../monoio-macros", optional = true }
monoio-macros = { version = "0.1.0", path = "../monoio-macros", optional = true }

auto-const-array = "0.2"
fxhash = "0.2"
Expand Down

0 comments on commit ca7877a

Please sign in to comment.