Add smooth profiler for redraw

This commit is contained in:
mars 2022-10-06 19:43:15 -06:00
parent de6f52ab81
commit 2fc9de7822
1 changed files with 41 additions and 4 deletions

View File

@ -1,3 +1,6 @@
use std::collections::VecDeque;
use std::time::Duration;
use alacritty_terminal::ansi::{Color, NamedColor};
use alacritty_terminal::term::cell::{Cell, Flags as CellFlags};
use alacritty_terminal::term::color::{Colors, Rgb};
@ -246,6 +249,8 @@ pub struct Graphics {
pub colors: Colors,
glyph_cache: GlyphCache,
cell_cache: HashMap<Point, GlyphInfo>,
redraw_profiler: Profiler,
present_profiler: Profiler,
}
impl Graphics {
@ -267,6 +272,8 @@ impl Graphics {
glyph_cache,
cell_cache,
colors,
redraw_profiler: Profiler::new("redraw", 300),
present_profiler: Profiler::new("present", 300),
}
}
@ -419,10 +426,40 @@ impl Graphics {
}
}
let copy_start = std::time::Instant::now();
context.set_buffer(&self.canvas.buffer, width as u16, height as u16);
log::debug!("buffer copy time: {:?}", copy_start.elapsed());
self.redraw_profiler.push(start.elapsed());
log::debug!("redraw() time: {:?}", start.elapsed());
let present_start = std::time::Instant::now();
context.set_buffer(&self.canvas.buffer, width as u16, height as u16);
self.present_profiler.push(present_start.elapsed());
}
}
pub struct Profiler {
name: String,
limit: usize,
sum: Duration,
ring: VecDeque<Duration>,
}
impl Profiler {
pub fn new(name: &str, limit: usize) -> Self {
Self {
name: name.to_string(),
limit,
sum: Duration::ZERO,
ring: VecDeque::with_capacity(limit),
}
}
pub fn push(&mut self, time: Duration) {
if self.ring.len() == self.limit {
self.sum -= self.ring.pop_front().unwrap();
}
self.sum += time;
self.ring.push_back(time);
let average = self.sum.div_f32(self.ring.len() as f32);
log::debug!("{} time: {:?}", self.name, average);
}
}