Allocating with capacity

This commit is contained in:
Marco De Araujo 2025-12-26 09:33:51 -04:00
parent 217ce94e1c
commit 21584f4edf
3 changed files with 7 additions and 6 deletions

View file

@ -7,7 +7,7 @@ use crate::data::{
}; };
pub fn unify_data(status_page: &StatusPageResponse, heartbeat: &HeartbeatResponse) -> UnifiedData { pub fn unify_data(status_page: &StatusPageResponse, heartbeat: &HeartbeatResponse) -> UnifiedData {
let mut groups = Vec::new(); let mut groups = Vec::with_capacity(status_page.public_group_list.len());
let heartbeat_map: HashMap<u64, Vec<HeartbeatEntry>> = heartbeat let heartbeat_map: HashMap<u64, Vec<HeartbeatEntry>> = heartbeat
.monitors .monitors
.iter() .iter()
@ -21,7 +21,7 @@ pub fn unify_data(status_page: &StatusPageResponse, heartbeat: &HeartbeatRespons
.collect(); .collect();
for group in &status_page.public_group_list { for group in &status_page.public_group_list {
let mut monitors = Vec::new(); let mut monitors = Vec::with_capacity(group.monitor_list.len());
for monitor_info in &group.monitor_list { for monitor_info in &group.monitor_list {
let uptime_data = uptime_map.get(&(monitor_info.id, 24)).cloned(); let uptime_data = uptime_map.get(&(monitor_info.id, 24)).cloned();
let heartbeats = heartbeat_map let heartbeats = heartbeat_map

View file

@ -35,7 +35,7 @@ fn layout_groups(area: Rect, groups: &[GroupViewState]) -> Vec<Rect> {
let line_height = content_height as usize / total_lines; let line_height = content_height as usize / total_lines;
let mut current_y = area.y + 1; let mut current_y = area.y + 1;
let mut areas = Vec::new(); let mut areas = Vec::with_capacity(groups.len());
for group in groups { for group in groups {
let group_lines = group.monitors.len() + 2; let group_lines = group.monitors.len() + 2;
@ -185,13 +185,14 @@ fn get_formated_line(text: String, color: Color, modifier: Modifier) -> Line<'st
} }
fn create_status_line_spans(status_history: &[MonitorStatus]) -> Line<'_> { fn create_status_line_spans(status_history: &[MonitorStatus]) -> Line<'_> {
let mut spans = Vec::new();
let recent_status: Vec<_> = status_history let recent_status: Vec<_> = status_history
.iter() .iter()
.rev() .rev()
.take(STATUS_LINE_LENGTH) .take(STATUS_LINE_LENGTH)
.collect(); .collect();
let mut spans = Vec::with_capacity(recent_status.len());
for status in recent_status.iter().rev() { for status in recent_status.iter().rev() {
let c = get_status_char(status); let c = get_status_char(status);
let color = get_status_color(status); let color = get_status_color(status);

View file

@ -47,7 +47,7 @@ impl DashboardViewState {
} }
pub fn from_unified_data(data: UnifiedData) -> Self { pub fn from_unified_data(data: UnifiedData) -> Self {
let mut groups = Vec::new(); let mut groups = Vec::with_capacity(data.groups.len());
for group in data.groups { for group in data.groups {
groups.push(GroupViewState { groups.push(GroupViewState {
@ -92,7 +92,7 @@ fn get_status_history(heartbeats: &[HeartbeatEntry]) -> Vec<MonitorStatus> {
} }
fn add_monitor_view_state(group: UnifiedGroupData) -> Vec<MonitorViewState> { fn add_monitor_view_state(group: UnifiedGroupData) -> Vec<MonitorViewState> {
let mut monitors = Vec::new(); let mut monitors = Vec::with_capacity(group.monitors.len());
for monitor in group.monitors { for monitor in group.monitors {
let status_history = get_status_history(&monitor.heartbeats); let status_history = get_status_history(&monitor.heartbeats);