-
Notifications
You must be signed in to change notification settings - Fork 260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Binary size optimization experiment #569
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,337 @@ | ||
use self::sealed::{LogArgs, LogKVs, LogLevel, LogTarget}; | ||
use crate::{Level, LevelFilter, Metadata, Record}; | ||
use std::cmp::Ordering; | ||
pub use std::convert::identity; | ||
use std::fmt::Arguments; | ||
use std::option::Option; | ||
pub use std::primitive::str; | ||
pub use std::{file, format_args, line, module_path, stringify}; | ||
|
||
#[cfg(feature = "kv_unstable")] | ||
pub type LogKvValue<'a> = dyn crate::kv::value::ToValue + 'a; | ||
|
||
#[cfg(not(feature = "kv_unstable"))] | ||
pub type LogKvValue<'a> = str; | ||
|
||
mod sealed { | ||
use crate::Level; | ||
use std::fmt::Arguments; | ||
|
||
pub trait LogLevel { | ||
fn into_log_level(self) -> Level; | ||
} | ||
|
||
pub trait LogArgs { | ||
fn with(self, f: impl FnOnce(Arguments)); | ||
} | ||
|
||
pub trait LogTarget { | ||
fn as_str(&self, module_path: &'static str) -> &str; | ||
} | ||
|
||
pub trait LogKVs { | ||
fn as_slice(&self) -> &[(&str, &super::LogKvValue)]; | ||
} | ||
} | ||
|
||
// `LogLevel`. | ||
|
||
impl LogLevel for Level { | ||
#[inline] | ||
fn into_log_level(self) -> Level { | ||
self | ||
} | ||
} | ||
|
||
macro_rules! define_static_levels { | ||
($($ty:ident => $lvl:ident,)*) => { | ||
$( | ||
#[derive(Debug)] | ||
pub struct $ty; | ||
|
||
impl LogLevel for $ty { | ||
#[inline] | ||
fn into_log_level(self) -> Level { | ||
Level::$lvl | ||
} | ||
} | ||
|
||
impl PartialEq<LevelFilter> for $ty { | ||
#[inline] | ||
fn eq(&self, other: &LevelFilter) -> bool { | ||
Level::$lvl.eq(other) | ||
} | ||
} | ||
|
||
impl PartialOrd<LevelFilter> for $ty { | ||
#[inline] | ||
fn partial_cmp(&self, other: &LevelFilter) -> Option<Ordering> { | ||
Level::$lvl.partial_cmp(other) | ||
} | ||
|
||
#[inline] | ||
fn lt(&self, other: &LevelFilter) -> bool { | ||
Level::$lvl.lt(other) | ||
} | ||
|
||
#[inline] | ||
fn le(&self, other: &LevelFilter) -> bool { | ||
Level::$lvl.le(other) | ||
} | ||
|
||
#[inline] | ||
fn gt(&self, other: &LevelFilter) -> bool { | ||
Level::$lvl.gt(other) | ||
} | ||
|
||
#[inline] | ||
fn ge(&self, other: &LevelFilter) -> bool { | ||
Level::$lvl.ge(other) | ||
} | ||
} | ||
)* | ||
}; | ||
} | ||
|
||
define_static_levels![ | ||
StaticLevelError => Error, | ||
StaticLevelWarn => Warn, | ||
StaticLevelInfo => Info, | ||
StaticLevelDebug => Debug, | ||
StaticLevelTrace => Trace, | ||
]; | ||
|
||
// `LogArgs`. | ||
|
||
impl LogArgs for &'static str { | ||
#[inline] | ||
fn with(self, f: impl FnOnce(Arguments)) { | ||
f(format_args!("{self}")) | ||
} | ||
} | ||
|
||
impl LogArgs for Arguments<'_> { | ||
#[inline] | ||
fn with(self, f: impl FnOnce(Arguments)) { | ||
f(self) | ||
} | ||
} | ||
|
||
// `LogTarget`. | ||
|
||
impl LogTarget for &str { | ||
#[inline] | ||
fn as_str(&self, _module_path: &'static str) -> &str { | ||
self | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct TargetIsModulePath; | ||
|
||
impl LogTarget for TargetIsModulePath { | ||
#[inline] | ||
fn as_str(&self, module_path: &'static str) -> &str { | ||
module_path | ||
} | ||
} | ||
|
||
// `LogKVs`. | ||
|
||
impl LogKVs for &[(&str, &LogKvValue<'_>)] { | ||
#[inline] | ||
fn as_slice(&self) -> &[(&str, &LogKvValue)] { | ||
self | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct EmptyKVs; | ||
|
||
impl LogKVs for EmptyKVs { | ||
#[inline] | ||
fn as_slice(&self) -> &[(&str, &LogKvValue)] { | ||
&[] | ||
} | ||
} | ||
|
||
// Log functions. | ||
|
||
fn log_0( | ||
&(module_path, file): &'static (&'static str, &'static str), | ||
line: u32, | ||
level: Level, | ||
args: Arguments, | ||
target: &str, | ||
kvs: &[(&str, &LogKvValue)], | ||
) { | ||
#[cfg(not(feature = "kv_unstable"))] | ||
if !kvs.is_empty() { | ||
panic!( | ||
"key-value support is experimental and must be enabled using the `kv_unstable` feature" | ||
); | ||
} | ||
|
||
let mut builder = Record::builder(); | ||
|
||
builder | ||
.args(args) | ||
.level(level) | ||
.target(target) | ||
.module_path_static(Some(module_path)) | ||
.file_static(Some(file)) | ||
.line(Some(line)); | ||
|
||
#[cfg(feature = "kv_unstable")] | ||
builder.key_values(&kvs); | ||
|
||
crate::logger().log(&builder.build()); | ||
} | ||
|
||
fn log_1<K>( | ||
module_path_and_file: &'static (&'static str, &'static str), | ||
line: u32, | ||
level: Level, | ||
args: Arguments, | ||
target: &str, | ||
kvs: K, | ||
) where | ||
K: LogKVs, | ||
{ | ||
log_0( | ||
module_path_and_file, | ||
line, | ||
level, | ||
args, | ||
target, | ||
kvs.as_slice(), | ||
); | ||
} | ||
|
||
fn log_2<T, K>( | ||
module_path_and_file: &'static (&'static str, &'static str), | ||
line: u32, | ||
level: Level, | ||
args: Arguments, | ||
target: T, | ||
kvs: K, | ||
) where | ||
T: LogTarget, | ||
K: LogKVs, | ||
{ | ||
log_1( | ||
module_path_and_file, | ||
line, | ||
level, | ||
args, | ||
target.as_str(module_path_and_file.0), | ||
kvs, | ||
) | ||
} | ||
|
||
pub fn log_3<A, T, K>( | ||
module_path_and_file: &'static (&'static str, &'static str), | ||
line: u32, | ||
level: Level, | ||
args: A, | ||
target: T, | ||
kvs: K, | ||
) where | ||
A: LogArgs, | ||
T: LogTarget, | ||
K: LogKVs, | ||
{ | ||
args.with(|args| log_2(module_path_and_file, line, level, args, target, kvs)) | ||
} | ||
|
||
pub fn log<L, A, T, K>( | ||
module_path_and_file: &'static (&'static str, &'static str), | ||
line: u32, | ||
level: L, | ||
args: A, | ||
target: T, | ||
kvs: K, | ||
) where | ||
L: LogLevel, | ||
A: LogArgs, | ||
T: LogTarget, | ||
K: LogKVs, | ||
{ | ||
log_3( | ||
module_path_and_file, | ||
line, | ||
level.into_log_level(), | ||
args, | ||
target, | ||
kvs, | ||
) | ||
} | ||
|
||
pub fn enabled(level: Level, target: &str) -> bool { | ||
crate::logger().enabled(&Metadata::builder().level(level).target(target).build()) | ||
} | ||
|
||
/// Check whether the format argument can be treated as a string literal. | ||
pub const fn is_literal(s: &str) -> bool { | ||
let s = s.as_bytes(); | ||
let n = s.len(); | ||
let mut i = 0; | ||
|
||
while i < n { | ||
if matches!(s[i], b'{' | b'}') { | ||
return false; | ||
} | ||
|
||
i += 1; | ||
} | ||
|
||
true | ||
} | ||
|
||
/// This function is used for persuading the compiler to generate all `log` generic function instances, so they can be | ||
/// reused by downstream crates, it is not intended for calling by anyone. | ||
pub fn instantiate_log_functions() -> usize { | ||
fn for_all_kvs<L, A, T>() -> usize | ||
where | ||
L: LogLevel, | ||
A: LogArgs, | ||
T: LogTarget, | ||
{ | ||
type K0 = &'static [(&'static str, &'static LogKvValue<'static>)]; | ||
type K1 = EmptyKVs; | ||
|
||
log::<L, A, T, K0> as usize ^ log::<L, A, T, K1> as usize | ||
} | ||
|
||
fn for_all_targets<L, A>() -> usize | ||
where | ||
L: LogLevel, | ||
A: LogArgs, | ||
{ | ||
type T0 = &'static str; | ||
type T1 = TargetIsModulePath; | ||
|
||
for_all_kvs::<L, A, T0>() ^ for_all_kvs::<L, A, T1>() | ||
} | ||
|
||
fn for_all_arguments<L>() -> usize | ||
where | ||
L: LogLevel, | ||
{ | ||
type A0 = &'static str; | ||
type A1 = Arguments<'static>; | ||
|
||
for_all_targets::<L, A0>() ^ for_all_targets::<L, A1>() | ||
} | ||
|
||
[ | ||
for_all_arguments::<Level>(), | ||
for_all_arguments::<StaticLevelError>(), | ||
for_all_arguments::<StaticLevelWarn>(), | ||
for_all_arguments::<StaticLevelInfo>(), | ||
for_all_arguments::<StaticLevelDebug>(), | ||
for_all_arguments::<StaticLevelTrace>(), | ||
] | ||
.iter() | ||
.fold(0, |x, y| x ^ y) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could try passing
&Arguments<'_>
here and in alllog_*
instead of passing generics here.I still think losing
Arguments::as_str
as an optimization is bad and passing it by reference could make up for it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added a const function to check whether the format argument is a string literal as a not perfect replacement for
Arguments::as_str
, that’s why I used a generic argument here. Sometimes I need to pass&str
asargs
.As for
&Arguments
, I think I should do some test, then decide which one to use.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was actually referring to using
Argument::as_str
inLog::log
, the implementation might be able to avoid heap alloc usingArgument::as_str
, passing&str
here breaks that optimization.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure I understand.
Log::log
is not implemented by this crate, so how do I utilizeArgument::as_str
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementator of
Log::log
could useArgument::as_str
to avoid heap allocation.Now that you decided to pass arg as
&str
if possible, they can no longer utilize this optimization.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point. Maybe this can be optimized by
format_args!
as a special case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally yes, but that will require the args to be
&'static str
, which involves lifetime and is a bit hard to do.I guess we can ask @m-ou-se for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A simpler approach could be
impl<'a> From<&'a &'static str> for Arguments<'a>
, utilizingArguments::new_const
.