feat: compile time optimization (#775)

This commit is contained in:
Nishant Joshi
2023-03-21 15:12:44 +05:30
committed by GitHub
parent 575abd6f5e
commit 5b5557b71d
10 changed files with 28 additions and 8 deletions

View File

@ -7,12 +7,15 @@ rust-version = "1.65"
readme = "README.md" readme = "README.md"
license = "Apache-2.0" license = "Apache-2.0"
[features]
vergen = [ "router_env/vergen" ]
[dependencies] [dependencies]
async-bb8-diesel = { git = "https://github.com/juspay/async-bb8-diesel", rev = "9a71d142726dbc33f41c1fd935ddaa79841c7be5" } async-bb8-diesel = { git = "https://github.com/juspay/async-bb8-diesel", rev = "9a71d142726dbc33f41c1fd935ddaa79841c7be5" }
bb8 = "0.8" bb8 = "0.8"
clap = { version = "4.1.8", default-features = false, features = ["std", "derive", "help", "usage"] } clap = { version = "4.1.8", default-features = false, features = ["std", "derive", "help", "usage"] }
config = { version = "0.13.3", features = ["toml"] } 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" error-stack = "0.3.1"
once_cell = "1.17.1" once_cell = "1.17.1"
serde = "1.0.155" 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"] } storage_models = { version = "0.1.0", path = "../storage_models", features = ["kv_store"] }
[build-dependencies] [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 }

View File

@ -1,3 +1,4 @@
fn main() { fn main() {
#[cfg(feature = "vergen")]
router_env::vergen::generate_cargo_instructions(); router_env::vergen::generate_cargo_instructions();
} }

View File

@ -10,7 +10,7 @@ use serde::Deserialize;
use crate::errors; use crate::errors;
#[derive(clap::Parser, Default)] #[derive(clap::Parser, Default)]
#[command(version = router_env::version!())] #[cfg_attr(feature = "vergen", command(version = router_env::version!()))]
pub struct CmdLineConf { pub struct CmdLineConf {
/// Config file. /// Config file.
/// Application will look for "config/config.toml" if this option isn't specified. /// Application will look for "config/config.toml" if this option isn't specified.

View File

@ -21,6 +21,7 @@ production = []
kv_store = [] kv_store = []
accounts_cache = [] accounts_cache = []
openapi = ["olap", "oltp"] openapi = ["olap", "oltp"]
vergen = [ "router_env/vergen" ]
[dependencies] [dependencies]
@ -39,7 +40,7 @@ bytes = "1.4.0"
clap = { version = "4.1.8", default-features = false, features = ["std", "derive", "help", "usage"] } clap = { version = "4.1.8", default-features = false, features = ["std", "derive", "help", "usage"] }
config = { version = "0.13.3", features = ["toml"] } config = { version = "0.13.3", features = ["toml"] }
crc32fast = "1.3.2" 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" dyn-clone = "1.0.11"
encoding_rs = "0.8.32" encoding_rs = "0.8.32"
error-stack = "0.3.1" 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"] } storage_models = { version = "0.1.0", path = "../storage_models", features = ["kv_store"] }
[build-dependencies] [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] [dev-dependencies]
actix-http = "3.3.1" actix-http = "3.3.1"

View File

@ -4,5 +4,6 @@ fn main() {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
println!("cargo:rustc-env=RUST_MIN_STACK=4194304"); // 4 * 1024 * 1024 = 4 MiB println!("cargo:rustc-env=RUST_MIN_STACK=4194304"); // 4 * 1024 * 1024 = 4 MiB
#[cfg(feature = "vergen")]
router_env::vergen::generate_cargo_instructions(); router_env::vergen::generate_cargo_instructions();
} }

View File

@ -16,7 +16,7 @@ use crate::{
}; };
#[derive(clap::Parser, Default)] #[derive(clap::Parser, Default)]
#[command(version = router_env::version!())] #[cfg_attr(feature = "vergen", command(version = router_env::version!()))]
pub struct CmdLineConf { pub struct CmdLineConf {
/// Config file. /// Config file.
/// Application will look for "config/config.toml" if this option isn't specified. /// Application will look for "config/config.toml" if this option isn't specified.

View File

@ -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"] } tokio = { version = "1.26.0", features = ["macros", "rt-multi-thread"] }
[build-dependencies] [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] [features]
default = ["actix_web"] default = ["actix_web"]

View File

@ -91,6 +91,7 @@ pub fn workspace_path() -> PathBuf {
/// - Timestamp of the latest git commit. /// - Timestamp of the latest git commit.
/// ///
/// Example: `0.1.0-abcd012-2038-01-19T03:14:08Z`. /// Example: `0.1.0-abcd012-2038-01-19T03:14:08Z`.
#[cfg(feature = "vergen")]
#[macro_export] #[macro_export]
macro_rules! version { 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` /// Example: `0.1.0-f5f383e-2022-09-04T11:39:37Z-1.63.0-x86_64-unknown-linux-gnu`
/// ///
#[cfg(feature = "vergen")]
#[macro_export] #[macro_export]
macro_rules! build { macro_rules! build {
() => { () => {
@ -141,7 +143,7 @@ macro_rules! build {
/// ///
/// Example: `f5f383ee7e36214d60ce3c6353b57db03ff0ceb1`. /// Example: `f5f383ee7e36214d60ce3c6353b57db03ff0ceb1`.
/// ///
#[cfg(feature = "vergen")]
#[macro_export] #[macro_export]
macro_rules! commit { macro_rules! commit {
() => { () => {

View File

@ -126,7 +126,9 @@ where
hostname: String, hostname: String,
env: String, env: String,
service: String, service: String,
#[cfg(feature = "vergen")]
version: String, version: String,
#[cfg(feature = "vergen")]
build: String, build: String,
default_fields: HashMap<String, Value>, default_fields: HashMap<String, Value>,
} }
@ -159,7 +161,9 @@ where
let pid = std::process::id(); let pid = std::process::id();
let hostname = gethostname::gethostname().to_string_lossy().into_owned(); let hostname = gethostname::gethostname().to_string_lossy().into_owned();
let service = service.to_string(); let service = service.to_string();
#[cfg(feature = "vergen")]
let version = crate::version!().to_string(); let version = crate::version!().to_string();
#[cfg(feature = "vergen")]
let build = crate::build!().to_string(); let build = crate::build!().to_string();
let env = crate::env::which().to_string(); let env = crate::env::which().to_string();
@ -169,7 +173,9 @@ where
hostname, hostname,
env, env,
service, service,
#[cfg(feature = "vergen")]
version, version,
#[cfg(feature = "vergen")]
build, build,
default_fields, default_fields,
} }
@ -195,7 +201,9 @@ where
map_serializer.serialize_entry(HOSTNAME, &self.hostname)?; map_serializer.serialize_entry(HOSTNAME, &self.hostname)?;
map_serializer.serialize_entry(PID, &self.pid)?; map_serializer.serialize_entry(PID, &self.pid)?;
map_serializer.serialize_entry(ENV, &self.env)?; map_serializer.serialize_entry(ENV, &self.env)?;
#[cfg(feature = "vergen")]
map_serializer.serialize_entry(VERSION, &self.version)?; map_serializer.serialize_entry(VERSION, &self.version)?;
#[cfg(feature = "vergen")]
map_serializer.serialize_entry(BUILD, &self.build)?; map_serializer.serialize_entry(BUILD, &self.build)?;
map_serializer.serialize_entry(LEVEL, &format!("{}", metadata.level()))?; map_serializer.serialize_entry(LEVEL, &format!("{}", metadata.level()))?;
map_serializer.serialize_entry(TARGET, metadata.target())?; map_serializer.serialize_entry(TARGET, metadata.target())?;

View File

@ -6,6 +6,7 @@
/// # Panics /// # Panics
/// ///
/// Panics if `vergen` fails to generate `cargo` build instructions. /// Panics if `vergen` fails to generate `cargo` build instructions.
#[cfg(feature = "vergen")]
#[allow(clippy::expect_used)] #[allow(clippy::expect_used)]
pub fn generate_cargo_instructions() { pub fn generate_cargo_instructions() {
use std::io::Write; use std::io::Write;
@ -31,3 +32,6 @@ pub fn generate_cargo_instructions() {
) )
.expect("Failed to set `CARGO_PROFILE` environment variable"); .expect("Failed to set `CARGO_PROFILE` environment variable");
} }
#[cfg(not(feature = "vergen"))]
pub fn generate_cargo_instructions() {}