From 5b5557b71d84de67fbda2d0eb09a947eee2823d0 Mon Sep 17 00:00:00 2001 From: Nishant Joshi Date: Tue, 21 Mar 2023 15:12:44 +0530 Subject: [PATCH] feat: compile time optimization (#775) --- crates/drainer/Cargo.toml | 7 +++++-- crates/drainer/build.rs | 1 + crates/drainer/src/settings.rs | 2 +- crates/router/Cargo.toml | 5 +++-- crates/router/src/build.rs | 1 + crates/router/src/configs/settings.rs | 2 +- crates/router_env/Cargo.toml | 2 +- crates/router_env/src/env.rs | 4 +++- crates/router_env/src/logger/formatter.rs | 8 ++++++++ crates/router_env/src/vergen.rs | 4 ++++ 10 files changed, 28 insertions(+), 8 deletions(-) diff --git a/crates/drainer/Cargo.toml b/crates/drainer/Cargo.toml index a98be82e9f..387c0b796a 100644 --- a/crates/drainer/Cargo.toml +++ b/crates/drainer/Cargo.toml @@ -7,12 +7,15 @@ rust-version = "1.65" readme = "README.md" license = "Apache-2.0" +[features] +vergen = [ "router_env/vergen" ] + [dependencies] async-bb8-diesel = { git = "https://github.com/juspay/async-bb8-diesel", rev = "9a71d142726dbc33f41c1fd935ddaa79841c7be5" } bb8 = "0.8" clap = { version = "4.1.8", default-features = false, features = ["std", "derive", "help", "usage"] } config = { version = "0.13.3", features = ["toml"] } -diesel = { version = "2.0.3", features = ["postgres", "serde_json", "time", "64-column-tables"] } +diesel = { version = "2.0.3", features = ["postgres"] } error-stack = "0.3.1" once_cell = "1.17.1" serde = "1.0.155" @@ -28,4 +31,4 @@ router_env = { version = "0.1.0", path = "../router_env", features = ["log_extra storage_models = { version = "0.1.0", path = "../storage_models", features = ["kv_store"] } [build-dependencies] -router_env = { version = "0.1.0", path = "../router_env", default-features = false, features = ["vergen"] } +router_env = { version = "0.1.0", path = "../router_env", default-features = false } \ No newline at end of file diff --git a/crates/drainer/build.rs b/crates/drainer/build.rs index 4b07385af6..a35e30be38 100644 --- a/crates/drainer/build.rs +++ b/crates/drainer/build.rs @@ -1,3 +1,4 @@ fn main() { + #[cfg(feature = "vergen")] router_env::vergen::generate_cargo_instructions(); } diff --git a/crates/drainer/src/settings.rs b/crates/drainer/src/settings.rs index f77853f1b8..5367d0dd59 100644 --- a/crates/drainer/src/settings.rs +++ b/crates/drainer/src/settings.rs @@ -10,7 +10,7 @@ use serde::Deserialize; use crate::errors; #[derive(clap::Parser, Default)] -#[command(version = router_env::version!())] +#[cfg_attr(feature = "vergen", command(version = router_env::version!()))] pub struct CmdLineConf { /// Config file. /// Application will look for "config/config.toml" if this option isn't specified. diff --git a/crates/router/Cargo.toml b/crates/router/Cargo.toml index b2b1ac0da6..2270892c0f 100644 --- a/crates/router/Cargo.toml +++ b/crates/router/Cargo.toml @@ -21,6 +21,7 @@ production = [] kv_store = [] accounts_cache = [] openapi = ["olap", "oltp"] +vergen = [ "router_env/vergen" ] [dependencies] @@ -39,7 +40,7 @@ bytes = "1.4.0" clap = { version = "4.1.8", default-features = false, features = ["std", "derive", "help", "usage"] } config = { version = "0.13.3", features = ["toml"] } crc32fast = "1.3.2" -diesel = { version = "2.0.3", features = ["postgres", "serde_json", "time", "64-column-tables"] } +diesel = { version = "2.0.3", features = ["postgres"] } dyn-clone = "1.0.11" encoding_rs = "0.8.32" error-stack = "0.3.1" @@ -89,7 +90,7 @@ router_env = { version = "0.1.0", path = "../router_env", features = ["log_extra storage_models = { version = "0.1.0", path = "../storage_models", features = ["kv_store"] } [build-dependencies] -router_env = { version = "0.1.0", path = "../router_env", default-features = false, features = ["vergen"] } +router_env = { version = "0.1.0", path = "../router_env", default-features = false } [dev-dependencies] actix-http = "3.3.1" diff --git a/crates/router/src/build.rs b/crates/router/src/build.rs index 1016b99d92..db16158893 100644 --- a/crates/router/src/build.rs +++ b/crates/router/src/build.rs @@ -4,5 +4,6 @@ fn main() { #[cfg(debug_assertions)] println!("cargo:rustc-env=RUST_MIN_STACK=4194304"); // 4 * 1024 * 1024 = 4 MiB + #[cfg(feature = "vergen")] router_env::vergen::generate_cargo_instructions(); } diff --git a/crates/router/src/configs/settings.rs b/crates/router/src/configs/settings.rs index 92891dc1c7..6e1bf525ae 100644 --- a/crates/router/src/configs/settings.rs +++ b/crates/router/src/configs/settings.rs @@ -16,7 +16,7 @@ use crate::{ }; #[derive(clap::Parser, Default)] -#[command(version = router_env::version!())] +#[cfg_attr(feature = "vergen", command(version = router_env::version!()))] pub struct CmdLineConf { /// Config file. /// Application will look for "config/config.toml" if this option isn't specified. diff --git a/crates/router_env/Cargo.toml b/crates/router_env/Cargo.toml index 8c090874d5..753170c3e6 100644 --- a/crates/router_env/Cargo.toml +++ b/crates/router_env/Cargo.toml @@ -33,7 +33,7 @@ vergen = { version = "8.0.0-beta.9", optional = true, features = ["cargo", "git" tokio = { version = "1.26.0", features = ["macros", "rt-multi-thread"] } [build-dependencies] -vergen = { version = "8.0.0-beta.9", features = ["cargo", "git", "git2", "rustc"] } +vergen = { version = "8.0.0-beta.9", features = ["cargo", "git", "git2", "rustc"], optional = true } [features] default = ["actix_web"] diff --git a/crates/router_env/src/env.rs b/crates/router_env/src/env.rs index 60302da455..cba7714cdf 100644 --- a/crates/router_env/src/env.rs +++ b/crates/router_env/src/env.rs @@ -91,6 +91,7 @@ pub fn workspace_path() -> PathBuf { /// - Timestamp of the latest git commit. /// /// Example: `0.1.0-abcd012-2038-01-19T03:14:08Z`. +#[cfg(feature = "vergen")] #[macro_export] macro_rules! version { () => { @@ -117,6 +118,7 @@ macro_rules! version { /// Example: `0.1.0-f5f383e-2022-09-04T11:39:37Z-1.63.0-x86_64-unknown-linux-gnu` /// +#[cfg(feature = "vergen")] #[macro_export] macro_rules! build { () => { @@ -141,7 +143,7 @@ macro_rules! build { /// /// Example: `f5f383ee7e36214d60ce3c6353b57db03ff0ceb1`. /// - +#[cfg(feature = "vergen")] #[macro_export] macro_rules! commit { () => { diff --git a/crates/router_env/src/logger/formatter.rs b/crates/router_env/src/logger/formatter.rs index 2c5ec771ca..97351c8a89 100644 --- a/crates/router_env/src/logger/formatter.rs +++ b/crates/router_env/src/logger/formatter.rs @@ -126,7 +126,9 @@ where hostname: String, env: String, service: String, + #[cfg(feature = "vergen")] version: String, + #[cfg(feature = "vergen")] build: String, default_fields: HashMap, } @@ -159,7 +161,9 @@ where let pid = std::process::id(); let hostname = gethostname::gethostname().to_string_lossy().into_owned(); let service = service.to_string(); + #[cfg(feature = "vergen")] let version = crate::version!().to_string(); + #[cfg(feature = "vergen")] let build = crate::build!().to_string(); let env = crate::env::which().to_string(); @@ -169,7 +173,9 @@ where hostname, env, service, + #[cfg(feature = "vergen")] version, + #[cfg(feature = "vergen")] build, default_fields, } @@ -195,7 +201,9 @@ where map_serializer.serialize_entry(HOSTNAME, &self.hostname)?; map_serializer.serialize_entry(PID, &self.pid)?; map_serializer.serialize_entry(ENV, &self.env)?; + #[cfg(feature = "vergen")] map_serializer.serialize_entry(VERSION, &self.version)?; + #[cfg(feature = "vergen")] map_serializer.serialize_entry(BUILD, &self.build)?; map_serializer.serialize_entry(LEVEL, &format!("{}", metadata.level()))?; map_serializer.serialize_entry(TARGET, metadata.target())?; diff --git a/crates/router_env/src/vergen.rs b/crates/router_env/src/vergen.rs index 93fd4f4f3d..41fb83d30d 100644 --- a/crates/router_env/src/vergen.rs +++ b/crates/router_env/src/vergen.rs @@ -6,6 +6,7 @@ /// # Panics /// /// Panics if `vergen` fails to generate `cargo` build instructions. +#[cfg(feature = "vergen")] #[allow(clippy::expect_used)] pub fn generate_cargo_instructions() { use std::io::Write; @@ -31,3 +32,6 @@ pub fn generate_cargo_instructions() { ) .expect("Failed to set `CARGO_PROFILE` environment variable"); } + +#[cfg(not(feature = "vergen"))] +pub fn generate_cargo_instructions() {}