Skip to content
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

Improve performance and reduce allocations #5

Merged
merged 3 commits into from
Nov 28, 2022
Merged

Improve performance and reduce allocations #5

merged 3 commits into from
Nov 28, 2022

Conversation

davidpdrsn
Copy link
Contributor

Checklist

  • I have read the Contributor Guide
  • I have read and agree to the Code of Conduct
  • I have added a description of my changes and why I'd like them included in the section below

Description of Changes

Hello 👋 These changes improve performance and reduce allocations quite a bit. Mostly by writing directly to the underlying writers rather than intermediate strings. The code is slightly less clean but maintains most of the previous structure.

The numbers

Baseline using version from crates.io:

time=601.537292ms
allocations=4.700.014

After these changes:

time=283.841666ms
allocations=70.0014

Benchmark script I used:

use std::time::Instant;

use alloc_counter::AllocCounterSystem;
use tracing::*;
use tracing_logfmt::{EventsFormatter, FieldsFormatter};

#[global_allocator]
static A: AllocCounterSystem = AllocCounterSystem;

fn main() {
    let subscriber = tracing_subscriber::fmt::Subscriber::builder()
        .event_format(EventsFormatter)
        .fmt_fields(FieldsFormatter)
        .with_writer(std::io::sink)
        .finish();

    tracing::subscriber::with_default(subscriber, || {
        let start = Instant::now();

        let ((allocations, _, _), _) = alloc_counter::count_alloc(|| {
            for _ in 0..100_000 {
                info!("message");
                {
                    let _span = info_span!("one", a = 1, b = 2).entered();
                    info!("message in span");
                    {
                        let _span = info_span!("two", a = 1, b = 2).entered();
                        info!("message in span");
                    }
                }
                debug!(a = 1, b = 2, c = false, "message with fields");
            }
        });

        println!("time={:?}", start.elapsed());
        println!("allocations={allocations}");
    });
}

@davidpdrsn davidpdrsn requested a review from fredr as a code owner November 26, 2022 23:54
Copy link
Contributor

@fredr fredr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work ❤️

Just had one nit/question that we can discuss

Copy link
Contributor

@fredr fredr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@fredr fredr merged commit e3607eb into main Nov 28, 2022
@fredr fredr deleted the david/perf branch November 28, 2022 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants