Line optimizations
This commit is contained in:
parent
975c4b1eaf
commit
64f5115d54
1 changed files with 8 additions and 7 deletions
|
|
@ -34,7 +34,7 @@ static DOWN_SPAN: Lazy<Span<'static>> =
|
|||
static UNKNOWN_SPAN: Lazy<Span<'static>> =
|
||||
Lazy::new(|| Span::styled("■", Style::default().fg(Color::Yellow)));
|
||||
|
||||
static STATUS_LINE_CACHE: OnceLock<RwLock<HashMap<u64, Vec<&'static Span<'static>>>>> = OnceLock::new();
|
||||
static STATUS_LINE_CACHE: OnceLock<RwLock<HashMap<u64, Line<'static>>>> = OnceLock::new();
|
||||
|
||||
pub fn render_monitor_list(main_frame: &mut Frame, area: Rect, state: &mut DashboardViewState) {
|
||||
let available_height = area.height as usize;
|
||||
|
|
@ -251,18 +251,19 @@ fn get_cached_status_line(status_history: &[MonitorStatus]) -> Line<'static> {
|
|||
let cache = STATUS_LINE_CACHE.get_or_init(|| RwLock::new(HashMap::new()));
|
||||
{
|
||||
let read = cache.read().unwrap();
|
||||
if let Some(spans) = read.get(&hash) {
|
||||
return Line::from(spans.iter().map(|&span| span.clone()).collect::<Vec<_>>());
|
||||
if let Some(line) = read.get(&hash) {
|
||||
return line.clone();
|
||||
}
|
||||
}
|
||||
|
||||
let spans: Vec<&'static Span<'static>> = status_history
|
||||
let spans: Vec<Span<'static>> = status_history
|
||||
.iter()
|
||||
.rev()
|
||||
.take(STATUS_LINE_LENGTH)
|
||||
.map(|status| get_status_span(status))
|
||||
.map(|status| get_status_span(status).clone())
|
||||
.collect();
|
||||
|
||||
let new_line = Line::from(spans);
|
||||
let mut write = cache.write().unwrap();
|
||||
if write.len() > 1000 {
|
||||
let keys_to_remove: Vec<_> = write.keys().take(250).copied().collect();
|
||||
|
|
@ -271,6 +272,6 @@ fn get_cached_status_line(status_history: &[MonitorStatus]) -> Line<'static> {
|
|||
write.remove(&key);
|
||||
}
|
||||
}
|
||||
write.insert(hash, spans.clone());
|
||||
Line::from(spans.iter().map(|&span| span.clone()).collect::<Vec<_>>())
|
||||
write.insert(hash, new_line.clone());
|
||||
new_line
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue