Skip to content

Commit

Permalink
other: remove some unnecessary vec allocations (#1234)
Browse files Browse the repository at this point in the history
* other: remove unnecessary vector allocations in farmer

* other: remove unnecessary vector allocations when drawing tui Rows
  • Loading branch information
ClementTsang authored Jul 1, 2023
1 parent dce30ee commit 3f53818
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
8 changes: 3 additions & 5 deletions src/app/data_farmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ impl DataCollection {
cpu.iter()
.for_each(|cpu| new_entry.cpu_data.push(cpu.cpu_usage));

self.cpu_harvest = cpu.to_vec();
self.cpu_harvest = cpu;
}

fn eat_load_avg(&mut self, load_avg: cpu::LoadAvgHarvest, new_entry: &mut TimedData) {
Expand All @@ -328,14 +328,12 @@ impl DataCollection {
}

fn eat_temp(&mut self, temperature_sensors: Vec<temperature::TempHarvest>) {
// TODO: [PO] To implement
self.temp_harvest = temperature_sensors.to_vec();
self.temp_harvest = temperature_sensors;
}

fn eat_disks(
&mut self, disks: Vec<disks::DiskHarvest>, io: disks::IoHarvest, harvested_time: Instant,
) {
// TODO: [PO] To implement
let time_since_last_harvest = harvested_time
.duration_since(self.current_instant)
.as_secs_f64();
Expand Down Expand Up @@ -457,6 +455,6 @@ impl DataCollection {
gpu.iter().for_each(|data| {
new_entry.gpu_data.push(data.1.use_percent);
});
self.gpu_harvest = gpu.to_vec();
self.gpu_harvest = gpu;
}
}
19 changes: 9 additions & 10 deletions src/canvas/widgets/battery_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl Painter {
}

let mut battery_rows = Vec::with_capacity(4);
battery_rows.push(Row::new(vec![
battery_rows.push(Row::new([
Cell::from("Charge %").style(self.colours.text_style),
Cell::from(bars).style(if charge_percentage < 10.0 {
self.colours.low_battery_colour
Expand All @@ -175,12 +175,12 @@ impl Painter {
}),
]));
battery_rows.push(
Row::new(vec!["Rate", &battery_details.watt_consumption])
Row::new(["Rate", &battery_details.watt_consumption])
.style(self.colours.text_style),
);

battery_rows.push(
Row::new(vec!["State", &battery_details.state]).style(self.colours.text_style),
Row::new(["State", &battery_details.state]).style(self.colours.text_style),
);

let mut s: String; // Keep string in scope.
Expand All @@ -191,20 +191,20 @@ impl Painter {
s = long_time(*secs);

if half_width as usize > s.len() {
battery_rows.push(Row::new(vec!["Time to empty", &s]).style(style));
battery_rows.push(Row::new(["Time to empty", &s]).style(style));
} else {
s = short_time(*secs);
battery_rows.push(Row::new(vec!["To empty", &s]).style(style));
battery_rows.push(Row::new(["To empty", &s]).style(style));
}
}
BatteryDuration::ToFull(secs) => {
s = long_time(*secs);

if half_width as usize > s.len() {
battery_rows.push(Row::new(vec!["Time to full", &s]).style(style));
battery_rows.push(Row::new(["Time to full", &s]).style(style));
} else {
s = short_time(*secs);
battery_rows.push(Row::new(vec!["To full", &s]).style(style));
battery_rows.push(Row::new(["To full", &s]).style(style));
}
}
BatteryDuration::Empty
Expand All @@ -214,15 +214,14 @@ impl Painter {
}

battery_rows.push(
Row::new(vec!["Health %", &battery_details.health])
.style(self.colours.text_style),
Row::new(["Health %", &battery_details.health]).style(self.colours.text_style),
);

// Draw
f.render_widget(
Table::new(battery_rows)
.block(battery_block)
.header(Row::new(vec![""]).bottom_margin(table_gap))
.header(Row::new([""]).bottom_margin(table_gap))
.widths(&[Constraint::Percentage(50), Constraint::Percentage(50)]),
margined_draw_loc,
);
Expand Down
4 changes: 2 additions & 2 deletions src/canvas/widgets/network_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl Painter {
let total_tx_display = &app_state.converted_data.total_tx_display;

// Gross but I need it to work...
let total_network = vec![Row::new(vec![
let total_network = vec![Row::new([
Text::styled(rx_display, self.colours.rx_style),
Text::styled(tx_display, self.colours.tx_style),
Text::styled(total_rx_display, self.colours.total_rx_style),
Expand All @@ -188,7 +188,7 @@ impl Painter {
// Draw
f.render_widget(
Table::new(total_network)
.header(Row::new(NETWORK_HEADERS.to_vec()).style(self.colours.table_header_style))
.header(Row::new(NETWORK_HEADERS).style(self.colours.table_header_style))
.block(Block::default().borders(Borders::ALL).border_style(
if app_state.current_widget.widget_id == widget_id {
self.colours.highlighted_border_style
Expand Down

0 comments on commit 3f53818

Please sign in to comment.