diff options
| author | ihc童鞋@提不起劲 <[email protected]> | 2023-07-13 11:31:11 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-13 11:31:11 +0800 |
| commit | ca7877a49c1d2650eac5228227678038aac589c3 (patch) | |
| tree | 4beff48ab3530a333cb319da36ad2520381e054f | |
| parent | 4e1cb8e846f56c2a993ce5f16481efb43a376a7c (diff) | |
feat: support auto-detected parallism (#194)
| -rw-r--r-- | monoio-macros/Cargo.toml | 2 | ||||
| -rw-r--r-- | monoio-macros/src/entry.rs | 15 | ||||
| -rw-r--r-- | monoio/Cargo.toml | 2 |
3 files changed, 12 insertions, 7 deletions
diff --git a/monoio-macros/Cargo.toml b/monoio-macros/Cargo.toml index 28c922c..4009be3 100644 --- a/monoio-macros/Cargo.toml +++ b/monoio-macros/Cargo.toml @@ -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 diff --git a/monoio-macros/src/entry.rs b/monoio-macros/src/entry.rs index 54cf8ad..c1a9439 100644 --- a/monoio-macros/src/entry.rs +++ b/monoio-macros/src/entry.rs @@ -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(()) } @@ -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() diff --git a/monoio/Cargo.toml b/monoio/Cargo.toml index 8451f93..37c0d8f 100644 --- a/monoio/Cargo.toml +++ b/monoio/Cargo.toml @@ -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" |
