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

Quick improvement: timestamps #26

Open
joaoantoniocardoso opened this issue Sep 17, 2024 · 0 comments · May be fixed by #48
Open

Quick improvement: timestamps #26

joaoantoniocardoso opened this issue Sep 17, 2024 · 0 comments · May be fixed by #48
Assignees
Labels
enhancement New feature or request

Comments

@joaoantoniocardoso
Copy link
Member

joaoantoniocardoso commented Sep 17, 2024

I was checking with callgrind and a considerable amount of energy of the software is concentrated (obviously) on message parsing.

Timestamp is a low-hanging fruit on our side, here is a quick investigation of replacing chrono and dealing with SystemTime ourselves:

Testing approaches with criterion

image

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

fn generate_timestamp_direct() -> u64 {
    let now = SystemTime::now();
    let duration = now.duration_since(UNIX_EPOCH).unwrap();
    let secs = duration.as_secs();
    let nanos = duration.subsec_nanos();
    secs * 1_000_000 + (nanos / 1_000) as u64
}

fn generate_timestamp_with_as_micros() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_micros() as u64)
        .unwrap()
}

fn generate_timestamp_with_as_micros_alt() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_micros().try_into().unwrap())
        .unwrap()
}

fn generate_timestamp_using_chrono() -> u64 {
    chrono::Utc::now().timestamp_micros() as u64
}

fn benchmark_timestamp(c: &mut Criterion) {
    let mut group = c.benchmark_group("generate_timestamp");

    group.bench_function("generate_timestamp_direct", |b| {
        b.iter(|| generate_timestamp_direct())
    });

    group.bench_function("generate_timestamp_with_as_micros", |b| {
        b.iter(|| generate_timestamp_with_as_micros())
    });

    group.bench_function("generate_timestamp_with_as_micros_alt", |b| {
        b.iter(|| generate_timestamp_with_as_micros_alt())
    });

    group.bench_function("generate_timestamp_using_chrono", |b| {
        b.iter(|| generate_timestamp_using_chrono())
    });
}

criterion_group!(benches, benchmark_timestamp);
criterion_main!(benches);

Results in callgrind:

image

@joaoantoniocardoso joaoantoniocardoso added the enhancement New feature or request label Sep 17, 2024
@joaoantoniocardoso joaoantoniocardoso self-assigned this Sep 17, 2024
@joaoantoniocardoso joaoantoniocardoso linked a pull request Oct 4, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant