Skip to content

Commit c6ac665

Browse files
perf: ⚡ Bolt: Remove redundant clone in worker map (#47)
* perf: ⚡ Bolt: Remove redundant clone in worker map Co-authored-by: damacus <40786+damacus@users.noreply.github.com> * style: 🎨 Format code to fix CI * 💡 **What:** Formatted `src/tui/mod.rs` to fix `cargo fmt -- --check` failure. * 🎯 **Why:** To pass CI formatting checks. Co-authored-by: damacus <40786+damacus@users.noreply.github.com> --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 9eca01e commit c6ac665

4 files changed

Lines changed: 87 additions & 5 deletions

File tree

.jules/bolt.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## 2024-05-18 - Remove redundant clone in worker map
2+
3+
**Learning:** Iterating over an owned collection (`Vec<Runner>`) by reference using `.iter()` and then cloning each element to create new structs (`ManagerRow`) results in unnecessary allocations and memory copying, slowing down the mapping process.
4+
**Action:** Changed `runners.iter()` to `runners.into_iter()` to take ownership of the iterator, allowing elements to be moved into the new structs without cloning, resulting in a performance improvement.

src/tui/app.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,13 @@ impl App {
217217
match command {
218218
Command::Workers => {
219219
self.manager_rows = runners
220-
.iter()
221-
.flat_map(|r| {
222-
r.managers.iter().map(move |m| ManagerRow {
220+
.into_iter()
221+
.flat_map(|mut r| {
222+
let tags = std::mem::take(&mut r.tag_list);
223+
r.managers.into_iter().map(move |m| ManagerRow {
223224
runner_id: r.id,
224-
runner_tags: r.tag_list.clone(),
225-
manager: m.clone(),
225+
runner_tags: tags.clone(),
226+
manager: m,
226227
})
227228
})
228229
.collect();

src/tui/app_bench.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use crate::models::manager::RunnerManager;
4+
use crate::models::runner::Runner;
5+
use crate::tui::app::ManagerRow;
6+
7+
#[test]
8+
fn test_benchmark_workers() {
9+
let mut runners = vec![];
10+
for i in 0..1000 {
11+
let mut managers = vec![];
12+
for j in 0..10 {
13+
managers.push(RunnerManager {
14+
id: j,
15+
system_id: format!("test-host-{}", j),
16+
created_at: "2024-01-15T10:30:00.000Z".to_string(),
17+
contacted_at: Some("2024-01-20T14:22:00.000Z".to_string()),
18+
ip_address: Some("10.0.1.1".to_string()),
19+
status: "online".to_string(),
20+
version: Some("17.5.0".to_string()),
21+
revision: None,
22+
platform: None,
23+
architecture: None,
24+
});
25+
}
26+
runners.push(Runner {
27+
id: i,
28+
description: Some("test".to_string()),
29+
ip_address: Some("10.0.1.1".to_string()),
30+
active: true,
31+
paused: false,
32+
is_shared: false,
33+
runner_type: "project_type".to_string(),
34+
status: "online".to_string(),
35+
tag_list: vec!["alm".to_string(), "prod".to_string()],
36+
version: Some("17.5.0".to_string()),
37+
revision: None,
38+
created_at: Some("2024-01-20T14:22:00.000Z".to_string()),
39+
managers,
40+
});
41+
}
42+
43+
let runners_clone = runners.clone();
44+
45+
// BASELINE
46+
let start = std::time::Instant::now();
47+
let _manager_rows: Vec<ManagerRow> = runners_clone
48+
.iter()
49+
.flat_map(|r| {
50+
r.managers.iter().map(move |m| ManagerRow {
51+
runner_id: r.id,
52+
runner_tags: r.tag_list.clone(),
53+
manager: m.clone(),
54+
})
55+
})
56+
.collect();
57+
let duration = start.elapsed();
58+
println!("Benchmark baseline (iter/clone): {:?}", duration);
59+
60+
// IMPROVED
61+
let start2 = std::time::Instant::now();
62+
let _manager_rows2: Vec<ManagerRow> = runners
63+
.into_iter()
64+
.flat_map(|mut r| {
65+
let tags = std::mem::take(&mut r.tag_list);
66+
r.managers.into_iter().map(move |m| ManagerRow {
67+
runner_id: r.id,
68+
runner_tags: tags.clone(),
69+
manager: m,
70+
})
71+
})
72+
.collect();
73+
let duration2 = start2.elapsed();
74+
println!("Benchmark improved (into_iter): {:?}", duration2);
75+
}
76+
}

src/tui/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod app;
2+
pub mod app_bench;
23
pub mod event;
34
pub mod ui;

0 commit comments

Comments
 (0)