Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
reiase committed Mar 18, 2024
1 parent cf9cd33 commit 15ea7e5
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 106 deletions.
24 changes: 6 additions & 18 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "hyperparameter"
version = "0.5.9"
license = "Apache-2.0"
description = "A high performance configuration system for Rust and Python."
description = "A high performance configuration system for Rust."
homepage = "https://reiase.github.io/hyperparameter/"
readme = "examples/rust/README.md"
repository = "https://github.com/reiase/hyperparameter"
Expand All @@ -16,22 +16,18 @@ exclude = [
]

[features]
default = ["json", "toml", "ini", "clap"]
default = ["json", "toml", "clap"]
json = ["config/json"]
toml = ["config/toml"]
ini = ["config/ini"]
clap = ["dep:linkme", "dep:clap"]

[lib]
name = "hyperparameter"
crate-type = ["rlib", "staticlib"]

[dependencies]
backtrace = "0.3.69"
lazy_static = "1.4.0"
phf = { version = "0.11", features = ["macros"] }
signal-hook = "0.3.17"
tokio = { version = "1.31.0", features = ["full"] }
xxhash-rust = { version = "0.8.7", features = ["xxh3", "xxh64", "const_xxh64"] }
const-str = "0.5.6"
config = "0.14.0"
Expand All @@ -46,18 +42,10 @@ clap = { version = "4.4.7", features = ["derive"] }
[profile.dev]
overflow-checks = false

[profile.release]
strip = "debuginfo"
lto = true

[[bench]]
name = "bench_apis"
harness = false

[[example]]
name = "clap_mini"
path = "examples/rust/clap_mini.rs"

[[example]]
name = "clap_layered"
path = "examples/rust/clap_layered.rs"

[[example]]
name = "clap_full"
path = "examples/rust/clap_full.rs"
25 changes: 13 additions & 12 deletions core/benches/bench_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn foo_with_config(x: i64, cfg: &Config) -> i64 {
fn call_foo(nloop: i64) -> i64 {
let mut sum = 0;
for i in 0..nloop {
sum = sum + foo(i, 42);
sum += foo(i, 42);
}
sum
}
Expand All @@ -39,7 +39,7 @@ fn call_foo_with_ps(nloop: i64) -> i64 {
with_params! {
set y = 42;

sum = sum + foo_with_ps(i);
sum += foo_with_ps(i);
}
}
sum
Expand All @@ -53,7 +53,7 @@ fn call_foo_with_ps_optimized(nloop: i64) -> i64 {
set y = 42;

for i in 0..nloop {
sum = sum + foo_with_ps(i);
sum += foo_with_ps(i);
}
}
sum
Expand All @@ -62,12 +62,12 @@ fn call_foo_with_ps_optimized(nloop: i64) -> i64 {
#[inline(never)]
fn call_foo_with_ps_and_raw_btree(nloop: i64) -> i64 {
let mut sum = 0;
const KEY: u64 = xxh::xxhash("y".as_bytes());
const KEY: u64 = xxhash("y".as_bytes());
with_params! {
set y = 42;

for i in 0..nloop {
sum = sum + THREAD_STORAGE.with(|ts| ts.borrow_mut().get_or_else(KEY, i));
sum += THREAD_STORAGE.with(|ts| ts.borrow_mut().get_or_else(KEY, i));
}
}
sum
Expand All @@ -77,7 +77,7 @@ fn call_foo_with_ps_and_raw_btree(nloop: i64) -> i64 {
fn call_foo_with_config_rs(nloop: i64, cfg: &Config) -> i64 {
let mut sum = 0;
for i in 0..nloop {
sum = sum + foo_with_config(i, cfg);
sum += foo_with_config(i, cfg);
}
sum
}
Expand Down Expand Up @@ -106,12 +106,12 @@ pub fn bench_apis_with_ps(c: &mut Criterion) {

pub fn bench_config_rs(c: &mut Criterion) {
let cfg = config::Config::builder()
.add_source(config::File::from_str(
"{\"y\": 1}",
config::FileFormat::Json,
))
.build()
.unwrap();
.add_source(config::File::from_str(
"{\"y\": 1}",
config::FileFormat::Json,
))
.build()
.unwrap();
c.bench_function("raw api with config-rs", |b| {
b.iter(|| call_foo_with_config_rs(black_box(10000), &cfg))
});
Expand All @@ -123,5 +123,6 @@ criterion_group!(
bench_apis_with_ps_and_raw_btree,
bench_apis_with_ps_optimized,
bench_apis_with_ps,
bench_config_rs,
);
criterion_main!(benches);
7 changes: 4 additions & 3 deletions core/examples/clap_full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ struct CommandLineArgs {
define: Vec<String>,

/// Specifies the configuration file path.
#[arg(short = 'C', long, default_value = "examples/rust/cfg.toml")]
#[arg(short = 'C', long, default_value = "examples/cfg.toml")]
config: String,
}

fn foo(desc: &str) {
with_params! {
/// Example param1 // this explain shows in `<app> --help`
get param1 = example.param1 or "default".to_string();
get param1 = example.param1 or "default".to_string();

println!("param1={} // {}", param1, desc);
}
Expand All @@ -30,7 +30,8 @@ fn main() {
let config_path = Path::new(&args.config);
let config = config::Config::builder()
.add_source(File::from(config_path))
.build().unwrap();
.build()
.unwrap();

foo("Outside any specific scope");

Expand Down
10 changes: 7 additions & 3 deletions core/examples/clap_layered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct CommandLineArgs {
define: Vec<String>,

/// Specifies the configuration file path.
#[arg(short = 'C', long, default_value = "examples/rust/cfg.toml")]
#[arg(short = 'C', long, default_value = "examples/cfg.toml")]
config: String,
}

Expand All @@ -21,10 +21,14 @@ fn main() {
let config_path = Path::new(&args.config);
let config = config::Config::builder()
.add_source(File::from(config_path))
.build().unwrap();
.build()
.unwrap();

// No scope
println!("param1={}\t// No scope", get_param!(example.param1, "default".to_string()));
println!(
"param1={}\t// No scope",
get_param!(example.param1, "default".to_string())
);

with_params! { // Scope with configuration file parameters
params config.param_scope();
Expand Down
9 changes: 4 additions & 5 deletions core/examples/clap_mini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::Parser;
use hyperparameter::*;

#[derive(Parser)]
#[command(after_long_help=generate_params_help())]
#[command(after_long_help=generate_params_help())] // Displays parameter helps when `<app> --help` is executed.
struct CommandLineArgs {
/// Specifies hyperparameters in the format `-D key=value` via the command line.
#[arg(short = 'D', long)]
Expand All @@ -13,9 +13,8 @@ fn main() {
let args = CommandLineArgs::parse();
with_params! {
params ParamScope::from(&args.define);

// Retrieves `example.param1` with a default value of `1` if not specified.
println!("param1={}", get_param!(example.param1, 1));
// Displays a help message when `<app> --help` is executed.
println!("param2={}", get_param!(example.param2, false, "help for example.param2"));
println!("param1={}", get_param!(example.param1, false, "help for example.param1"));
}
}
}
36 changes: 21 additions & 15 deletions core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ where
key
);
if let ParamScope::Just(changes) = self {
if changes.contains_key(&key) {
changes.update(key, val);
if let std::collections::btree_map::Entry::Vacant(e) = changes.entry(key) {
e.insert(Entry::new("", val));
} else {
changes.insert(key, Entry::new("", val));
changes.update(key, val);
}
}
}
Expand All @@ -148,11 +148,17 @@ where
fn put(&mut self, key: K, val: V) {
let hkey = key.xxh();
if let ParamScope::Just(changes) = self {
if changes.contains_key(&hkey) {
changes.update(hkey, val);
} else {
// if changes.contains_key(&hkey) {
// changes.update(hkey, val);
// } else {
// let key: String = key.into();
// changes.insert(hkey, Entry::new(key, val));
// }
if let std::collections::btree_map::Entry::Vacant(e) = changes.entry(hkey) {
let key: String = key.into();
changes.insert(hkey, Entry::new(key, val));
e.insert(Entry::new(key, val));
} else {
changes.update(hkey, val);
}
} else {
THREAD_STORAGE.with(|ts| ts.borrow_mut().put(key, val))
Expand All @@ -167,15 +173,15 @@ pub fn frozen() {
#[macro_export]
macro_rules! get_param {
($name:expr, $default:expr) => {{
const CONST_KEY: &str = const_str::replace!(stringify!($name), ";", "");
const CONST_HASH: u64 = xxhash_rust::const_xxh64::xxh64(CONST_KEY.as_bytes(), 42);
const CONST_KEY: &str = ::const_str::replace!(stringify!($name), ";", "");
const CONST_HASH: u64 = ::xxhash_rust::const_xxh64::xxh64(CONST_KEY.as_bytes(), 42);
THREAD_STORAGE.with(|ts| ts.borrow_mut().get_or_else(CONST_HASH, $default))
// ParamScope::default().get_or_else(CONST_HASH, $default)
}};

($name:expr, $default:expr, $help: expr) => {{
const CONST_KEY: &str = const_str::replace!(stringify!($name), ";", "");
const CONST_HASH: u64 = xxhash_rust::const_xxh64::xxh64(CONST_KEY.as_bytes(), 42);
const CONST_KEY: &str = ::const_str::replace!(stringify!($name), ";", "");
const CONST_HASH: u64 = ::xxhash_rust::const_xxh64::xxh64(CONST_KEY.as_bytes(), 42);
// ParamScope::default().get_or_else(CONST_HASH, $default)
{
const CONST_HELP: &str = $help;
Expand Down Expand Up @@ -221,7 +227,7 @@ macro_rules! with_params {
) =>{
let mut ps = ParamScope::default();
{
const CONST_KEY: &str = const_str::replace!(stringify!($($key).+), ";", "");
const CONST_KEY: &str = ::const_str::replace!(stringify!($($key).+), ";", "");
ps.put(CONST_KEY, $val);
}
with_params!(params ps; $($body)*)
Expand All @@ -234,7 +240,7 @@ macro_rules! with_params {
$($body:tt)*
) => {
{
const CONST_KEY: &str = const_str::replace!(stringify!($($key).+), ";", "");
const CONST_KEY: &str = ::const_str::replace!(stringify!($($key).+), ";", "");
$ps.put(CONST_KEY, $val);
}
with_params!(params $ps; $($body)*)
Expand Down Expand Up @@ -322,7 +328,7 @@ macro_rules! with_params_readonly {
) =>{
let mut ps = ParamScope::default();
{
const CONST_KEY: &str = const_str::replace!(stringify!($($key).+), ";", "");
const CONST_KEY: &str = ::const_str::replace!(stringify!($($key).+), ";", "");
ps.put(CONST_KEY, $val);
}
with_params!(params ps; $($body)*)
Expand Down Expand Up @@ -491,7 +497,7 @@ mod tests {
#[test]
fn test_param_scope_with_param_mixed_get_set() {
with_params! {
get a_b_c = a.b.c or 1;
get _a_b_c = a.b.c or 1;
set a.b.c = 3;
get a_b_c = a.b.c or 2;

Expand Down
10 changes: 4 additions & 6 deletions core/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ pub use crate::storage::THREAD_STORAGE;
pub use crate::value::Value;
pub use crate::xxh::XXHashable;

use config;

pub trait AsParamScope {
fn param_scope(self: &Self) -> ParamScope;
fn param_scope(&self) -> ParamScope;
}

impl AsParamScope for config::Config {
fn param_scope(self: &Self) -> ParamScope {
fn param_scope(&self) -> ParamScope {
let mut ps = ParamScope::default();
fn unpack(ps: &mut ParamScope, prefix: Option<String>, value: config::Value) -> () {
fn unpack(ps: &mut ParamScope, prefix: Option<String>, value: config::Value) {
match (prefix, value.kind) {
(None, config::ValueKind::Table(v)) => v.iter().for_each(|(k, v)| {
unpack(ps, Some(k.to_string()), v.clone());
Expand All @@ -25,7 +23,7 @@ impl AsParamScope for config::Config {
(Some(k), config::ValueKind::Float(v)) => ps.put(k, v),
(Some(k), config::ValueKind::String(v)) => ps.put(k, v),
(Some(prefix), config::ValueKind::Table(v)) => v.iter().for_each(|(k, v)| {
unpack(ps, Some(format!("{}.{}", prefix, k.to_string())), v.clone());
unpack(ps, Some(format!("{}.{}", prefix, k)), v.clone());
}),
_ => todo!(),
};
Expand Down
2 changes: 1 addition & 1 deletion core/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn generate_params_help() -> String {
let mut params: Vec<_> = params
.iter()
.map(|kv| {
let mut descs = Vec::from_iter(kv.1.iter().map(|x| x.clone()));
let mut descs = Vec::from_iter(kv.1.iter().cloned());
descs.sort();
(kv.0.clone(), descs.join("\n\t"))
})
Expand Down
27 changes: 11 additions & 16 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,23 @@
#[macro_use]
extern crate proptest;

pub extern crate const_str;
pub extern crate xxhash_rust;
mod storage;
mod value;

mod api;
mod cfg;
mod cli;
mod ffi;
mod xxh;

pub use crate::api::frozen;
pub use crate::api::ParamScope;
pub use crate::api::ParamScopeOps;
pub use crate::cfg::AsParamScope;
pub use crate::cli::generate_params_help;
pub use crate::cli::PARAMS;
pub use crate::storage::GetOrElse;
pub use crate::storage::THREAD_STORAGE;
pub use crate::value::Value;
pub use crate::xxh::xxhash;
pub use crate::xxh::XXHashable;

pub mod storage;
pub mod value;

pub mod api;
pub mod cfg;
pub mod ffi;
pub mod xxh;

// #[cfg(clap)]
pub mod cli;
// #[cfg(clap)]
pub use crate::cli::PARAMS;
pub use crate::cli::generate_params_help;
Loading

0 comments on commit 15ea7e5

Please sign in to comment.