llguidance/earley/
perf.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use std::{
    fmt::{Display, Formatter},
    sync::atomic::AtomicUsize,
    time::Duration,
};

use serde::{ser::SerializeStruct as _, Serialize};

pub struct PerfTimer {
    name: String,
    max_time_us: AtomicUsize,
    time_us: AtomicUsize,
    num_calls: AtomicUsize,
}

impl PerfTimer {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            max_time_us: AtomicUsize::new(0),
            time_us: AtomicUsize::new(0),
            num_calls: AtomicUsize::new(0),
        }
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn get(&self) -> (usize, usize, usize) {
        (
            self.max_time_us.load(std::sync::atomic::Ordering::Relaxed),
            self.time_us.load(std::sync::atomic::Ordering::Relaxed),
            self.num_calls.load(std::sync::atomic::Ordering::Relaxed),
        )
    }

    #[inline(always)]
    pub fn record(&self, d: Duration) {
        let us = Duration::as_micros(&d) as usize;
        self.max_time_us
            .fetch_max(us, std::sync::atomic::Ordering::Relaxed);
        self.time_us
            .fetch_add(us, std::sync::atomic::Ordering::Relaxed);
        self.num_calls
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    }
}

impl Serialize for PerfTimer {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::ser::Serializer,
    {
        let (max_time_us, time_us, num_calls) = self.get();
        let avg = time_us / num_calls;
        let mut state = serializer.serialize_struct("PerfTimer", 4)?;
        state.serialize_field("name", &self.name)?;
        state.serialize_field("avg", &avg)?;
        state.serialize_field("calls", &num_calls)?;
        state.serialize_field("max", &max_time_us)?;
        state.end()
    }
}

impl Display for PerfTimer {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let (max_time_us, time_us, num_calls) = self.get();
        let avg = time_us / std::cmp::max(1, num_calls);
        write!(
            f,
            "{}: avg:{}μs calls:{} max:{}μs total:{}ms",
            self.name,
            num_with_commas(avg),
            num_with_commas(num_calls),
            num_with_commas(max_time_us),
            num_with_commas(time_us / 1000)
        )
    }
}

#[derive(Serialize)]
pub struct ParserPerfCounters {
    pub force_bytes: PerfTimer,
    pub force_bytes_empty: PerfTimer,
    pub tmp_counter: PerfTimer,
    pub tokenize_ff: PerfTimer,
    pub compute_bias: PerfTimer,
    pub compute_mask: PerfTimer,
}

impl ParserPerfCounters {
    pub fn new() -> Self {
        Self {
            force_bytes: PerfTimer::new("force_bytes"),
            force_bytes_empty: PerfTimer::new("force_bytes_empty"),
            tmp_counter: PerfTimer::new("tmp_counter"),
            tokenize_ff: PerfTimer::new("tokenize_ff"),
            compute_bias: PerfTimer::new("compute_bias"),
            compute_mask: PerfTimer::new("compute_mask"),
        }
    }

    pub fn counters(&self) -> Vec<&PerfTimer> {
        vec![
            &self.force_bytes,
            &self.force_bytes_empty,
            &self.tokenize_ff,
            &self.compute_bias,
            &self.compute_mask,
            &self.tmp_counter,
        ]
    }
}

impl Display for ParserPerfCounters {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        for c in self.counters() {
            c.fmt(f)?;
            write!(f, "\n")?;
        }
        Ok(())
    }
}

pub fn num_with_commas(x: usize) -> String {
    let s = x.to_string();
    let len = s.len();
    let offset = len % 3;
    let mut result = String::new();

    for (i, c) in s.chars().enumerate() {
        // Insert a comma once we've passed 'offset' and every 3 digits after that.
        if i != 0 && i >= offset && (i - offset) % 3 == 0 {
            result.push(',');
        }
        result.push(c);
    }

    result
}