Skip to content

Commit

Permalink
perf: Drop a dep for faster builds
Browse files Browse the repository at this point in the history
`OnceLock` became available as of 1.70 which is older than our new MSRV.
We can easily get away without `Lazy` variants.
  • Loading branch information
epage committed Aug 31, 2023
1 parent 7126f78 commit 087224a
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 63 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ suggestions = ["clap_builder/suggestions"]

# Optional
deprecated = ["clap_builder/deprecated", "clap_derive?/deprecated"] # Guided experience to prepare for next breaking release (at different stages of development, this may become default)
derive = ["dep:clap_derive", "dep:once_cell"]
derive = ["dep:clap_derive"]
cargo = ["clap_builder/cargo"] # Disable if you're not using Cargo, enables Cargo-env-var-dependent macros
wrap_help = ["clap_builder/wrap_help"]
env = ["clap_builder/env"] # Use environment variables during arg parsing
Expand All @@ -103,7 +103,6 @@ bench = false
[dependencies]
clap_builder = { path = "./clap_builder", version = "=4.4.1", default-features = false }
clap_derive = { path = "./clap_derive", version = "=4.4.0", optional = true }
once_cell = { version = "1.12.0", optional = true }

[dev-dependencies]
trybuild = "1.0.82"
Expand Down
3 changes: 1 addition & 2 deletions clap_builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ suggestions = ["dep:strsim", "error-context"]

# Optional
deprecated = [] # Guided experience to prepare for next breaking release (at different stages of development, this may become default)
cargo = ["dep:once_cell"] # Disable if you're not using Cargo, enables Cargo-env-var-dependent macros
cargo = [] # Disable if you're not using Cargo, enables Cargo-env-var-dependent macros
wrap_help = ["help", "dep:terminal_size"]
env = [] # Use environment variables during arg parsing
unicode = ["dep:unicode-width", "dep:unicase"] # Support for unicode characters in arguments and help messages
Expand All @@ -66,7 +66,6 @@ anstyle = "1.0.0"
terminal_size = { version = "0.2.1", optional = true }
backtrace = { version = "0.3.67", optional = true }
unicode-width = { version = "0.1.9", optional = true }
once_cell = { version = "1.12.0", optional = true }

[dev-dependencies]
trybuild = "1.0.82"
Expand Down
7 changes: 0 additions & 7 deletions clap_builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ pub type Error = crate::error::Error<crate::error::DefaultFormatter>;

pub use crate::derive::{Args, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum};

#[doc(hidden)]
pub mod __macro_refs {
#[cfg(feature = "cargo")]
#[doc(hidden)]
pub use once_cell;
}

#[macro_use]
#[allow(missing_docs)]
mod macros;
Expand Down
6 changes: 3 additions & 3 deletions clap_builder/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ macro_rules! crate_authors {
($sep:expr) => {{
static authors: &str = env!("CARGO_PKG_AUTHORS");
if authors.contains(':') {
static CACHED: clap::__macro_refs::once_cell::sync::Lazy<String> =
clap::__macro_refs::once_cell::sync::Lazy::new(|| authors.replace(':', $sep));
let s: &'static str = &*CACHED;
static CACHED: std::sync::OnceLock<String> = std::sync::OnceLock::new();
let s = CACHED.get_or_init(|| authors.replace(':', $sep));
let s: &'static str = &*s;
s
} else {
authors
Expand Down
72 changes: 32 additions & 40 deletions clap_derive/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,20 +575,22 @@ impl Item {
.any(|a| a.magic == Some(MagicAttrName::ValueEnum))
{
quote_spanned!(attr.name.clone().span()=> {
static DEFAULT_VALUE: clap::__derive_refs::once_cell::sync::Lazy<String> = clap::__derive_refs::once_cell::sync::Lazy::new(|| {
static DEFAULT_VALUE: ::std::sync::OnceLock<String> = ::std::sync::OnceLock::new();
let s = DEFAULT_VALUE.get_or_init(|| {
let val: #ty = #val;
clap::ValueEnum::to_possible_value(&val).unwrap().get_name().to_owned()
});
let s: &'static str = &*DEFAULT_VALUE;
let s: &'static str = &*s;
s
})
} else {
quote_spanned!(attr.name.clone().span()=> {
static DEFAULT_VALUE: clap::__derive_refs::once_cell::sync::Lazy<String> = clap::__derive_refs::once_cell::sync::Lazy::new(|| {
static DEFAULT_VALUE: ::std::sync::OnceLock<String> = ::std::sync::OnceLock::new();
let s = DEFAULT_VALUE.get_or_init(|| {
let val: #ty = #val;
::std::string::ToString::to_string(&val)
});
let s: &'static str = &*DEFAULT_VALUE;
let s: &'static str = &*s;
s
})
};
Expand Down Expand Up @@ -643,14 +645,11 @@ impl Item {
})
}

static DEFAULT_STRINGS: clap::__derive_refs::once_cell::sync::Lazy<Vec<::std::string::String>> = clap::__derive_refs::once_cell::sync::Lazy::new(|| {
iter_to_vals(#expr).collect()
});

static DEFAULT_VALUES: clap::__derive_refs::once_cell::sync::Lazy<Vec<&str>> = clap::__derive_refs::once_cell::sync::Lazy::new(|| {
DEFAULT_STRINGS.iter().map(::std::string::String::as_str).collect()
});
DEFAULT_VALUES.iter().copied()
static DEFAULT_STRINGS: ::std::sync::OnceLock<Vec<String>> = ::std::sync::OnceLock::new();
static DEFAULT_VALUES: ::std::sync::OnceLock<Vec<&str>> = ::std::sync::OnceLock::new();
DEFAULT_VALUES.get_or_init(|| {
DEFAULT_STRINGS.get_or_init(|| iter_to_vals(#expr).collect()).iter().map(::std::string::String::as_str).collect()
}).iter().copied()
}
})
} else {
Expand All @@ -663,14 +662,11 @@ impl Item {
iterable.into_iter().map(|val| val.borrow().to_string())
}

static DEFAULT_STRINGS: clap::__derive_refs::once_cell::sync::Lazy<Vec<::std::string::String>> = clap::__derive_refs::once_cell::sync::Lazy::new(|| {
iter_to_vals(#expr).collect()
});

static DEFAULT_VALUES: clap::__derive_refs::once_cell::sync::Lazy<Vec<&str>> = clap::__derive_refs::once_cell::sync::Lazy::new(|| {
DEFAULT_STRINGS.iter().map(::std::string::String::as_str).collect()
});
DEFAULT_VALUES.iter().copied()
static DEFAULT_STRINGS: ::std::sync::OnceLock<Vec<String>> = ::std::sync::OnceLock::new();
static DEFAULT_VALUES: ::std::sync::OnceLock<Vec<&str>> = ::std::sync::OnceLock::new();
DEFAULT_VALUES.get_or_init(|| {
DEFAULT_STRINGS.get_or_init(|| iter_to_vals(#expr).collect()).iter().map(::std::string::String::as_str).collect()
}).iter().copied()
}
})
};
Expand Down Expand Up @@ -707,20 +703,22 @@ impl Item {
.any(|a| a.magic == Some(MagicAttrName::ValueEnum))
{
quote_spanned!(attr.name.clone().span()=> {
static DEFAULT_VALUE: clap::__derive_refs::once_cell::sync::Lazy<::std::ffi::OsString> = clap::__derive_refs::once_cell::sync::Lazy::new(|| {
static DEFAULT_VALUE: ::std::sync::OnceLock<String> = ::std::sync::OnceLock::new();
let s = DEFAULT_VALUE.get_or_init(|| {
let val: #ty = #val;
clap::ValueEnum::to_possible_value(&val).unwrap().get_name().to_owned()
});
let s: &'static ::std::ffi::OsStr = &*DEFAULT_VALUE;
let s: &'static str = &*s;
s
})
} else {
quote_spanned!(attr.name.clone().span()=> {
static DEFAULT_VALUE: clap::__derive_refs::once_cell::sync::Lazy<::std::ffi::OsString> = clap::__derive_refs::once_cell::sync::Lazy::new(|| {
static DEFAULT_VALUE: ::std::sync::OnceLock<::std::ffi::OsString> = ::std::sync::OnceLock::new();
let s = DEFAULT_VALUE.get_or_init(|| {
let val: #ty = #val;
::std::ffi::OsString::from(val)
});
let s: &'static ::std::ffi::OsStr = &*DEFAULT_VALUE;
let s: &'static ::std::ffi::OsStr = &*s;
s
})
};
Expand Down Expand Up @@ -775,14 +773,11 @@ impl Item {
})
}

static DEFAULT_OS_STRINGS: clap::__derive_refs::once_cell::sync::Lazy<Vec<::std::ffi::OsString>> = clap::__derive_refs::once_cell::sync::Lazy::new(|| {
iter_to_vals(#expr).collect()
});

static DEFAULT_VALUES: clap::__derive_refs::once_cell::sync::Lazy<Vec<&::std::ffi::OsStr>> = clap::__derive_refs::once_cell::sync::Lazy::new(|| {
DEFAULT_OS_STRINGS.iter().map(::std::ffi::OsString::as_os_str).collect()
});
DEFAULT_VALUES.iter().copied()
static DEFAULT_STRINGS: ::std::sync::OnceLock<Vec<::std::ffi::OsString>> = ::std::sync::OnceLock::new();
static DEFAULT_VALUES: ::std::sync::OnceLock<Vec<&::std::ffi::OsStr>> = ::std::sync::OnceLock::new();
DEFAULT_VALUES.get_or_init(|| {
DEFAULT_STRINGS.get_or_init(|| iter_to_vals(#expr).collect()).iter().map(::std::ffi::OsString::as_os_str).collect()
}).iter().copied()
}
})
} else {
Expand All @@ -795,14 +790,11 @@ impl Item {
iterable.into_iter().map(|val| val.borrow().into())
}

static DEFAULT_OS_STRINGS: clap::__derive_refs::once_cell::sync::Lazy<Vec<::std::ffi::OsString>> = clap::__derive_refs::once_cell::sync::Lazy::new(|| {
iter_to_vals(#expr).collect()
});

static DEFAULT_VALUES: clap::__derive_refs::once_cell::sync::Lazy<Vec<&::std::ffi::OsStr>> = clap::__derive_refs::once_cell::sync::Lazy::new(|| {
DEFAULT_OS_STRINGS.iter().map(::std::ffi::OsString::as_os_str).collect()
});
DEFAULT_VALUES.iter().copied()
static DEFAULT_STRINGS: ::std::sync::OnceLock<Vec<::std::ffi::OsString>> = ::std::sync::OnceLock::new();
static DEFAULT_VALUES: ::std::sync::OnceLock<Vec<&::std::ffi::OsStr>> = ::std::sync::OnceLock::new();
DEFAULT_VALUES.get_or_init(|| {
DEFAULT_STRINGS.get_or_init(|| iter_to_vals(#expr).collect()).iter().map(::std::ffi::OsString::as_os_str).collect()
}).iter().copied()
}
})
};
Expand Down
7 changes: 0 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,3 @@ pub mod _faq;
pub mod _features;
#[cfg(feature = "unstable-doc")]
pub mod _tutorial;

#[doc(hidden)]
#[cfg(feature = "derive")]
pub mod __derive_refs {
#[doc(hidden)]
pub use once_cell;
}

0 comments on commit 087224a

Please sign in to comment.