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

perf: improve compile-time efficiency #52

Merged
merged 5 commits into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ doctest = false
proc-macro = true

[dependencies]
Inflector = "0.11.4"
cargo_metadata = "0.15.4"
darling = "0.13.0"
once_cell = "1.17.1"
Expand Down
22 changes: 10 additions & 12 deletions impl/src/models/fncmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use inflector::Inflector;
use proc_macro2::TokenStream;
use proc_macro_error::abort;
use quote::{quote, ToTokens};
Expand Down Expand Up @@ -37,15 +36,15 @@ impl Fncmd {
);
}

let fn_attrs = item.attrs.iter();
let fn_attrs = &item.attrs;
let fn_vis = &item.vis;
let fn_args = item.sig.inputs.iter();
let fn_args = &item.sig.inputs;
let fn_ret = &item.sig.output;
let fn_body = &item.block;
let asyncness = &item.sig.asyncness;

let mut fn_doc = None;
let mut fncmd_attrs: Vec<Attribute> = Vec::new();
let mut fncmd_attrs: Vec<Attribute> = Vec::with_capacity(fn_attrs.len());

for attr in fn_attrs {
if attr.path.is_ident("doc") {
Expand All @@ -55,7 +54,7 @@ impl Fncmd {
}
}

let fncmd_args: Vec<FncmdArg> = fn_args.map(FncmdArg::parse).collect();
let fncmd_args: Vec<FncmdArg> = fn_args.iter().map(FncmdArg::parse).collect();

Fncmd {
name,
Expand All @@ -67,7 +66,7 @@ impl Fncmd {
visibility: fn_vis.clone(),
subcmds,
version,
asyncness: *asyncness,
asyncness: asyncness.clone(),
}
}
}
Expand Down Expand Up @@ -102,12 +101,11 @@ impl ToTokens for Fncmd {

let (subcmd_imports, subcmd_patterns): (Vec<_>, Vec<_>) = subcmds
.iter()
.map(|(name, (_, path))| {
let subcmd_name = name.strip_prefix(cmd_name).unwrap();
let snake_case_name = subcmd_name.to_snake_case();
let enumitem_name: Ident = parse_str(&format!("__{}", snake_case_name)).unwrap();
let mod_name: Ident =
parse_str(&format!("__fncmd_mod_{}", snake_case_name)).unwrap();
.map(|(name, path)| {
let subcmd_name = name.strip_prefix(&format!("{cmd_name}-")).unwrap();
let subcmd_name = subcmd_name.to_lowercase();
let enumitem_name: Ident = parse_str(&format!("__{}", subcmd_name)).unwrap();
let mod_name: Ident = parse_str(&format!("__fncmd_mod_{}", subcmd_name)).unwrap();
let path = path
.to_str()
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion impl/src/models/fncmd_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl FncmdArg {
};

let mut doc: Option<TokenStream> = None;
let mut attrs = Vec::new();
let mut attrs = Vec::with_capacity(arg.attrs.len());

for attr in arg.attrs.iter() {
match attr.path.get_ident().map(|ident| ident.to_string()).as_deref() {
Expand Down
21 changes: 7 additions & 14 deletions impl/src/models/fncmd_subcmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use syn::visit::Visit;
use syn::ItemFn;

pub struct FncmdSubcmds {
map: HashMap<String, (bool, PathBuf)>,
map: HashMap<String, PathBuf>,
}

impl FncmdSubcmds {
pub fn iter(&self) -> impl Iterator<Item = (&String, &(bool, PathBuf))> { self.map.iter() }
pub fn iter(&self) -> impl Iterator<Item = (&String, &PathBuf)> { self.map.iter() }
}

/// Check if `it` is a subcommand of `of`.
Expand All @@ -27,7 +27,7 @@ impl From<(&Target, &Package)> for FncmdSubcmds {
});

// Enumerate all possible subcommands.
let mut map: HashMap<String, (bool, PathBuf)> = targets
let mut map: HashMap<String, PathBuf> = targets
.filter_map(|target| {
// Read the file.
let content = {
Expand All @@ -38,15 +38,9 @@ impl From<(&Target, &Package)> for FncmdSubcmds {
};
// If parsing failed, just skip hereafter.
parse_file(&content).ok().and_then(|ast| {
Visitor::from(&ast).get_main_fncmd().map(|function| {
Visitor::from(&ast).get_main_fncmd().is_some().then(|| {
// Prepare to `collect` into a `HashMap`.
(
target.name.to_owned(),
(
matches!(function.vis, syn::Visibility::Public(_)),
target.src_path.to_owned().into_std_path_buf(),
),
)
(target.name.clone(), target.src_path.clone().into_std_path_buf())
})
})
})
Expand All @@ -62,9 +56,6 @@ impl From<(&Target, &Package)> for FncmdSubcmds {
map.retain(|name, _| {
!table.iter().any(|(name_other, _)| is_subcommand(name, name_other))
});

// Remove all non-`pub` targets.
map.retain(|_, (is_pub, _)| *is_pub);
}

FncmdSubcmds { map }
Expand Down Expand Up @@ -100,6 +91,8 @@ impl<'ast> Visitor<'ast> {
.find(|&&function| {
// Needs to be `main`.
function.sig.ident == "main"
// And needs to be `pub`.
&& matches!(function.vis, syn::Visibility::Public(_))
// And needs to have `#[fncmd]` attribute.
&& function
.attrs
Expand Down
8 changes: 4 additions & 4 deletions impl/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub mod fncmd;
mod fncmd;
pub use self::fncmd::*;

pub mod fncmd_arg;
mod fncmd_arg;
pub use self::fncmd_arg::*;

pub mod fncmd_attr;
mod fncmd_attr;
pub use self::fncmd_attr::*;

pub mod fncmd_subcmds;
mod fncmd_subcmds;
pub use self::fncmd_subcmds::*;