From 63ae090515545af377839d4ca57aa9ace4a17eee Mon Sep 17 00:00:00 2001 From: Donough Liu Date: Wed, 12 Apr 2023 06:43:23 +0800 Subject: [PATCH] tracing: `inline` methods to make macro-generated code smaller (#2555) ## Motivation Make `tracing::event!` codegen smaller ## Solution Add `inline` to several functions called by `tracing::event!`. Simple example: https://github.com/ldm0/tracing_test After inlining, executable size drops from 746kb to 697kb (`cargo build --release + strip`), saves 50 bytes per `event!`. Test environment: ``` toolchain: nightly-aarch64-apple-darwin rustc-version: rustc 1.70.0-nightly (88fb1b922 2023-04-10) ``` There are also performance improvements in the benchmarks: ``` event/scoped [-40.689% -40.475% -40.228%] event/scoped_recording [-14.972% -14.685% -14.410%] event/global [-48.412% -48.217% -48.010%] span_fields/scoped [-25.317% -24.876% -24.494%] span_fields/global [-39.695% -39.488% -39.242%] span_repeated/global [-27.514% -26.633% -25.298%] static/baseline_single_threaded [-32.275% -32.032% -31.808%] static/single_threaded [-29.628% -29.376% -29.156%] static/enabled_one [-29.777% -29.544% -29.305%] static/enabled_many [-30.901% -30.504% -30.140%] dynamic/baseline_single_threaded [-20.157% -19.880% -19.603%] ``` I retried benchmark several times and the improvements seem to be fairly stable. raw log: https://gist.github.com/ldm0/6573935f4979d2645fbcf5bde7361386 --- tracing-core/src/field.rs | 3 +++ tracing-core/src/metadata.rs | 1 + tracing/src/macros.rs | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tracing-core/src/field.rs b/tracing-core/src/field.rs index 7e2c26b1d7..04b8e1b297 100644 --- a/tracing-core/src/field.rs +++ b/tracing-core/src/field.rs @@ -820,6 +820,7 @@ impl FieldSet { /// /// [`Identifier`]: super::callsite::Identifier /// [`Callsite`]: super::callsite::Callsite + #[inline] pub(crate) fn callsite(&self) -> callsite::Identifier { callsite::Identifier(self.callsite.0) } @@ -857,6 +858,7 @@ impl FieldSet { } /// Returns an iterator over the `Field`s in this `FieldSet`. + #[inline] pub fn iter(&self) -> Iter { let idxs = 0..self.len(); Iter { @@ -960,6 +962,7 @@ impl PartialEq for FieldSet { impl Iterator for Iter { type Item = Field; + #[inline] fn next(&mut self) -> Option { let i = self.idxs.next()?; Some(Field { diff --git a/tracing-core/src/metadata.rs b/tracing-core/src/metadata.rs index a154419a74..5e475c1294 100644 --- a/tracing-core/src/metadata.rs +++ b/tracing-core/src/metadata.rs @@ -273,6 +273,7 @@ impl<'a> Metadata<'a> { } /// Returns the names of the fields on the described span or event. + #[inline] pub fn fields(&self) -> &field::FieldSet { &self.fields } diff --git a/tracing/src/macros.rs b/tracing/src/macros.rs index f3968e5c11..a737348cbc 100644 --- a/tracing/src/macros.rs +++ b/tracing/src/macros.rs @@ -2311,7 +2311,7 @@ macro_rules! valueset { ) }; - // Remainder is unparseable, but exists --- must be format args! + // Remainder is unparsable, but exists --- must be format args! (@ { $(,)* $($out:expr),* }, $next:expr, $($rest:tt)+) => { $crate::valueset!(@ { (&$next, Some(&format_args!($($rest)+) as &dyn Value)), $($out),* }, $next, ) };