Skip to content

Commit

Permalink
tracing: inline methods to make macro-generated code smaller (#2555)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
ldm0 authored and hawkw committed Apr 21, 2023
1 parent fd17727 commit 334bee7
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -960,6 +962,7 @@ impl PartialEq for FieldSet {

impl Iterator for Iter {
type Item = Field;
#[inline]
fn next(&mut self) -> Option<Field> {
let i = self.idxs.next()?;
Some(Field {
Expand Down
1 change: 1 addition & 0 deletions tracing-core/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion tracing/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, )
};
Expand Down

0 comments on commit 334bee7

Please sign in to comment.