From 6c1a48aab1f2899fe1507df803a28eb9af1a37f6 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Sun, 26 Nov 2023 16:44:28 +0800 Subject: [PATCH] Group arguments that are likely constant --- src/__private_api.rs | 42 ++++++++++++++++++++---------------------- src/macros.rs | 16 ++++++++++++---- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/__private_api.rs b/src/__private_api.rs index 359deebbc..3587d515a 100644 --- a/src/__private_api.rs +++ b/src/__private_api.rs @@ -37,9 +37,8 @@ impl<'a> KVs<'a> for () { // Log implementation. fn log_impl( - args: Arguments, - level: Level, - &(target, module_path, file): &(&str, &'static str, &'static str), + fmt_args: Arguments, + const_args: &LikelyConstantArgs, line: u32, kvs: Option<&[(&str, &Value)]>, ) { @@ -53,11 +52,11 @@ fn log_impl( let mut builder = Record::builder(); builder - .args(args) - .level(level) - .target(target) - .module_path_static(Some(module_path)) - .file_static(Some(file)) + .args(fmt_args) + .level(const_args.level) + .target(const_args.target) + .module_path_static(Some(const_args.module_path)) + .file_static(Some(const_args.file)) .line(Some(line)); #[cfg(feature = "kv_unstable")] @@ -66,22 +65,21 @@ fn log_impl( crate::logger().log(&builder.build()); } -pub fn log<'a, K>( - args: Arguments, - level: Level, - target_module_path_and_file: &(&str, &'static str, &'static str), - line: u32, - kvs: K, -) where +// Group arguments that are likely constant together so that the compiler can reuse these arguments +// between different calls. +#[derive(Debug)] +pub struct LikelyConstantArgs<'a> { + pub level: Level, + pub target: &'a str, + pub module_path: &'static str, + pub file: &'static str, +} + +pub fn log<'a, K>(fmt_args: Arguments, const_args: &LikelyConstantArgs, line: u32, kvs: K) +where K: KVs<'a>, { - log_impl( - args, - level, - target_module_path_and_file, - line, - kvs.into_kvs(), - ) + log_impl(fmt_args, const_args, line, kvs.into_kvs()) } pub fn enabled(level: Level, target: &str) -> bool { diff --git a/src/macros.rs b/src/macros.rs index 44945f0d9..49cda6620 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -35,8 +35,12 @@ macro_rules! log { if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() { $crate::__private_api::log::<&_>( $crate::__private_api::format_args!($($arg)+), - lvl, - &($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()), + &$crate::__private_api::LikelyConstantArgs { + level: lvl, + target: $target, + module_path: $crate::__private_api::module_path!(), + file: $crate::__private_api::file!(), + }, $crate::__private_api::line!(), &[$(($crate::__log_key!($key), &$value)),+] ); @@ -49,8 +53,12 @@ macro_rules! log { if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() { $crate::__private_api::log( $crate::__private_api::format_args!($($arg)+), - lvl, - &($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()), + &$crate::__private_api::LikelyConstantArgs { + level: lvl, + target: $target, + module_path: $crate::__private_api::module_path!(), + file: $crate::__private_api::file!(), + }, $crate::__private_api::line!(), (), );